Main Content

创建自定义布尔约束

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

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

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

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

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

matlab.unittest.constraints.BooleanConstraint 类是 matlab.unittest.constraints.Constraint 类的子类。因此,从 BooleanConstraint 类派生的类必须覆盖 Constraint 类的方法。在 methods 块内,覆盖 satisfiedBygetDiagnosticFor 方法。satisfiedBy 实现必须包含比较逻辑并返回一个逻辑值。getDiagnosticFor 实现必须针对约束对实际值进行计算,并提供 Diagnostic 对象。在此示例中,getDiagnosticFor 返回 StringDiagnostic 对象。

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

        function diag = getDiagnosticFor(constraint,actual)
            import matlab.automation.diagnostics.StringDiagnostic
            if constraint.sizeMatchesExpected(actual)
                diag = StringDiagnostic('HasSameSizeAs passed.');
            else
                diag = StringDiagnostic(sprintf(...
                    'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...
                    int2str(size(actual)),...
                    int2str(size(constraint.ValueWithExpectedSize))));
            end
        end
    end

派生自 BooleanConstraint 的类必须实现 getNegativeDiagnosticFor 方法。当约束取反时,此方法必须提供 Diagnostic 对象。

在具有 protected 访问权限的 methods 块中覆盖 getNegativeDiagnosticFor

    methods(Access = protected)
        function diag = getNegativeDiagnosticFor(constraint,actual)
            import matlab.automation.diagnostics.StringDiagnostic
            if constraint.sizeMatchesExpected(actual)
                diag = StringDiagnostic(sprintf(...
                    ['Negated HasSameSizeAs failed.\nSize [%s] of '...
                    'Actual Value and Expected Value were the same '...
                    'but should not have been.'],int2str(size(actual))));
            else
                diag = StringDiagnostic('Negated HasSameSizeAs passed.');
            end
        end
    end

为实现必需方法,约束继承相应的 andornot 重载,以便与其他 BooleanConstraint 对象进行组合或求反。

HasSameSizeAs 类定义

这是 HasSameSizeAs 类的完整代码。

classdef HasSameSizeAs < matlab.unittest.constraints.BooleanConstraint
    
    properties(SetAccess = immutable)
        ValueWithExpectedSize
    end
    
    methods
        function constraint = HasSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end
        
        function bool = satisfiedBy(constraint,actual)
            bool = constraint.sizeMatchesExpected(actual);
        end
        
        function diag = getDiagnosticFor(constraint,actual)
            import matlab.automation.diagnostics.StringDiagnostic
            
            if constraint.sizeMatchesExpected(actual)
                diag = StringDiagnostic('HasSameSizeAs passed.');
            else
                diag = StringDiagnostic(sprintf(...
                    'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...
                    int2str(size(actual)),...
                    int2str(size(constraint.ValueWithExpectedSize))));
            end
        end
    end
    
    methods(Access = protected)
        function diag = getNegativeDiagnosticFor(constraint,actual)
            import matlab.automation.diagnostics.StringDiagnostic
            if constraint.sizeMatchesExpected(actual)
                diag = StringDiagnostic(sprintf(...
                    ['Negated HasSameSizeAs failed.\nSize [%s] of '...
                    'Actual Value and Expected Value were the same '...
                    'but should not have been.'],int2str(size(actual))));
            else
                diag = StringDiagnostic('Negated HasSameSizeAs passed.');
            end
        end
    end

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

end

测试预期大小

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

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

测试一个通过的用例。测试通过,原因是 or 条件之一 HasLength(5) 为 true。

testCase.verifyThat(zeros(5),HasLength(5) | ~HasSameSizeAs(repmat(1,5)))
Verification passed.

测试一个失败的用例。测试失败,原因是 and 条件之一 ~HasSameSizeAs(repmat(1,5)) 为 false。

testCase.verifyThat(zeros(5),HasLength(5) & ~HasSameSizeAs(repmat(1,5)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    AndConstraint failed.
    --> + [First Condition]:
         |   HasLength passed.
         |   
         |   Actual Value:
         |        0     0     0     0     0
         |        0     0     0     0     0
         |        0     0     0     0     0
         |        0     0     0     0     0
         |        0     0     0     0     0
         |   Expected Length:
         |        5
    --> AND
        + [Second Condition]:
         |   Negated HasSameSizeAs failed.
         |   Size [5  5] of Actual Value and Expected Value were the same but should not have been.
        -+---------------------

另请参阅

| | | | | |

相关主题