Main Content

matlab.unittest.constraints.IsInstanceOf Class

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

Test if value is instance of specified class

Description

The matlab.unittest.constraints.IsInstanceOf class provides a constraint to test if a value is an instance of a specified class.

The IsInstanceOf constraint tests for inclusion in a class hierarchy. To test for an exact class match, use the IsOfClass constraint.

Creation

Description

example

c = matlab.unittest.constraints.IsInstanceOf(class) creates a constraint to test if a value is an instance of class and sets the Class property. The constraint is satisfied if the value is an instance of class or if it derives from class.

Properties

expand all

Expected class, returned as a character vector. Specify the value of this property during creation of the constraint as a string scalar, character vector, or matlab.metadata.Class instance.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Test a numeric value using the IsInstanceOf constraint.

First, import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that the value 1 is an instance of the class double.

testCase.verifyThat(1,IsInstanceOf("double"))
Verification passed.

Repeat the test using a matlab.metadata.Class instance instead of a string.

testCase.verifyThat(1,IsInstanceOf(?double))
Verification passed.

Verify that a numeric value is not an instance of the class logical.

testCase.verifyThat(1,~IsInstanceOf("logical"))
Verification passed.

Test a function handle using the IsInstanceOf constraint.

First, import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that @sin is a function handle.

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

Repeat the test using the function name "sin". The test fails.

testCase.verifyThat("sin",IsInstanceOf(?function_handle))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsInstanceOf failed.
    --> The value must be an instance of the expected type.
        
        Actual Class:
            string
        Expected Type:
            function_handle
    
    Actual Value:
        "sin"

Test an instance of a derived class using the IsInstanceOf constraint.

In a file in your current folder, create the ExampleHandle handle class.

classdef ExampleHandle < handle
    properties
        Number = 1;
    end
end

Import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Test an instance of the ExampleHandle class using the IsInstanceOf constraint. The test passes.

actual = ExampleHandle;
testCase.verifyThat(actual,IsInstanceOf(?ExampleHandle))
Verification passed.

Test if actual also is an instance of the handle class. The test passes because ExampleHandle derives from the handle class.

testCase.verifyThat(actual,IsInstanceOf(?handle))
Verification passed.

Version History

Introduced in R2013a