Main Content

matlab.unittest.constraints.IsSubstringOf Class

Namespace: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Test if value is substring of specified string

Description

The matlab.unittest.constraints.IsSubstringOf class provides a constraint to test if a value is a substring of a specified string.

Creation

Description

c = matlab.unittest.constraints.IsSubstringOf(superstring) creates a constraint to test if a value is a substring of the specified string. The constraint is satisfied by a string scalar or character vector that occurs in superstring.

example

c = matlab.unittest.constraints.IsSubstringOf(superstring,Name,Value) sets additional options using one or more name-value arguments. For example, c = matlab.unittest.constraints.IsSubstringOf(superstring,"IgnoringCase",true) creates a constraint that is insensitive to case.

example

Input Arguments

expand all

Expected superstring, specified as a nonempty string scalar or character vector.

This argument sets the Superstring property.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: c = matlab.unittest.constraints.IsSubstringOf(superstring,IgnoringCase=true)

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: c = matlab.unittest.constraints.IsSubstringOf(superstring,"IgnoringCase",true)

Whether to ignore case, specified as a numeric or logical 0 (false) or 1 (true). By default, the constraint is sensitive to case.

This argument sets the IgnoreCase property.

Whether to ignore white space, specified as a numeric or logical 0 (false) or 1 (true). By default, the constraint is sensitive to white-space characters. White-space characters consist of space (' '), form feed ('\f'), new line ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').

This argument sets the IgnoreWhitespace property.

Note

When IgnoringWhitespace is true, superstring must contain at least one non-white-space character.

Number of times the value to test must occur in superstring, specified as a positive integer scalar.

You can specify this name-value argument to count only nonoverlapping occurrences of a substring. For example, this tests fails.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsSubstringOf

testCase = TestCase.forInteractiveUse;
testCase.verifyThat("aba",IsSubstringOf("ababa","WithCount",2))

The testing framework uses the count function to count occurrences of the value to test. If the value to test is an empty string, the count function counts empty strings at the start and end of superstring and between each pair of its characters. In other words, if superstring has n characters, it has n+1 empty substrings.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Properties

expand all

Expected superstring, returned as a string scalar or character vector.

This property is set by the superstring input argument.

Attributes:

GetAccess
public
SetAccess
immutable

Whether to ignore case, returned as a logical 0 (false) or 1 (true). By default, the constraint is sensitive to case.

This property is set by the IgnoringCase name-value argument.

Attributes:

GetAccess
public
SetAccess
private

Whether to ignore white space, returned as a logical 0 (false) or 1 (true). By default, the constraint is sensitive to white-space characters.

This property is set by the IgnoringWhitespace name-value argument.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Test for substring occurrences using the IsSubstringOf constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsSubstringOf

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Specify the expected superstring.

str = "This Is One Long Message!";

Verify that the value "One" is a substring of str.

testCase.verifyThat("One",IsSubstringOf(str))
Verification passed.

Test if "long" is a substring of str. The test fails because the constraint is sensitive to case.

testCase.verifyThat("long",IsSubstringOf(str))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSubstringOf failed.
    --> The value is not found within the superstring.
    
    Actual Value:
        "long"
    Expected Superstring:
        "This Is One Long Message!"

Test if the value "is" occurs in str twice. For the test to pass, ignore case.

testCase.verifyThat("is",IsSubstringOf(str, ...
    "WithCount",2,"IgnoringCase",true))
Verification passed.

Test if the value "thisisone" is a substring of str. For the test to pass, ignore case and white-space characters.

testCase.verifyThat("thisisone",IsSubstringOf(str, ...
    "IgnoringCase",true,"IgnoringWhitespace",true))
Verification passed.

Verify that "Longer" is not a substring of str.

testCase.verifyThat("Longer",~IsSubstringOf(str))
Verification passed.

Version History

Introduced in R2013a

expand all