matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues

Class: matlab.unittest.constraints.PublicPropertyComparator
Package: matlab.unittest.constraints

Comparator for public properties that supports any value in recursion

Description

example

PublicPropertyComparator.supportingAllValues creates a comparator for public properties of MATLAB® objects. This comparator supports any value in recursion. supportingAllValues is a Static method of the PublicPropertyComparator class.

PublicPropertyComparator.supportingAllValues(Name,Value) creates a comparator for public properties of MATLAB objects with additional options specified by one or more Name,Value pair arguments.

You typically pass this comparator to another constraint, such as IsEqualTo. You can use the Name,Value pairs of the IsEqualTo constraint with a comparator constructed with the PublicPropertyComparator.supportingAllValues syntax.

Input Arguments

expand all

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Setting for whether the comparator is insensitive to case, specified as false or true (logical 0 or 1). When it is false, the comparator is sensitive to case. The comparator uses this name-value pair when comparing character vectors and string arrays.

Fields to ignore during struct comparison, specified as a cell array of character vectors.

Example: PublicPropertyComparator.supportingAllValues('IgnoringFields',{'timestamp'})

Properties to ignore during object comparison, specified as a cell array of character vectors.

Example: PublicPropertyComparator.supportingAllValues('IgnoringProperties',{'Stack'})

Setting for whether the comparator is insensitive to whitespace characters, specified as false or true (logical 0 or 1). When it is false, the comparator is sensitive to whitespace characters. Whitespace characters consist of space, form feed, new line, carriage return, horizontal tab, and vertical tab. The comparator uses this name-value pair only if the contents being compared consist of character vectors.

Tolerance to use for numerical comparison, specified as a matlab.unittest.constraints.Tolerance object. The comparator uses this name-value pair only if the contents being compared consist of numeric types.

Limitations

  • The PublicPropertyComparator does not compare public properties of objects that overload the subsref, numel, or properties functions.

Examples

expand all

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.PublicPropertyComparator

testCase = TestCase.forInteractiveUse;

Test a passing case.

m1 = MException('Msg:ID','MsgText');
m2 = MException('Msg:ID','MsgText');
testCase.verifyThat(m1, IsEqualTo(m2, 'Using', ...
    PublicPropertyComparator.supportingAllValues))
Interactive verification passed.

Test a failing case.

m1 = MException('Msg:ID','MsgText');
m2 = MException('Msg:ID','msgtext');
testCase.verifyThat(m1, IsEqualTo(m2, 'Using', ...
    PublicPropertyComparator.supportingAllValues))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> Path to failure: <Value>.message
    --> StringComparator failed.
        --> The character arrays are not equal.
        
        Actual char:
            MsgText
        Expected char:
            msgtext

Actual MException:
      MException with properties:
    
        identifier: 'Msg:ID'
           message: 'MsgText'
             cause: {}
             stack: [0×1 struct]
Expected MException:
      MException with properties:
    
        identifier: 'Msg:ID'
           message: 'msgtext'
             cause: {}
             stack: [0×1 struct]

Test a case that passes when the comparator ignores differences in case.

m1 = MException('Msg:ID','MsgText');
m2 = MException('Msg:ID','msgtext');
testCase.verifyThat(m1, IsEqualTo(m2,'Using', ...
    PublicPropertyComparator.supportingAllValues('IgnoringCase',true)))
Interactive verification passed.

Test a case that passes when the comparator ignores the message property of the MException object.

testCase.verifyThat(m1, IsEqualTo(m2,'Using', ...
    PublicPropertyComparator.supportingAllValues('IgnoringProperties',{'message'})))
Interactive verification passed.

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.RelativeTolerance
import matlab.unittest.constraints.PublicPropertyComparator

testCase = TestCase.forInteractiveUse;

Define actual and expected timeseries objects. Perturb one of the actual data points by 1%.

expected = timeseries(1:10);
actual = expected;
actual.Data(7) = 1.01*actual.Data(7);

Test that the actual and expected values are equal within a relative tolerance of 2%.

testCase.verifyThat(actual, IsEqualTo(expected,...
    'Within', RelativeTolerance(.02)))
Verification failed.

---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> ObjectComparator failed.
    --> The objects are not equal using "isequal".
    --> The tolerance was ignored. The tolerance as specified does not support comparisons of timeseries values.
    
    Actual timeseries:
          timeseries
        
          Common Properties:
                    Name: 'unnamed'
                    Time: [10x1 double]
                TimeInfo: [1x1 tsdata.timemetadata]
                    Data: [1x1x10 double]
                DataInfo: [1x1 tsdata.datametadata]
        
          More properties, Methods
    Expected timeseries:
          timeseries
        
          Common Properties:
                    Name: 'unnamed'
                    Time: [10x1 double]
                TimeInfo: [1x1 tsdata.timemetadata]
                    Data: [1x1x10 double]
                DataInfo: [1x1 tsdata.datametadata]
        
          More properties, Methods

Use the PublicPropertyComparator in the construction of the constraint.

testCase.verifyThat(actual, IsEqualTo(expected,...
    'Within', RelativeTolerance(.02),...
    'Using', PublicPropertyComparator.supportingAllValues))
Interactive verification passed.

The test passes because the PublicPropertyComparator compares each public property individually instead of comparing the object all at once. In the former test, the ObjectComparator compares timeseries objects, and therefore relies on the isequal method of the timeseries class. Due to the perturbation in the actual timeseries, isequal returns false. The comparator does not apply the tolerance because the double-valued tolerance cannot apply directly to the timeseries object. In the latter test, the comparator applies the tolerance to each public property that contains double-valued data.

Introduced in R2014a