satisfiedBy

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

Determine whether value satisfies constraint

Description

example

TF = satisfiedBy(constObj,actVal) determines whether actVal satisfies the constraint constObj. The method returns true (logical 1) when the constraint is satisfied and false (logical 0) when the constraint is not satisfied. When creating a custom constraint, you must place the comparison logic in this method.

The satisfiedBy method is more commonly used to test for the passing case. Therefore, providing more detailed analysis after a failure can be more efficiently handled by other methods such as the getDiagnosticFor method.

Input Arguments

constObj

Constraint instance

actVal

Value to evaluate against the constraint

Examples

expand all

Create a custom constraint that determines if a given value has the same size as an expected value. Include the comparison logic in the satisfiedBy method.

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