Main Content

getDiagnosticFor

Class: matlab.unittest.constraints.Constraint
Package: matlab.unittest.constraints

Produce diagnostic for compared value

Description

example

diag = getDiagnosticFor(constObj,actVal) produces the Diagnostic object diag for the constraint constObj and the value actVal. When creating a custom constraint, you must implement the getDiagnosticFor method to analyze the value actVal against the constraint constObj and return a matlab.unittest.diagnostics.Diagnostic object.

Typically, the testing framework calls this method when it encounters a qualification failure. Therefore, providing more detailed analysis after a failure can be more efficiently handled by getDiagnosticFor than the satisfiedBy method.

Input Arguments

constObj

Constraint instance

actVal

Value for comparison

Examples

expand all

Create a custom constraint that determines if a given value has the same size as an expected value. Implement the getDiagnosticFor method to evaluate the actual value against the constraint and provide a Diagnostic object.

classdef HasSameSizeAs < matlab.unittest.constraints.Constraint
    
    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.unittest.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 = 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