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

Tips

  • In most cases, you are not required to use a StringComparator object. The IsEqualTo class creates a constraint to test for equality between data of various types, including strings.

    Use a StringComparator object when you need to override the comparison performed by the IsEqualTo class. For example, if you want the comparison to fail when actual and expected values are nontextual, include a StringComparator object in your test. You also can use StringComparator to restrict the values contained in structures, cell arrays, and tables. In this example, MATLAB® throws an exception because the actual and expected values are numeric arrays.

    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.StringComparator
          
    exp = magic(5); 
    act = exp;
    
    testCase = matlab.unittest.TestCase.forInteractiveUse;
    testCase.verifyThat(act,IsEqualTo(exp,'Using',StringComparator))
    

Introduced in R2013a