Main Content

创建自定义约束

此示例演示如何创建一个自定义约束,该约束确定给定值的大小是否与预期值的大小相同。

在当前文件夹中的一个文件中,创建名为 IsSameSizeAs 的类,该类派生自 matlab.unittest.constraints.Constraint 类。类构造函数接受预期值,该预期值的大小会与实际值的大小进行比较。预期值存储在 ValueWithExpectedSize 属性中。建议的做法是使 Constraint 实现不可变,因此将属性 SetAccess 特性设置为 immutable

classdef IsSameSizeAs < matlab.unittest.constraints.Constraint
    properties (SetAccess=immutable)
        ValueWithExpectedSize
    end
    
    methods
        function constraint = IsSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end
    end 
end

在具有 private 访问权限的 methods 块中,定义辅助方法 sizeMatchesExpected,该方法确定实际值和预期值是否大小相同。该方法由其他约束方法调用。

    methods (Access=private)
        function tf = sizeMatchesExpected(constraint,actual)
            tf = isequal(size(actual), ...
                size(constraint.ValueWithExpectedSize));
        end
    end

matlab.unittest.constraints.Constraint 类派生的类必须实现 satisfiedBy 方法。此方法必须包含比较逻辑并返回一个逻辑值。在 methods 块中,通过调用辅助方法实现 satisfiedBy。如果实际大小和预期大小相等,该方法返回 true

    methods
        function tf = satisfiedBy(constraint,actual)
            tf = constraint.sizeMatchesExpected(actual);
        end
    end

matlab.unittest.constraints.Constraint 类派生的类还必须实现 getDiagnosticFor 方法。此方法必须针对约束计算实际值并提供 Diagnostic 对象。在此示例中,getDiagnosticFor 返回 StringDiagnostic 对象。

    methods
        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

IsSameSizeAs 类定义

这是 IsSameSizeAs 类的完整代码。

classdef IsSameSizeAs < matlab.unittest.constraints.Constraint
    properties (SetAccess=immutable)
        ValueWithExpectedSize
    end

    methods
        function constraint = IsSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end

        function tf = satisfiedBy(constraint,actual)
            tf = constraint.sizeMatchesExpected(actual);
        end

        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=private)
        function tf = sizeMatchesExpected(constraint,actual)
            tf = isequal(size(actual), ...
                size(constraint.ValueWithExpectedSize));
        end
    end
end

测试预期大小

在命令提示符处,创建测试用例以执行交互式测试。

import matlab.unittest.TestCase
testCase = TestCase.forInteractiveUse;

测试一个通过的用例。

testCase.verifyThat(zeros(5),IsSameSizeAs(repmat(1,5)))
Verification passed.

测试一个失败的用例。

testCase.verifyThat(zeros(5),IsSameSizeAs(ones(1,5)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSameSizeAs failed.
    Actual Size: [5  5]
    ExpectedSize: [1  5]

另请参阅

相关主题