matlab.unittest.constraints.Constraint - Fundamental interface for constraints - MATLAB (original) (raw)
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 frommatlab.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 theassertThat
, 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
(|
), andnot
(~
) 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
satisfiedBy | tf = satisfiedBy(constraint,actual)Determine if the value satisfies the constraint. This method provides the comparison logic.Input Arguments constraint — Constraint, specified as amatlab.unittest.constraints.Constraint object.actual — Value to test.Output Arguments tf — Whether the value satisfies the constraint, returned as a 1 or 0 of data typelogical.Abstract:true |
---|---|
getDiagnosticFor | diagnostic = getDiagnosticFor(constraint,actual)Produce diagnostic information for the value being evaluated against the constraint. Typically, the testing framework calls this method when it encounters a qualification failure. Therefore, a more detailed analysis after a failure can be more efficiently handled by getDiagnosticFor than thesatisfiedBy method.Input Arguments constraint — Constraint, specified as amatlab.unittest.constraints.Constraint object.actual — Value to test.Output Arguments diagnostic — Diagnostic information to display, returned as a matlab.automation.diagnostics.Diagnostic object.Depending on the test runner configuration, the testing framework can display diagnostics when the qualification passes or fails. By default, the framework displays diagnostics only when the qualification fails. You can override the default behavior by customizing the test runner. For example, use a DiagnosticsOutputPlugin instance to display both failing and passing event diagnostics.Abstract:true |
Examples
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)))
Version History
Introduced in R2013a