Main Content

matlab.unittest.constraints.StringComparator class

Package: matlab.unittest.constraints

Comparator for two strings, character arrays, or cell arrays of character arrays

Construction

StringComparator creates a comparator for two strings, character arrays, or cell arrays of character arrays. The comparator is satisfied if the two values are equal. By default, StringComparator checks that the values have equal size and class, and then performs a case-sensitive comparison of each value.

StringComparator(Name,Value) creates a comparator with additional options specified by one or more Name,Value pair arguments.

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

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 characters, specified in the name-value pair argument, 'IgnoringWhitespace'

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

testCase = TestCase.forInteractiveUse;

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

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

Change the actual character vector and repeat the comparison.

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

---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> StringComparator failed.
    --> The character arrays are not equal.
    
    Actual char:
        coffee
    Expected char:
        coF Fee

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

testCase.verifyThat(actual,IsEqualTo(expected, ...
    'Using', StringComparator('IgnoringCase', true, ...
    'IgnoringWhitespace', true)))
Interactive verification passed.
Introduced in R2013a