Main Content

matlab.unittest.constraints.ContainsSubstring Class

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

Test if value contains specified string

Description

The matlab.unittest.constraints.ContainsSubstring class provides a constraint to test if a value contains a specified string.

Creation

Description

example

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

example

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

Input Arguments

expand all

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

This argument sets the Substring 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.ContainsSubstring(substring,IgnoringCase=true)

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

Example: c = matlab.unittest.constraints.ContainsSubstring(substring,"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, substring must contain at least one non-white-space character.

Number of times substring must occur, 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.ContainsSubstring

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

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

Properties

expand all

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

This property is set by the substring 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 substrings using the ContainsSubstring constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.ContainsSubstring

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Specify the actual value.

str = "This Is One Long Message!";

Verify that str contains the string "One".

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

Test if str contains the string "long". The test fails because the constraint is sensitive to case.

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

Test if str contains the string "is" twice. For the test to pass, ignore case.

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

Test if str contains the string "thisisone". For the test to pass, ignore case and white-space characters.

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

Verify that str does not contain a string that is longer than itself.

testCase.verifyThat(str,~ContainsSubstring( ...
    "This Is One Long Message With Extra Words!"))
Verification passed.

Version History

Introduced in R2013a

expand all