Main Content

matlab.unittest.constraints.EveryCellOf class

Package: matlab.unittest.constraints

Test if all elements of cell array meet constraint

Description

The EveryCellOf class creates a proxy of the actual value to the framework. The proxy enables a test writer to apply a constraint against each element of a cell array, which ensures that a passing result occurs when every element of the cell array satisfies the constraint.

It is intended that you use this class through matlab.unittest qualifications as shown in the examples. The class does not modify the provided actual value, but serves as a wrapper to perform the constraint analysis. The testing framework analyzes the constraint on an element-by-element basis.

Construction

EveryCellOf(actVal) creates a proxy instance that tests if every element of a provided cell array, actVal, meets a constraint. The test passes if all elements satisfy the constraint.

Tips

  • EveryCellOf checks if every element in the provided cell array satisfies an associated constraint. However, there are some constraints, a prominent one being IsEqualTo, that natively validate if all elements in cell arrays satisfy a condition. In these situations, use of EveryCellOf is unnecessary and impedes qualification performance.

Input Arguments

actVal

Actual value to test against constraint

Properties

ActualValue

Actual value to test against constraint. Set this property through the constructor via the actVal input argument.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.EveryCellOf

testCase = TestCase.forInteractiveUse;

Test that every cell of actVal contains the substring 'ain'.

import matlab.unittest.constraints.ContainsSubstring
actVal = {'Rain','Main','Plain'};
testCase.verifyThat(EveryCellOf(actVal), ContainsSubstring('ain'))
Interactive verification passed.

Test that every cell of the actual value array has two elements.

import matlab.unittest.constraints.HasElementCount
testCase.verifyThat(EveryCellOf({{'hello','world'}, {11 38}}), HasElementCount(2))
Interactive verification passed.

Test that every cell of the actual value array is empty.

import matlab.unittest.constraints.IsEmpty
testCase.verifyThat(EveryCellOf({inputParser.empty,''}), IsEmpty)
Interactive verification passed.

Test that every cell of the actual value array is finite.

import matlab.unittest.constraints.IsFinite
testCase.verifyThat(EveryCellOf({NaN, Inf, 5}), IsFinite)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
At least one cell failed.

Failing indices:
         1     2
The first failing cell failed because:
--> IsFinite failed.
    --> The value must be finite.
    
    Actual Value:
           NaN

Actual Value Cell Array:
        [NaN]    [Inf]    [5]

Only the third element has a finite value.

Test that every cell of the actual value array is real.

import matlab.unittest.constraints.IsReal
testCase.verifyThat(EveryCellOf({1 4i}), IsReal)
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
At least one cell failed.

Failing indices:
         2
The first failing cell failed because:
--> IsReal failed.
    --> The value must be real.
    
    Actual Value:
          0.000000000000000 + 4.000000000000000i

Actual Value Cell Array:
        [1]    [0.000000000000000 + 4.000000000000000i]

The second element has an imaginary value.