matlab.unittest.constraints.BooleanConstraint 类
命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.Constraint
支持布尔运算的约束的基础接口
描述
matlab.unittest.constraints.BooleanConstraint
类提供接口,您可以使用该接口创建约束,可以使用 and
(&
)、or
(|
) 和 not
(~
) 运算符对这些约束进行组合和求反。
要创建支持布尔运算的自定义约束类,请从 matlab.unittest.constraints.BooleanConstraint
派生您的类,并实现所需的抽象方法:
实现
satisfiedBy
方法以编写比较逻辑。BooleanConstraint
类从matlab.unittest.constraints.Constraint
继承此方法。实现
getDiagnosticFor
方法,用于在测试框架根据约束评估实际值时产生诊断信息。BooleanConstraint
类从Constraint
类继承此方法。实现
getNegativeDiagnosticFor
方法,以便在框架根据求反约束评估实际值时产生诊断信息。当对约束求反时,必须使用与标准(非求反)用法不同的形式写入诊断信息。
由于 BooleanConstraint
类派生自 Constraint
类,因此 BooleanConstraint
子类支持 Constraint
子类提供的功能。例如,您可以将它们与 matlab.unittest.qualifications
命名空间中的 assertThat
、assumeThat
、fatalAssertThat
和 verifyThat
鉴定方法结合使用。此外,您还可以对 BooleanConstraint
对象求反或将其与其他 BooleanConstraint
对象组合使用。
方法
专用运算符和函数
这些方法为此类的对象指定运算符和函数。
and (& ) | 返回该约束和另一个约束的逻辑合取。以这种方式组合两个约束指定实际值必须同时满足这两个约束。如果不满足其中任一约束,就会出现鉴定失败。
|
or (| ) | 返回该约束和另一个约束的逻辑析取。以这种方式组合两个约束指定实际值必须满足其中任一约束。如果两个约束都不满足,则鉴定失败。
|
not (~ ) | 返回约束的逻辑补码。对约束求反指定实际值不能满足约束。如果满足约束,则鉴定失败。
|
受保护方法
getNegativeDiagnosticFor |
为根据求反约束评估的值产生诊断信息。此方法产生的诊断信息采用否定形式的约束来表述。例如,假设有一个 与 输入参量
输出参量
|
示例
在测试中使用布尔约束
一些内置约束(如 HasElementCount
和 HasLength
)是 BooleanConstraint
的子类。您可以在测试中对这些约束进行组合和求反。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.HasElementCount import matlab.unittest.constraints.HasLength import matlab.unittest.constraints.HasInf import matlab.unittest.constraints.HasNaN import matlab.unittest.constraints.IsEqualTo import matlab.unittest.constraints.IsGreaterThanOrEqualTo import matlab.unittest.constraints.IsReal
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
验证值 3
为实数并且大于或等于 3
。
testCase.verifyThat(3,IsReal & IsGreaterThanOrEqualTo(3))
Verification passed.
验证值 3
不等于 4
。
testCase.verifyThat(3,~IsEqualTo(4))
Verification passed.
测试矩阵 [1 2 3; 4 5 6]
的长度是否为六,即是否包含六个元素。测试通过,原因是 or
条件之一为 true。
testCase.verifyThat([1 2 3; 4 5 6],HasLength(6) | HasElementCount(6))
Verification passed.
测试向量 [3 NaN 5]
是否有 NaN
和无限值。测试失败,原因是 and
条件之一为 false。
testCase.verifyThat([3 NaN 5],HasNaN & HasInf)
Verification failed. --------------------- Framework Diagnostic: --------------------- AndConstraint failed. --> + [First Condition]: | HasNaN passed. | --> Indices that have NaN values: | 2 | | Actual Value: | 3 NaN 5 --> AND + [Second Condition]: | HasInf failed. | --> At least one element must be Inf or -Inf. | | Actual Value: | 3 NaN 5 -+---------------------
创建布尔约束以测试大小
创建一个布尔约束,该约束确定一个值的大小是否与预期值的大小相同。
在当前文件夹下的一个文件中,创建一个名为 IsSameSizeAs
的类,该类派生自 matlab.unittest.constraints.BooleanConstraint
,并实现 satisfiedBy
、getDiagnosticFor
和 getNegativeDiagnosticFor
方法。
classdef IsSameSizeAs < matlab.unittest.constraints.BooleanConstraint properties (SetAccess=immutable) ValueWithExpectedSize end methods % Class constructor function constraint = IsSameSizeAs(value) constraint.ValueWithExpectedSize = value; end % Determine if the actual value satisfies the constraint function tf = satisfiedBy(constraint,actual) tf = constraint.sizeMatchesExpected(actual); end % Produce a diagnostic for the constraint function diagnostic = getDiagnosticFor(constraint,actual) import matlab.automation.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(actual) diagnostic = StringDiagnostic("IsSameSizeAs passed."); else diagnostic = StringDiagnostic( ... "IsSameSizeAs failed." + newline + "Actual Size: [" ... + int2str(size(actual)) + "]" + newline ... + "Expected Size: [" ... + int2str(size(constraint.ValueWithExpectedSize)) ... + "]"); end end end methods (Access=protected) % Produce a diagnostic for the negated constraint function diagnostic = getNegativeDiagnosticFor(constraint,actual) import matlab.automation.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(actual) diagnostic = StringDiagnostic( ... "Negated IsSameSizeAs failed." + newline + ... "Actual and expected sizes were the same ([" ... + int2str(size(actual)) + ... "]) but should not have been."); else diagnostic = StringDiagnostic( ... "Negated IsSameSizeAs passed."); end end end methods (Access=private) % Determine if the actual and expected values are the same size function tf = sizeMatchesExpected(constraint,actual) tf = isequal(size(actual), ... size(constraint.ValueWithExpectedSize)); end end end
创建一个供交互测试的测试用例。
testCase = matlab.unittest.TestCase.forInteractiveUse;
通过使用 IsSameSizeAs
布尔约束,验证一个 5×5 零点矩阵与一个 5×5 全 1 矩阵大小相同,而与一个由 1 组成的 1×5 向量大小不同。
testCase.verifyThat(zeros(5), ...
IsSameSizeAs(ones(5)) & ~IsSameSizeAs(ones(1,5)))
Verification passed.
版本历史记录
在 R2013a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)