Create Custom Constraint - MATLAB & Simulink (original) (raw)

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

In a file in your current folder, create a class namedIsSameSizeAs 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 IsSameSizeAs < matlab.unittest.constraints.Constraint properties (SetAccess=immutable) ValueWithExpectedSize end

methods
    function constraint = IsSameSizeAs(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 are the same size. This method is invoked by other constraint methods.

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

Classes that derive from thematlab.unittest.constraints.Constraint class must implement 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 returnstrue.

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

Classes that derive from thematlab.unittest.constraints.Constraint class must also implement 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 diagnostic = getDiagnosticFor(constraint,actual)
        import matlab.automation.diagnostics.StringDiagnostic
        if constraint.sizeMatchesExpected(actual)
            diagnostic = StringDiagnostic("IsSameSizeAs passed.");
        else
            diagnostic = StringDiagnostic( ...
                "IsSameSizeAs failed." + newline + "Actual Size: [" ...
                + int2str(size(actual)) + "]" + newline ...
                + "Expected Size: [" ...
                + int2str(size(constraint.ValueWithExpectedSize)) ...
                + "]");
        end
    end
end

IsSameSizeAs Class Definition

This is the complete code for the IsSameSizeAs class.

classdef IsSameSizeAs < matlab.unittest.constraints.Constraint properties (SetAccess=immutable) ValueWithExpectedSize end

methods
    function constraint = IsSameSizeAs(value)
        constraint.ValueWithExpectedSize = value;
    end

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

    function diagnostic = getDiagnosticFor(constraint,actual)
        import matlab.automation.diagnostics.StringDiagnostic
        if constraint.sizeMatchesExpected(actual)
            diagnostic = StringDiagnostic("IsSameSizeAs passed.");
        else
            diagnostic = StringDiagnostic( ...
                "IsSameSizeAs failed." + newline + "Actual Size: [" ...
                + int2str(size(actual)) + "]" + newline ...
                + "Expected Size: [" ...
                + int2str(size(constraint.ValueWithExpectedSize)) ...
                + "]");
        end
    end
end

methods (Access=private)
    function tf = sizeMatchesExpected(constraint,actual)
        tf = 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),IsSameSizeAs(repmat(1,5)))

Test a failing case.

testCase.verifyThat(zeros(5),IsSameSizeAs(ones(1,5)))

Verification failed. --------------------- Framework Diagnostic: --------------------- IsSameSizeAs failed. Actual Size: [5 5] ExpectedSize: [1 5]

See Also

Classes

Topics