matlab.unittest.constraints.CellComparator class

Package: matlab.unittest.constraints

Comparator for cell arrays

Description

The CellComparator compares cell arrays.

Construction

CellComparator creates a comparator for cell arrays.

CellComparator(compObj) indicates a comparator, compObj, that defines the comparator used to compare values contained in the cell array. By default, a cell comparator only supports empty cell arrays.

CellComparator(___,Name,Value) provides a comparator with additional options specified by one or more Name,Value pair arguments. Name must appear inside single quotes (''). You can specify several name-value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Input Arguments

compObj

Comparator object

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'IgnoringCase'

Indicator whether the comparator is insensitive to case, specified as false or true (logical 0 or 1). When it is false, the comparator is sensitive to case. The comparator only utilizes this name-value pair if the contents being compared are strings.

Default: false

'IgnoringWhitespace'

Indicator whether the comparator is insensitive to whitespace, specified as false or true (logical 0 or 1). When it is false, the comparator is sensitive to whitespace. Whitespace characters are space, form feed, new line, carriage return, horizontal tab, and vertical tab. The comparator only utilizes this name-value pair if the contents being compared are strings.

Default: false

'Recursively'

Indicator of whether comparator operates recursively, specified as false or true (logical 0 or 1). When this value is false, the comparator does not operate recursively on its data.

When the value is true, the data types the cell comparator supports are fully supported in recursion. For example:

comp1 = CellComparator(StringComparator)
comp2 = CellComparator(StringComparator,'Recursively', true)

Both comp1 and comp2 support a cell arrays of strings. However, only comp2 supports cell arrays that recursively contain either cell arrays or strings as their elements.

Default: false

'Within'

Tolerance to use for numerical comparison, specified as a matlab.unittest.constraints.Tolerance object. The comparator only utilizes this name-value pair if the contents being compared are a numeric type.

Default: (empty)

Properties

IgnoreCase

Indicator whether the comparator is insensitive to case, specified in the name-value pair argument, 'IgnoringCase'.

IgnoreWhitespace

Indicator whether the comparator is insensitive to whitespace, specified in the name-value pair argument, 'IgnoringWhitespace'.

Recursive

Indicator of whether comparator operates recursively, specified in the name-value pair argument, 'Recursively'.

Tolerance

Specific tolerance used in construction of the comparator, specified as a matlab.unittest.constraints.Tolerance object in the name-value pair argument, 'Within'.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects in the MATLAB® documentation.

Examples

expand all

Compare Cell Arrays

Create a test case for interactive testing.

import matlab.unittest.constraints.CellComparator
import matlab.unittest.constraints.StringComparator
import matlab.unittest.constraints.IsEqualTo

testCase = matlab.unittest.TestCase;

Use a CellComparator to test that two cell arrays are equal to each other.

actual = {'abc','def'};
expected = {'abc','def'};
testCase.verifyThat(actual, IsEqualTo(expected,...
    'Using', CellComparator(StringComparator)))
Interactive verification passed.

By default, the CellComparator only supports comparison empty cell arrays, it is necessary to pass it a StringComparator.

Change the actual value and compare it to the expected value. For the constraint to be satisfied, it is necessary that you construct the constraint to ignore case and whitespace.

actual = {'ABC','D E F'};
testCase.verifyThat(actual, IsEqualTo(expected, 'Using', ...
    CellComparator(StringComparator, ...
    'IgnoringWhitespace', true, 'IgnoringCase',true)))
Interactive verification passed.

Test nested cell arrays of strings by constructing the comparator to operate recursively.

actual = {'abc',{'def','ghi'}};
expected = {'abc',{'def','ghi'}};

testCase.verifyThat(actual, IsEqualTo(expected, 'Using', ...
    CellComparator(StringComparator, 'Recursively', true)))
Interactive verification passed.
Was this topic helpful?