Main Content

matlab.unittest.constraints.IsSameHandleAs Class

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

Test if two handle arrays are the same

Description

The matlab.unittest.constraints.IsSameHandleAs class provides a constraint to test if a handle array is the same as another handle array. Two handle arrays are the same if they are the same size and their corresponding elements point to the same handle object.

Creation

Description

c = matlab.unittest.constraints.IsSameHandleAs(expectedHandle) creates a constraint to test if a handle array is the same as the expected handle array and sets the ExpectedHandle property. The constraint is satisfied if the handle array and expectedHandle are the same size and their corresponding elements point to the same handle object.

example

Properties

expand all

Expected value, returned as a handle array. Specify the value of this property during creation of the constraint.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Compare handle arrays using the IsSameHandleAs 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.IsSameHandleAs

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Create two ExampleHandle objects assigned to the variables h1 and h2. Then, assign the value of h2 to another variable, h3. The variables h1 and h2 point to different objects, but the variables h2 and h3 point to the same object.

h1 = ExampleHandle;
h2 = ExampleHandle;
h3 = h2;

Test if h1 and h2 point to the same object. The test fails.

testCase.verifyThat(h1,IsSameHandleAs(h2))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSameHandleAs failed.
    --> Values do not refer to the same handle.
    
    Actual Value:
      ExampleHandle with properties:
    
        Number: 1
    Expected Handle Object:
      ExampleHandle with properties:
    
        Number: 1

Verify that h2 and h3 point to the same object.

testCase.verifyThat(h2,IsSameHandleAs(h3))
Verification passed.

Test if [h2 h3] and [h3 h2] are the same. The test passes because the corresponding vector elements point to the same object.

testCase.verifyThat([h2 h3],IsSameHandleAs([h3 h2]))
Verification passed.

Test if [h1 h2] and [h2 h1] are the same. The test fails because the corresponding vector elements point to different objects.

testCase.verifyThat([h1 h2],IsSameHandleAs([h2 h1]))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSameHandleAs failed.
    --> Some elements in the handle array refer to the wrong handle.
    
    Actual Value:
      1×2 ExampleHandle array with properties:
    
        Number
    Expected Handle Object:
      1×2 ExampleHandle array with properties:
    
        Number

Verify that two handle arrays of different shapes are not the same.

testCase.verifyThat([h1 h1 h2 h3],~IsSameHandleAs([h1 h1; h2 h3]))
Verification passed.

Version History

Introduced in R2013a

expand all