satisfiedBy

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

Determine whether value satisfies constraint

Description

example

TF = satisfiedBy(constObj,actVal) determines whether a value, actVal, satisfies a constraint, constObj. The satisfiedBy method is used to determine qualification success or failure. It returns true or false (logical 0 or 1). When creating a custom constraint, you must place the comparison logic in this method.

Since the most common usage is for the passing case, you should optimize for speed in that case. It is only in the failing case that more expensive detailed analysis is helpful.

Input Arguments

actVal

Value to evaluate against the constraint

constObj

Constraint instance

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 satisfiedFor method.

classdef HasSameSizeAs < matlab.unittest.constraints.Constraint
    
    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.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)
        function bool = sizeMatchesExpected(constraint,actual)
            bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));
        end
    end
    
end