Documentation Center

  • Trial Software
  • Product Updates

matlab.unittest.constraints.HasField class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Constraint specifying structure containing particular field

Construction

HasField(fieldname) provides a constraint specifying structure containing particular field, fieldname. The constraint is satisfied if the actual value is a structure and that structure contains a field named fieldname.

Input Arguments

fieldname

Name of the field that a structure must contain to satisfy the constraint, specified as a string.

Properties

Field

Name of the field that a structure must contain to satisfy the constraint. Set this property through the constructor via the fieldname input argument.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects in the MATLAB® documentation.

Examples

expand all

Test That Structure Has Particular Field

Create at TestCase for interactive testing.

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

testCase = TestCase.forInteractiveUse;

Define the following structure, S, with two fields.

S = struct('Tag', 123, 'Serial', 345);

Verify that the structure has a 'Tag' field.

testCase.verifyThat(S, HasField('Tag'));
Interactive verification passed.

Verify that the structure has a 'tag' field.

testCase.verifyThat(S, HasField('tag'));
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
HasField failed.
--> The value did not have the expected field.
    
    Actual Fieldnames:
            'Tag'
            'Serial'
    Expected Fieldname:
            'tag'

Actual Value:
           Tag: 123
        Serial: 345

The verification fails because the field name comparison is case sensitive.

Verify that the structure has a 'Tag' field.

testCase.verifyThat(S, HasField('Tag'));
Interactive verification passed.

Verify that the structure has both a 'Tag' and a 'Serial' field.

testCase.verifyThat(S, HasField('Tag') & HasField('Serial'));
Interactive verification passed.

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

testCase.verifyThat(S, ~HasField('Name'));
Interactive verification passed.
Was this topic helpful?