matlab.unittest.qualifications.Verifiable.verifySameHandle - Verify two handle arrays are the same - MATLAB (original) (raw)

Class: matlab.unittest.qualifications.Verifiable
Namespace: matlab.unittest.qualifications

Verify two handle arrays are the same

Syntax

Description

verifySameHandle([testCase](#bt00rcb-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5Fe91e2e13-ef02-4d27-a829-4d9749a8e2c0),[expected](#mw%5F320c8253-a988-4b1f-87c1-59cfa994b2cb)) verifies that actual is the same as the expected handle array. Two handle arrays are the same if they have the same size and their corresponding elements refer to the same handle object.

example

verifySameHandle([testCase](#bt00rcb-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5Fe91e2e13-ef02-4d27-a829-4d9749a8e2c0),[expected](#mw%5F320c8253-a988-4b1f-87c1-59cfa994b2cb),[diagnostic](#mw%5F490eddb6-8ea9-49d6-9d34-d12cd04f17ed)) also associates the diagnostic information in diagnostic with the qualification.

example

Input Arguments

expand all

Test case, specified as a matlab.unittest.qualifications.Verifiable object. Because the matlab.unittest.TestCase class subclasses matlab.unittest.qualifications.Verifiable and inherits its methods, testCase is typically amatlab.unittest.TestCase object.

Value to test, specified as a value of any data type. Although you can provide a value of any data type, the test fails if actual is not a handle array.

Expected value, specified as a handle array.

Diagnostic information to display when the qualification passes or fails, specified as a string array, character array, function handle, or array of matlab.automation.diagnostics.Diagnostic objects.

Depending on the test runner configuration, the testing framework can display diagnostics when the qualification passes or fails. By default, the framework displays diagnostics only when the qualification fails. You can override the default behavior by customizing the test runner. For example, use a DiagnosticsOutputPlugin instance to display both failing and passing event diagnostics.

Example: "My Custom Diagnostic"

Example: @dir

Examples

expand all

Test if the actual value is the same as the specified handle array.

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

classdef ExampleHandle < handle properties Number = 1; end end

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;

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

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

verifySameHandle(testCase,h1,h2,"Values must point to the same object.")

Verification failed. ---------------- Test Diagnostic: ---------------- Values must point to the same object. --------------------- Framework Diagnostic: --------------------- verifySameHandle failed. --> Values do not refer to the same handle.

Actual Value:
  ExampleHandle with properties:

    Number: 1
Expected Handle Object:
  ExampleHandle with properties:

    Number: 1
------------------
Stack Information:
------------------
In C:\work\TestHandlesForEqualityExample.m (TestHandlesForEqualityExample) at 29

Verify that h2 and h3 point to the same object.

verifySameHandle(testCase,h2,h3)

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

verifySameHandle(testCase,[h2 h3],[h3 h2])

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

verifySameHandle(testCase,[h1 h2],[h2 h1])

Verification failed. --------------------- Framework Diagnostic: --------------------- verifySameHandle 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
------------------
Stack Information:
------------------
In C:\work\TestHandlesForEqualityExample.m (TestHandlesForEqualityExample) at 43

Test if two handle arrays of different shapes are the same. The test fails.

verifySameHandle(testCase,[h1 h1 h2 h3],[h1 h1; h2 h3])

Verification failed. --------------------- Framework Diagnostic: --------------------- verifySameHandle failed. --> Sizes do not match. Actual Value Size : [1 4] Expected Handle Object Size : [2 2]

Actual Value:
  1×4 ExampleHandle array with properties:

    Number
Expected Handle Object:
  2×2 ExampleHandle array with properties:

    Number
------------------
Stack Information:
------------------
In C:\work\TestHandlesForEqualityExample.m (TestHandlesForEqualityExample) at 48

Tips

Version History

Introduced in R2013a

expand all

In qualifications, character vectors are no longer equivalent to enumerations of a handle class. For example, consider this enumeration class.

classdef MyClass < handle enumeration X Y end end

This test fails because 'X' does not represent the enumerationMyClass.X. In previous releases, the test passes.

testCase = matlab.unittest.TestCase.forInteractiveUse; testCase.verifySameHandle('X',MyClass.X)

Verification failed. --------------------- Framework Diagnostic: --------------------- verifySameHandle failed. --> Values do not refer to the same handle. --> Value must be a handle object. It is of class "char". --> Classes do not match. Actual Value class : [char] Expected Handle Object class : [MyClass]

Actual char:
    X
Expected Handle Object:
  MyClass enumeration

    X