Main Content

matlab.unittest.constraints.StartsWithSubstring Class

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

Test if value starts with specified string

Description

The matlab.unittest.constraints.StartsWithSubstring class provides a constraint to test if a value starts with a specified string.

Creation

Description

c = matlab.unittest.constraints.StartsWithSubstring(prefix) creates a constraint to test if a value starts with the specified string. The constraint is satisfied by a string scalar or character vector that starts with prefix.

example

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

example

Input Arguments

expand all

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

This argument sets the Prefix 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.StartsWithSubstring(prefix,IgnoringCase=true)

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

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

Properties

expand all

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

This property is set by the prefix 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 if a string has a specified prefix by using the StartsWithSubstring constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.StartsWithSubstring

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Specify the actual value.

str = "This Is One Long Message!";

Verify that str starts with "This".

testCase.verifyThat(str,StartsWithSubstring("This"))
Verification passed.

Test if str starts with the substring "This is". The test fails because the constraint is sensitive to case.

testCase.verifyThat(str,StartsWithSubstring("This is"))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    StartsWithSubstring failed.
    --> The value does not start with the supplied prefix.
    
    Actual Value:
        "This Is One Long Message!"
    Expected Prefix:
        "This is"

Test if str starts with the substring "thisisone". For the test to pass, ignore case and white-space characters.

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

Verify that str does not start with "Long".

testCase.verifyThat(str,~StartsWithSubstring("Long"))
Verification passed.

Version History

Introduced in R2013a