Main Content

matlab.unittest.constraints.AnyCellOf Class

Namespace: matlab.unittest.constraints

Test if any element of cell array satisfies constraint

Description

The matlab.unittest.constraints.AnyCellOf class provides a proxy of the actual value, so you can test if at least one element of a cell array satisfies a given constraint. When you include the proxy in your test, the testing framework examines the constraint on an element-by-element basis.

You can use an instance of this class in tests performed with the qualification methods assertThat, assumeThat, fatalAssertThat, or verifyThat. The class does not modify the supplied actual value. It serves only as a wrapper to perform the constraint analysis.

Creation

Description

p = matlab.unittest.constraints.AnyCellOf(actualValue) creates a proxy to test if any element of the specified cell array satisfies a constraint and sets the ActualValue property. When you use this proxy to test against a constraint, the test passes if at least one element of actualValue satisfies the constraint.

example

Properties

expand all

Actual value to test against the constraint, returned as a value of any data type. Specify the value of this property during creation of the proxy. Even though you can specify a value of any data type, the test fails if the actual value is not a nonempty cell array.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Test if at least one element of a cell array satisfies a constraint by using the AnyCellOf class.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.AnyCellOf
import matlab.unittest.constraints.HasElementCount
import matlab.unittest.constraints.IsFinite
import matlab.unittest.constraints.IsLessThan
import matlab.unittest.constraints.Matches
import matlab.unittest.constraints.IsEmpty

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that at least one cell of {42,[11 38],1:5} contains five elements.

testCase.verifyThat(AnyCellOf({42,[11 38],1:5}),HasElementCount(5))
Verification passed.

Verify that at least one element of the cell array {NaN,Inf,5} is finite.

testCase.verifyThat(AnyCellOf({NaN,Inf,5}),IsFinite)
Verification passed.

Verify that at least one element of the cell array {-1,5} is negative.

testCase.verifyThat(AnyCellOf({-1,5}),IsLessThan(0))
Verification passed.

Test if at least one element of the cell array {'Coffee','Tea','Water'} matches "tea" regardless of case. The test passes.

testCase.verifyThat(AnyCellOf({'Coffee','Tea','Water'}), ...
    Matches("tea","IgnoringCase",true))
Verification passed.

Test if at least one element of the cell array {struct([]),''} is not empty. The test fails because both elements are empty.

testCase.verifyThat(AnyCellOf({struct([]),''}),~IsEmpty)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    All cells failed. The first cell failed because:
    --> Negated IsEmpty failed.
        --> The value must not be empty.
        --> The value has a size of [0  0].
        
        Actual Value:
          0×0 empty struct array with no fields.
    
    Actual Value Cell Array:
      1×2 cell array
    
        {0×0 struct}    {0×0 char}

Version History

Introduced in R2013a