Main Content

getNegativeDiagnosticFor

类: matlab.unittest.constraints.BooleanConstraint
包: matlab.unittest.constraints

生成值的相反情况诊断信息

说明

示例

diag = getNegativeDiagnosticFor(constObj,actVal) 根据约束 constObj 对提供的值 actVal 进行分析,并生成 matlab.automation.diagnostics.Diagnostic 对象 diag,该对象对应于 constObj 的求反情况。此方法为受保护方法。

此方法生成的诊断信息以约束的相反情形表示。例如,假设有一个 IsTasty 约束。当在验证中使用对 IsTasty 的求反时,如果发现实际值为“tasty”,则测试失败。因此,getNegativeDiagnosticFor 应返回说明为什么该值不能正确满足约束的详细信息。

ConstraintgetDiagnosticFor 方法类似,通常是在验证失败时调用 getNegativeDiagnosticFor 方法。因此,与 satisfiedBy 方法相比,验证失败后调用 getNegativeDiagnosticFor 方法能够更高效地提供更详细的分析信息。

输入参数

constObj

BooleanConstraint 实例

actVal

要比较的值

示例

全部展开

创建一个自定义布尔约束,该约束确定给定值的大小是否与预期值的大小相同。实现 getNegativeDiagnosticFor 方法,以在对约束求反时提供一个 Diagnostic 对象。

classdef HasSameSizeAs < matlab.unittest.constraints.BooleanConstraint
    
    properties(SetAccess = immutable)
        ValueWithExpectedSize
    end
    
    methods
        % Class constructor
        function constraint = HasSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end
        
        % Determine if the actual value satisfies the constraint
        function bool = satisfiedBy(constraint,actual)
            bool = constraint.sizeMatchesExpected(actual);
        end
        
        % Produce a diagnostic for the constraint
        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)
        % Produce a diagnostic for the negated constraint
        function diag = getNegativeDiagnosticFor(constraint,actual)
            import matlab.automation.diagnostics.StringDiagnostic
            if constraint.sizeMatchesExpected(actual) % Constraint satisfied incorrectly
                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)
        % Determine if the actual and expected values have the same size
        function bool = sizeMatchesExpected(constraint,actual)
            bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));
        end
    end

end