Main Content

matlab.unittest.constraints.Constraint Class

Namespace: matlab.unittest.constraints

Fundamental interface for constraints

Description

The matlab.unittest.constraints.Constraint class provides an interface that you can use to encode comparison logic in qualifications and produce diagnostic information. All constraints are derived from the Constraint class, whether they are user-supplied constraints or framework constraints.

To create a custom constraint class, derive your class from matlab.unittest.constraints.Constraint and implement its abstract methods:

  • Implement the satisfiedBy method to encode the comparison logic.

  • Implement the getDiagnosticFor method to produce diagnostic information when the testing framework evaluates the actual value against the constraint.

Then, you can use your Constraint subclass with the assertThat, assumeThat, fatalAssertThat, and verifyThat qualification methods from the matlab.unittest.qualifications namespace.

To create a constraint that can be combined and negated using the and (&), or (|), and not (~) operators, derive your class from matlab.unittest.constraints.BooleanConstraint instead.

Class Attributes

Abstract
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Methods

expand all

Examples

collapse all

Create a constraint that determines if a value is the same size as an expected value.

In a file in your current folder, create a class named IsSameSizeAs that derives from matlab.unittest.constraints.Constraint, and implement the satisfiedBy and getDiagnosticFor methods.

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

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

        % Determine if the actual value satisfies the constraint 
        function tf = satisfiedBy(constraint,actual)
            tf = constraint.sizeMatchesExpected(actual);
        end

        % Produce a diagnostic for the constraint
        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)
        % Determine if the actual and expected values are the same size
        function tf = sizeMatchesExpected(constraint,actual)
            tf = isequal(size(actual), ...
                size(constraint.ValueWithExpectedSize));
        end
    end
end

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Verify that a 5-by-5 matrix of zeros is the same size as a 5-by-5 matrix of ones by using the IsSameSizeAs constraint.

testCase.verifyThat(zeros(5),IsSameSizeAs(ones(5)))
Verification passed.

Version History

Introduced in R2013a