Main Content

matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues

Class: matlab.unittest.constraints.PublicPropertyComparator
Namespace: matlab.unittest.constraints

Comparator for public properties that supports all values in recursion

Description

example

c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues creates a comparator for public properties of MATLAB® object arrays. This comparator operates recursively and supports all data types contained in the public properties.

example

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

Input Arguments

expand all

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.PublicPropertyComparator.supportingAllValues(IgnoringCase=true)

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

Example: c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues("IgnoringCase",true)

Whether to ignore case when comparing textual values, specified as a numeric or logical 0 (false) or 1 (true). By default, the comparator is sensitive to case.

Whether to ignore white space when comparing textual values, specified as a numeric or logical 0 (false) or 1 (true). By default, the comparator 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').

Fields to ignore when comparing structure arrays, specified as a string array or cell array of character vectors. The comparator does not compare the values in the specified fields.

Example: "IgnoringFields","field1"

Properties to ignore when comparing MATLAB object arrays, specified as a string array or cell array of character vectors. The comparator does not compare the values of the specified properties.

Example: "IgnoringProperties","Property1"

Tolerance to use during comparison, specified as a matlab.unittest.constraints.Tolerance object.

Example: "Within",matlab.unittest.constraints.AbsoluteTolerance(1)

Example: "Within",matlab.unittest.constraints.AbsoluteTolerance(1) | matlab.unittest.constraints.RelativeTolerance(0.1)

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Compare the public properties of two objects using a comparator that supports all data types.

In a file named Student.m in your current folder, create the Student class. The class has two public properties and one private property.

classdef Student
    properties (SetAccess=immutable)
        Name
        Age
    end
    properties (Access=private)
        Field
    end
    methods
        function obj = Student(name,age,field)
            arguments
                name = "";
                age = [];
                field = "";
            end
            obj.Name = name;
            obj.Age = age;
            obj.Field = field;
        end
    end
end

Import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Create two Student objects and compare them using the IsEqualTo constraint. In this example, the test fails because of the different values in the private property.

s1 = Student("Mary Jones",20,"physics");
s2 = Student("Mary Jones",20,"biology");
testCase.verifyThat(s1,IsEqualTo(s2))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> ObjectComparator failed.
        --> The objects are not equal using "isequaln".
        
        Actual Value:
          Student with properties:
        
            Name: "Mary Jones"
             Age: 20
        Expected Value:
          Student with properties:
        
            Name: "Mary Jones"
             Age: 20
    ------------------
    Stack Information:
    ------------------
    In C:\work\ComparePublicPropertiesExample.m (ComparePublicPropertiesExample) at 29

Repeat the test using a PublicPropertyComparator instance. Because the Name and Age properties are of different types, perform the comparison by using a comparator that supports all data types. Even though s1.Field and s2.Field have different values, the test passes because the comparator examines only the public properties of s1 and s2.

testCase.verifyThat(s1,IsEqualTo(s2, ...
    "Using",PublicPropertyComparator.supportingAllValues))
Verification passed.

Create a new Student object and compare it to s1. The test fails because s1.Name and s3.Name have different values.

s3 = Student("mary jones",20,"chemistry");
testCase.verifyThat(s1,IsEqualTo(s3, ...
    "Using",PublicPropertyComparator.supportingAllValues))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>.Name
        --> StringComparator failed.
            --> The strings are not equal.
            
            Actual Value:
                "Mary Jones"
            Expected Value:
                "mary jones"
    
    Actual Value:
      Student with properties:
    
        Name: "Mary Jones"
         Age: 20
    Expected Value:
      Student with properties:
    
        Name: "mary jones"
         Age: 20
    ------------------
    Stack Information:
    ------------------
    In C:\work\ComparePublicPropertiesExample.m (ComparePublicPropertiesExample) at 44

For the test to pass, use a comparator that ignores case.

testCase.verifyThat(s1,IsEqualTo(s3, ...
    "Using",PublicPropertyComparator.supportingAllValues( ...
    "IgnoringCase",true)))
Verification passed.

Alternatively, you can instruct the comparator to ignore the Name property during comparison.

testCase.verifyThat(s1,IsEqualTo(s3, ...
    "Using",PublicPropertyComparator.supportingAllValues( ...
    "IgnoringProperties","Name")))
Verification passed.

Limitations

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

Version History

Introduced in R2014a