matlab.unittest.constraints.BooleanConstraint class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.Constraint

Interface class for boolean combinations of constraints

Description

The BooleanConstraint interface class provides an interface for boolean combinations of Constraints. Any constraint that derives from BooleanConstraint can be combined and negated using the and (&), or (|), and not (~) operators.

Classes that derive from the BooleanConstraint interface class must implement everything required by the standard Constraint interface. When a given constraint is negated, the diagnostics must be written in a different form than for a standard (non-negated) failure. Therefore, classes deriving from the BooleanConstraint class must implement a method to provide a Diagnostic object for the negated case, in addition to the non-negated case.

In exchange for meeting these requirements, all BooleanConstraint implementations inherit the appropriate MATLAB® overloads for and, or, and not so that they can be combined with other BooleanConstraint objects or negated.

Methods

andForm logical conjunction of constraints
getNegativeDiagnosticForProduce negated diagnostic for value
notForm logical negation of constraint
orForm logical disjunction of constraints

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects in the MATLAB documentation.

Examples

expand all

Create Boolean HasSameSizeAs Constraint

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

In a file in your working folder, create a file HasSameSizeAs.m. The constructor accepts a value to compare to the actual size. This value is stored within the ValueWithExpectedSize property. It is recommended that BooleanConstraint implementations be immutable, so set the property SetAccess=immutable.

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

Include these methods in the methods block in HasSameSizeAs.m. Since the BooleanConstraint class is a subclass of Constraint, classes that derive from it must implement the satisfiedBy and getDiagnosticFor methods. For more information about these methods, see matlab.unittest.constraints.Constraint.

    methods
        function bool = satisfiedBy(constraint, actual)
            bool = isequal(size(actual), size(constraint.ValueWithExpectedSize));
        end
        function diag = getDiagnosticFor(constraint, actual)
            import matlab.unittest.diagnostics.StringDiagnostic
            
            if constraint.satisfiedBy(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

Include the getNegativeDiagnosticFor method in the a methods block with protected access in HasSameSizeAs.m. Classes that derive from BooleanConstraint must implement the getNegativeDiagnosticFor method. This method must provide a Diagnostic object that is expressed in the negative sense of the constraint.

    methods(Access=protected)
        function diag = getNegativeDiagnosticFor(constraint, actual)
            import matlab.unittest.diagnostics.StringDiagnostic
            
            if constraint.satisfiedBy(actual)
                diag = StringDiagnostic(sprintf(...
                    ['Negated HasSameSizeAs failed.\nSize [%s] of ' ...
                    'Actual Value and Expected Value were the same ' ...
                    'but should not have been.'], int2str(size(actual))));
            else
                diag = StringDiagnostic('Negated HasSameSizeAs passed.');
            end 
        end 
    end

In exchange for implementing the required methods, the constraint inherits the appropriate and, or, and not overloads so it can be combined with other BooleanConstraint objects or negated.

 HasSameSizeAs Class Definition Summary

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasLength

testCase = TestCase.forInteractiveUse;

Test a passing case.

testCase.verifyThat(zeros(5), HasLength(5) | ~HasSameSizeAs(repmat(1,5)))
Interactive verification passed.

The test passes because one of the or conditions, HasLength(5), is true.

Test a failing case.

testCase.verifyThat(zeros(5), HasLength(5) & ~HasSameSizeAs(repmat(1,5)))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
AndConstraint failed.
--> + [First Condition]:
     |   HasLength passed.
--> AND
    + [Second Condition]:
     |   Negated HasSameSizeAs failed.
     |   Size [5  5] of Actual Value and Expected Value were the same but should not have been.
    -+---------------------

The test fails because one of the and conditions, ~HasSameSizeAs(repmat(1,5)), is false.

Was this topic helpful?