Main Content

matlab.unittest.constraints.ContainsSubstring class

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

Constraint specifying string containing substring

Construction

ContainsSubstring(substring) creates a constraint that specifies a string scalar or character vector containing substring. The constraint is satisfied only if the actual value contains an expected substring.

ContainsSubstring(substring,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

substring

Text that must be contained within the actual value, specified as a character vector or string scalar. substring 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 substring must contain at least one non-whitespace character.

Default: false

WithCount

Number of times substring must be contained within the actual value, specified as a positive integer.

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

Substring

Character vector or string scalar that must be included in the actual value, specified in the input argument, substring.

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

testCase = TestCase.forInteractiveUse;

Define the actual value.

actVal = 'This Is One Long Message';

Test that actVal contains the text 'One'.

testCase.verifyThat(actVal, ContainsSubstring('One'))
Verification passed.

Test that actVal contains the text 'long'.

testCase.verifyThat(actVal, ContainsSubstring('long'))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    ContainsSubstring failed.
    --> The value does not contain the substring.
    
    Actual char:
        This Is One Long Message
    Expected Substring:
        long

By default, the ContainsSubstring constraint is case sensitive.

Repeat the test ignoring case.

testCase.verifyThat(actVal, ContainsSubstring('long', ...
    'IgnoringCase', true))
Verification passed.

Test that actVal contains the text 'is' twice. For the test to pass, configure the constraint to ignore case.

testCase.verifyThat(actVal, ContainsSubstring('is', ...
    'WithCount', 2, 'IgnoringCase', true))
Verification passed.

Test that actVal contains the text 'thisisone'. For the test to pass, configure the constraint to ignore whitespace and case.

testCase.verifyThat(actVal, ContainsSubstring('thisisone', ...
    'IgnoringCase', true, 'IgnoringWhitespace', true))
Verification passed.

Assert that actVal does not contain the text 'longer'.

testCase.assertThat(actVal, ~ContainsSubstring('longer', ...
    'IgnoringCase', true))
Assertion passed.