matlab.unittest.constraints.IsOfClass class

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

Constraint specifying class type

Construction

IsOfClass(class) provides a constraint specifying the class type. The constraint is satisfied if the actual value is the same class as class . The constraint is not satisfied if the actual value derives from class.

Input Arguments

class

Class name that must be matched by the actual value to satisfy the constraint, specified as a fully qualified class name string or a meta.class instance.

Properties

Class

Class name that must be matched by the actual value to satisfy the constraint. Set this property through the constructor via the class input argument.

Copy Semantics

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

Examples

expand all

Test That Actual Value Class Is Specified Class

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

testCase = TestCase.forInteractiveUse;

Verify that the actual value, 5, is a double.

testCase.verifyThat(5, IsOfClass('double'))
Interactive verification passed.

Repeat the test using an instance of meta.class instead of a string.

testCase.verifyThat(5, IsOfClass(?double))
Interactive verification passed.

Assert that zero is an instance of the logical class.

testCase.assertThat(0, IsOfClass('logical'))
Interactive assertion failed.

---------------------
Framework Diagnostic:
---------------------
IsOfClass failed.
--> The value's class is incorrect.
    
    Actual Class:
        double
    Expected Class:
        logical

Actual Value:
         0
Assertion failed.

Verify that @sin is a function handle.

testCase.verifyThat(@sin, IsOfClass(?function_handle))
Interactive verification passed.

Verify that name is an instance of the char class.

name = 42;
testCase.verifyThat(name, IsOfClass('char'))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
IsOfClass failed.
--> The value's class is incorrect.
    
    Actual Class:
        double
    Expected Class:
        char

Actual Value:
        42

Test That Derived Class Is Instance of Specified Class

In a file in your working folder, create a class, DerivedExample, that inherits from the handle class.

classdef DerivedExample < handle
end

At the command prompt, create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

testCase = TestCase.forInteractiveUse

Verify that an instance of the DerivedExample class is an instance of a handle.

exObj = DerivedExample;
testCase.verifyThat(exObj, IsOfClass(?handle))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
IsOfClass failed.
--> The value's class is incorrect.
    
    Actual Class:
        DerivedExample
    Expected Class:
        handle

Actual Value:
      DerivedExample with no properties.

Even though exObj derives from the handle class, it is not an instance of the handle class.

Verify that an instance of the DerivedExample class is an instance of a DerivedExample.

testCase.verifyThat(exObj, IsOfClass(?DerivedExample))
Interactive verification passed.

See Also

|

Was this topic helpful?