Main Content

Create Custom Constraint

This example shows how to create a custom constraint that determines if a given value has the same size as an expected value.

In a file in your current folder, create a class named HasSameSizeAs that derives from the matlab.unittest.constraints.Constraint class. The class constructor accepts an expected value whose size is compared to the size of an actual value. The expected value is stored in the ValueWithExpectedSize property. The recommended practice is to make Constraint implementations immutable, so set the property SetAccess attribute to immutable.

classdef HasSameSizeAs < matlab.unittest.constraints.Constraint
    
    properties(SetAccess = immutable)
        ValueWithExpectedSize
    end
    
    methods
        function constraint = HasSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end
    end
    
end

In a methods block with private access, define a helper method sizeMatchesExpected that determines if the actual and expected values have the same size. This method is invoked by other constraint methods.

methods(Access = private)
    function bool = sizeMatchesExpected(constraint,actual)
        bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));
    end
end

Classes that derive from the matlab.unittest.constraints.Constraint class must override the satisfiedBy method. This method must contain the comparison logic and return a logical value. Within a methods block, implement satisfiedBy by invoking the helper method. If the actual size and the expected size are equal, the method returns true.

methods
    function bool = satisfiedBy(constraint,actual)
        bool = constraint.sizeMatchesExpected(actual);
    end
end

Classes that derive from the matlab.unittest.constraints.Constraint class must also override the getDiagnosticFor method. This method must evaluate the actual value against the constraint and provide a Diagnostic object. In this example, getDiagnosticFor returns a StringDiagnostic object.

methods        
    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

HasSameSizeAs Class Definition

This is the complete code for the HasSameSizeAs class.

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

Test for Expected Size

At the command prompt, create a test case for interactive testing.

import matlab.unittest.TestCase
testCase = TestCase.forInteractiveUse;

Test a passing case.

testCase.verifyThat(zeros(5),HasSameSizeAs(repmat(1,5)))
Verification passed.

Test a failing case.

testCase.verifyThat(zeros(5),HasSameSizeAs(ones(1,5)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    HasSameSizeAs failed.
    Actual Size: [5  5]
    ExpectedSize: [1  5]

See Also

| | | |

Related Topics