主要内容

本页翻译不是最新的。点击此处可查看最新英文版本。

matlab.unittest.constraints.BooleanConstraint 类

R2026a

命名空间: 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 对象组合使用。

类属性

Abstract
true

有关类属性的信息,请参阅类属性。

方法

全部展开

示例

全部折叠

一些内置约束(如 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 中推出