Main Content

matlab.unittest.constraints.IsValid Class

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

Test if array elements are valid handles

Since R2023a

Description

The matlab.unittest.constraints.IsValid class provides a constraint to test if all elements of a handle array are valid. A handle is invalid if it has no corresponding object.

Creation

Description

example

c = matlab.unittest.constraints.IsValid creates a constraint to test if a handle array is valid. The constraint is satisfied if all array elements are valid handles.

Examples

collapse all

Test for valid handles using the IsValid constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsValid

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that a value object does not satisfy the IsValid constraint.

testCase.verifyThat(pi,~IsValid)
Verification passed.

Create a figure and test if the handle to the figure is valid. The test passes.

f = figure;
testCase.verifyThat(f,IsValid)
Verification passed.

Delete the figure and test again. The test fails because a handle variable becomes invalid if its corresponding object has been deleted.

delete(f)
testCase.verifyThat(f,IsValid)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsValid failed.
    --> Value must be a valid handle.
    
    Actual Value:
      handle to deleted Figure

Create several handle objects and verify that the corresponding handle array is valid.

p = plot(magic(5));
testCase.verifyThat(p,IsValid)
Verification passed.

Delete one of the handle objects and test again. The test fails because, for the test to pass, all array elements must be valid handles.

delete(p(3))
testCase.verifyThat(p,IsValid)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsValid failed.
    --> All array elements must be valid handles.
        Indices of invalid handles:
            3
    
    Actual Value:
      5×1 Line array:
    
      Line
      Line
      Line
      Line
      Line

Version History

Introduced in R2023a