matlab.unittest.constraints.ContainsSubstring - Test if value contains specified string - MATLAB (original) (raw)
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
c = matlab.unittest.constraints.ContainsSubstring([substring](#mw%5Faa08d11f-29fa-4c26-bd8e-2c2eac0c59a8))
creates a constraint to test if a value contains the specified string. The constraint is satisfied by a string scalar or character vector that containssubstring
.
c = matlab.unittest.constraints.ContainsSubstring([substring](#mw%5Faa08d11f-29fa-4c26-bd8e-2c2eac0c59a8),[Name,Value](#namevaluepairarguments))
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
Expected substring, specified as a nonempty string scalar or character vector.
This argument sets the [Substring](matlab.unittest.constraints.containssubstring-class.html#mw%5F7945465a-0e70-4e23-8b78-5d0c19510cad)
property.
Name-Value Arguments
Specify optional pairs of arguments asName1=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](matlab.unittest.constraints.containssubstring-class.html#mw%5F8c37558d-9fd6-4fde-bf1c-846a4e5fad79)
property.
Whether to ignore white space, specified as a numeric or logical0
(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](matlab.unittest.constraints.containssubstring-class.html#mw%5F3dec128c-c50e-4cb3-9525-e6cdc491636e)
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
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
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"))
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))
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))
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!"))
Version History
Introduced in R2013a
To specify the number of times a string scalar or character vector must occur, use theWithCount
name-value argument.