Main Content

matlab.unittest.constraints.IsScalar Class

Namespace: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Test if value is scalar

Description

The matlab.unittest.constraints.IsScalar class provides a constraint to test if a value is a scalar.

Creation

Description

example

c = matlab.unittest.constraints.IsScalar creates a constraint to test if a value is a scalar. The constraint is satisfied by a 1-by-1 array.

Examples

collapse all

Test values using the IsScalar constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsScalar

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that the value 1 satisfies the IsScalar constraint.

testCase.verifyThat(1,IsScalar)
Verification passed.

Test if the vector [1 1 2 3 5 8 13] satisfies the constraint. The test fails.

testCase.verifyThat([1 1 2 3 5 8 13],IsScalar)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsScalar failed.
    --> The value must be a scalar.
    --> The value has a size of [1  7].
    
    Actual Value:
         1     1     2     3     5     8    13

Test the value "Hello, World!". The test passes because the value is a string scalar.

testCase.verifyThat("Hello, World!",IsScalar)
Verification passed.

Test an empty structure. The test fails because the value is not a 1-by-1 array.

testCase.verifyThat(struct([]),IsScalar)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsScalar failed.
    --> The value must be a scalar.
    --> The value has a size of [0  0].
    
    Actual Value:
      0×0 empty struct array with no fields.

Version History

Introduced in R2014b