Main Content

getNegativeDiagnosticFor

Class: matlab.unittest.constraints.BooleanConstraint
Package: matlab.unittest.constraints

Produce negated diagnostic for value

Description

example

diag = getNegativeDiagnosticFor(constObj,actVal) analyzes the provided value actVal against the constraint constObj and produces a matlab.automation.diagnostics.Diagnostic object diag that corresponds to the negation of constObj. This method is a protected method.

The diagnostics that this method produces are expressed in the negative sense of the constraint. For example, consider a hypothetical IsTasty constraint. When the negation of IsTasty is used in a qualification, the test fails if the actual value is found to be "tasty". Therefore, getNegativeDiagnosticFor should return the details that describe why the value incorrectly satisfied the constraint.

Like the getDiagnosticFor method of Constraint, the getNegativeDiagnosticFor method is typically called for qualification failures. Therefore, providing more detailed analysis after a failure can be more efficiently handled by getNegativeDiagnosticFor than the satisfiedBy method.

Input Arguments

constObj

BooleanConstraint instance

actVal

Value for comparison

Examples

expand all

Create a custom Boolean constraint that determines if a given value has the same size as an expected value. Implement the getNegativeDiagnosticFor method to provide a Diagnostic object when the constraint is negated.

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