matlab.unittest.constraints.ReturnsTrue class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Constraint specifying function handle that returns true

Construction

ReturnsTrue provides a constraint specifying that a function handle that returns true. The constraint is satisfied only by a function handle that returns a scalar logical with a value of true.

When negated using the tilde operator, ~, this constraint not only passes when the function handle returns false, but also when the function handle returns any non-scalar value (such as [true true]) or any non-logical (such as integer valued1).

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects in the MATLAB® documentation.

Tips

  • To display custom comparisons in the form of a function handle, use ReturnsTrue instead of IsTrue.

Examples

expand all

Test Actual Value Specified by Function Handle Returns True

These comparisons are shown for example only. There are other constraints that might better handle the particular comparisons.

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.ReturnsTrue

testCase = TestCase.forInteractiveUse;

Verify that the ReturnsTrue constraint is satisfied by the value returned by a handle to true.

testCase.verifyThat(@true, ReturnsTrue)
Interactive verification passed.

Verify that the ReturnsTrue constraint is not satisfied by a handle to false.

testCase.verifyThat(@false, ReturnsTrue)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
ReturnsTrue failed.
--> The function handle should have evaluated to "true".
--> Returned value:
             0

Actual Function Handle:
        @false

Verify that a call to isequal returns true.

testCase.verifyThat(@() isequal(1,1), ReturnsTrue)
Interactive verification passed.

Verify that a function that returns a double-valued 1 does not satisfy the ReturnsTrue constraint.

testCase.verifyThat(@() double(true), ReturnsTrue)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
ReturnsTrue failed.
--> The function handle should have returned a logical value. It was of type "double".
--> Returned value:
             1

Actual Function Handle:
        @()double(true)

Verify that the negation of a string comparison of 'a' and 'b' returns true.

testCase.verifyThat(@() ~strcmp('a','b'), ReturnsTrue)
Interactive verification passed.

Test if a comparison of 'a' to the cell array {'a','a'} returns true.

testCase.verifyThat(@() strcmp('a',{'a','a'}), ReturnsTrue)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
ReturnsTrue failed.
--> The function handle should have returned a scalar. The return value had a size of [1  2].
--> Returned value:
             1     1

Actual Function Handle:
        @()strcmp('a',{'a','a'})

The constraint is not satisfied because the call to strcmp results a logical array, not a logical scalar.

See Also

|

Was this topic helpful?