Documentation

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 supports only empty cell arrays.

CellComparator(compObj,Name,Value) provides a comparator with additional options specified by one or more Name,Value pair arguments.

CellComparator(Name,Value) provides a comparator for empty cell arrays with additional options specified by one or more Name,Value pair arguments.

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 uses this name-value pair only if the contents being compared are strings.

Default: false

'IgnoringWhitespace'

Indicator whether the comparator is insensitive to whitespace characters, specified as false or true (logical 0 or 1). When it is false, the comparator is sensitive to whitespace characters. Whitespace characters consist of space, form feed, new line, carriage return, horizontal tab, and vertical tab. The comparator uses this name-value pair only 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 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 uses this name-value pair only if the contents being compared are numeric types.

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 characters, 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

collapse 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 supports only comparison of empty cell arrays. Therefore, it is necessary to pass it a StringComparator.

Change the actual value and compare it to the expected value. To satisfy the constraint, construct it to ignore case and whitespace characters.

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.

Introduced in R2013a

Was this topic helpful?