Main Content

matlab.unittest.constraints.IsInstanceOf class

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

Constraint specifying inclusion in given class hierarchy

Construction

IsInstanceOf(class) provides a constraint specifying inclusion in a given class hierarchy. The constraint is satisfied if the actual value instance passes the “isa” relationship with class .

Input Arguments

class

Class name that the actual value must derive from or be an instance of to satisfy the constraint, specified as a fully qualified class name represented by a character vector or a meta.class instance.

Properties

Class

Class name that the actual value must derive from or be an instance of 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.

Examples

collapse all

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsInstanceOf

testCase = TestCase.forInteractiveUse;

Verify that the actual value, 5, is an instance of the double class.

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

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

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

Assert that zero is an instance of the logical class.

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

---------------------
Framework Diagnostic:
---------------------
IsInstanceOf failed.
--> The value must be an instance of the expected type.
    
    Actual Class:
        double
    Expected Type:
        logical

Actual Value:
         0
Assertion failed.

Verify that @sin is a function handle.

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

Verify that name is an instance of the char class.

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

---------------------
Framework Diagnostic:
---------------------
IsInstanceOf failed.
--> The value must be an instance of the expected type.
    
    Actual Class:
        double
    Expected Type:
        char

Actual Value:
        42

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.IsInstanceOf

testCase = TestCase.forInteractiveUse;

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

exObj = DerivedExample;
testCase.verifyThat(exObj, IsInstanceOf(?handle))
Interactive verification passed.

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

See Also

|