Main Content

matlab.unittest.constraints.EndsWithSubstring Class

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

Test if value ends with specified string

Description

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

Creation

Description

example

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

example

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

Input Arguments

expand all

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

This argument sets the Suffix 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.EndsWithSubstring(suffix,IgnoringCase=true)

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

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

Properties

expand all

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

This property is set by the suffix 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 suffix by using the EndsWithSubstring constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.EndsWithSubstring

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Specify the actual value.

str = "This Is One Long Message!";

Verify that str ends with "Message!".

testCase.verifyThat(str,EndsWithSubstring("Message!"))
Verification passed.

Test if str ends with the substring "Age!". The test fails because the constraint is sensitive to case.

testCase.verifyThat(str,EndsWithSubstring("Age!"))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    EndsWithSubstring failed.
    --> The value does not end with the supplied suffix.
    
    Actual Value:
        "This Is One Long Message!"
    Expected Suffix:
        "Age!"

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

testCase.verifyThat(str,EndsWithSubstring("longmessage!", ...
    "IgnoringCase",true,"IgnoringWhitespace",true))
Verification passed.

Verify that str does not end with "Long".

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

Version History

Introduced in R2013a