Main Content

matlab.unittest.constraints.HasField Class

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

Test if structure array has specified field

Description

The matlab.unittest.constraints.HasField class provides a constraint to test if a value is a structure array that has a specified field.

Creation

Description

example

c = matlab.unittest.constraints.HasField(field) creates a constraint to test if a value is a structure array that has the specified field and sets the Field property. The constraint is satisfied by a structure array that has a field named field.

Properties

expand all

Name of the expected field, returned as a character vector. Specify the value of this property during creation of the constraint as a string scalar or character vector.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Test a structure using the HasField constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasField

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Create a structure with two fields.

s = struct("ID",10,"score",90);

Verify that s is a structure with an "ID" field.

testCase.verifyThat(s,HasField("ID"))
Verification passed.

Test if the structure has a "Score" field. The test fails because the field name comparison is case sensitive.

testCase.verifyThat(s,HasField("Score"))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    HasField failed.
    --> The value did not have the expected field.
        
        Actual Fieldnames:
                'ID'
                'score'
        Expected Fieldname:
                'Score'
    
    Actual Value:
      struct with fields:
    
           ID: 10
        score: 90

Test if the structure has both an "ID" and a "score" field. The test passes.

testCase.verifyThat(s,HasField("ID") & HasField("score"))
Verification passed.

Verify that the structure does not have a "Name" field.

testCase.verifyThat(s,~HasField("Name"))
Verification passed.

Version History

Introduced in R2013b