matlab.unittest.constraints.IsSubstringOf class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Constraint specifying substring of another string

Construction

IsSubstringOf(superstring) creates a constraint specifying a substring of another string scalar or character vector. The constraint is satisfied only if the actual value is contained within an expected superstring, superstring.

IsSubstringOf(superstring,Name,Value) provides a constraint 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

superstring

Text that contains the actual value, specified as a string scalar or character vector. superstring can include newline characters.

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 to ignore case, specified as false or true (logical 0 or 1).

Default: false

'IgnoringWhitespace'

Indicator to ignore whitespace, specified as false or true (logical 0 or 1). If IgnoringWhitespace is true, MATLAB® removes any whitespace characters from both the actual value and the expected value before determining whether the constraint is satisfied.

Note

When IgnoringWhitespace is true, the input argument superstring must contain at least one non-whitespace character.

Default: false

'WithCount'

Number of times superstring must contain the actual value, specified as a positive integer.

The testing framework uses the count function to count occurrences of the actual value. When you specify an empty substring, the count function counts empty strings at the beginning and end of superstring and between each pair of its characters. In other words, if superstring has N characters, it also has N+1 empty substrings.

Properties

IgnoreCase

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

IgnoreWhitespace

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

Superstring

Superstring that includes the actual value, specified in the input argument, superstring.

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

testCase = TestCase.forInteractiveUse;

Define the actual value string.

S = string('This Is One Long String');

Test that the actual value 'One' is contained in S.

testCase.verifyThat('One', IsSubstringOf(S))
Verification passed.

Test that the actual value 'long' is contained in S.

testCase.verifyThat('long', IsSubstringOf(S))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSubstringOf failed.
    --> The value is not found within the superstring.
    
    Actual char:
        long
    Expected Superstring:
        "This Is One Long String"

By default, the IsSubstringOf constraint is case sensitive.

Repeat the test ignoring case.

testCase.verifyThat('long', IsSubstringOf(S, ...
    'IgnoringCase', true))
Verification passed.

Test that the actual value 'is' is contained in S twice. For the test to pass, configure the constraint to ignore case.

testCase.verifyThat('is', IsSubstringOf(S, ...
    'WithCount', 2, 'IgnoringCase', true))
Verification passed.

Test that the actual value 'thisisone' is contained in S. For the test to pass, configure the constraint to ignore whitespace and case.

testCase.verifyThat('thisisone', IsSubstringOf(S, ...
    'IgnoringCase', true, 'IgnoringWhitespace', true))
Verification passed.

Assert that the actual value 'longer' is not contained in S.

testCase.assertThat('Longer', ~IsSubstringOf(S))
Assertion passed.