This example shows how to create a custom boolean 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
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.
matlab.unittest.constraints.BooleanConstraint