Main Content

matlab.unittest.constraints.IsOfClass Class

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

Test if class of value is specified class

Description

The matlab.unittest.constraints.IsOfClass class provides a constraint to test if the class of a value is a specified class.

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

Creation

Description

c = matlab.unittest.constraints.IsOfClass(class) creates a constraint to test if the class of a value is class and sets the Class property. The constraint is satisfied if the class of the value is class. It is not satisfied if the value derives from class.

example

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 numeric values using the IsOfClass constraint.

First, import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that the class of the numeric value 5 is double.

testCase.verifyThat(5,IsOfClass("double"))
Verification passed.

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

testCase.verifyThat(5,IsOfClass(?double))
Verification passed.

Verify that zero is not a logical value.

testCase.verifyThat(0,~IsOfClass("logical"))
Verification passed.

Test a function handle using the IsOfClass constraint.

First, import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that @sin is a function handle.

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

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

testCase.verifyThat("sin",IsOfClass(?function_handle))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsOfClass failed.
    --> The value's class is incorrect.
        
        Actual Class:
            string
        Expected Class:
            function_handle
    
    Actual Value:
        "sin"

Test an instance of a derived class using the IsOfClass 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.IsOfClass

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Test an ExampleHandle object using the IsOfClass constraint. The test passes.

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

Repeat the test using the handle class. Even though actual derives from the handle class, the test fails because handle is not the exact class of actual.

testCase.verifyThat(actual,IsOfClass(?handle))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsOfClass failed.
    --> The value's class is incorrect.
        
        Actual Class:
            ExampleHandle
        Expected Class:
            handle
    
    Actual Value:
      ExampleHandle with properties:
    
        Number: 1

Test the output of a function using the IsOfClass constraint.

In a file in your current folder, create the add5 function. The function accepts a numeric input and increments it by five.

function y = add5(x)
% add5 - Increment input by 5
if ~isa(x,"numeric")
    error("add5:InputMustBeNumeric","Input must be numeric.")
end
y = x + 5;
end

Import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Call the function with a valid input. Then, test if the class of the returned value is double. The test passes.

actual = add5(1);
testCase.verifyThat(actual,IsOfClass(?double))
Verification passed.

Version History

Introduced in R2013a