Main Content

matlab.unittest.constraints.ReturnsTrue Class

Namespace: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.Constraint

Test if function returns true

Description

The matlab.unittest.constraints.ReturnsTrue class provides a constraint to test if a function returns true.

The matlab.unittest.constraints.ReturnsTrue class is a handle class.

Creation

Description

c = matlab.unittest.constraints.ReturnsTrue creates a constraint to test if a function returns true. The constraint is satisfied by a function handle that returns a logical scalar value of 1 (true).

example

Examples

collapse all

Test functions using the ReturnsTrue constraint.

First, import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Test if the true function satisfies the ReturnsTrue constraint. The test passes.

testCase.verifyThat(@true,ReturnsTrue)
Verification passed.

Test the false function. The test fails.

testCase.verifyThat(@false,ReturnsTrue)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    ReturnsTrue failed.
    --> The function handle did not evaluate to "true".
    --> Returned value:
              logical
            
               0
    
    Evaluated Function:
      function_handle with value:
    
        @false

Test if a call to isequal returns true given two equivalent numeric values. The test passes.

testCase.verifyThat(@() isequal(1,single(1)),ReturnsTrue)
Verification passed.

Verify that it is true that two different letters are not the same.

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

Test a function that returns a vector of true values. The test fails because the returned value is nonscalar.

testCase.verifyThat(@() strcmp('a',{'a','a'}),ReturnsTrue)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    ReturnsTrue failed.
    --> The function handle did not return a scalar. The return value had a size of [1  2].
    --> Returned value:
              1×2 logical array
            
               1   1
    
    Evaluated Function:
      function_handle with value:
    
        @()strcmp('a',{'a','a'})

Test a function that returns a numeric value. The test fails.

testCase.verifyThat(@ones,ReturnsTrue)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    ReturnsTrue failed.
    --> The function handle did not return a logical value. The return value was of type "double".
    --> Returned value:
                 1
    
    Evaluated Function:
      function_handle with value:
    
        @ones

Tips

  • An alternative to ReturnsTrue is the IsTrue constraint. IsTrue runs faster and is easier to use, but ReturnsTrue provides slightly better diagnostic information. In this example, both tests fail, but the second test displays the function handle as part of the diagnostics.

    import matlab.unittest.TestCase
    import matlab.unittest.constraints.IsTrue
    import matlab.unittest.constraints.ReturnsTrue
    
    testCase = TestCase.forInteractiveUse;
    actual = 1;
    expected = 2;
    testCase.verifyThat(isequal(actual,expected),IsTrue)
    testCase.verifyThat(@() isequal(actual,expected),ReturnsTrue)

Version History

Introduced in R2013a

expand all