matlab.unittest.constraints.StringComparator class

Package: matlab.unittest.constraints

Comparator for two strings

Construction

StringComparator creates a comparator for two strings. The comparator is satisfied if the two strings are equal.

StringComparator(,Name,Value) creates 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

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 if 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.

Default: false

'IgnoringWhitespace'

Indicator if 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.

Default: false

Properties

IgnoreCase

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

IgnoreWhitespace

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

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.TestCase
import matlab.unittest.constraints.StringComparator
import matlab.unittest.constraints.IsEqualTo

testCase = TestCase.forInteractiveUse;

Verify that the actual and expected strings are equal using a string comparator.

expected = 'coffee';
actual = 'coffee';
testCase.verifyThat(actual,IsEqualTo(expected, ...
    'Using', StringComparator))
Interactive verification passed.

Change the actual string and repeat the comparison.

expected = 'coF Fee';
testCase.verifyThat(actual,IsEqualTo(expected, ...
    'Using', StringComparator))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> StringComparator failed.
    --> The strings are not equal

Actual String:
    coffee
Expected String:
    coF Fee

For the test to pass, construct a comparator that ignores case and whitespace.

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