Main Content

matlab.unittest.constraints.ObjectComparator Class

Namespace: matlab.unittest.constraints

Comparator for MATLAB or Java object arrays

Description

The matlab.unittest.constraints.ObjectComparator class provides a comparator for MATLAB® or Java® object arrays. To use this comparator in your tests, create an ObjectComparator instance, and specify it as the value of the Using name-value argument of the IsEqualTo constraint constructor.

Creation

Description

c = matlab.unittest.constraints.ObjectComparator creates a comparator for MATLAB or Java object arrays. The comparator first checks if the actual and expected values are object arrays of the same class and size with equal values for all properties. If so, then the comparator is satisfied. Otherwise, the comparator calls isequaln or isequal to make a comparison. (If the class of the expected value defines an isequaln method, the comparator calls isequaln. Otherwise, it calls isequal.) The comparator is satisfied if the call returns true.

example

c = matlab.unittest.constraints.ObjectComparator("Within",tol) uses the specified tolerance in comparison. When you use this syntax, the comparator first checks for equality, as described above. If the check passes, the comparator is satisfied. Otherwise, the comparator checks for equal class, size, and sparsity of the actual and expected values. If any of these checks fail, the comparator is not satisfied. If they pass, the comparator delegates comparison to tol.

example

Input Arguments

expand all

Tolerance, specified as a matlab.unittest.constraints.Tolerance object.

This argument sets the Tolerance property.

Example: matlab.unittest.constraints.AbsoluteTolerance(MyInt(1))

Properties

expand all

Tolerance, returned as a matlab.unittest.constraints.Tolerance object.

This property is set by the tol input argument.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Compare actual and expected values using the ObjectComparator class.

In a file named MyInt.m in your current folder, create a subclass of the int8 class.

classdef MyInt < int8
    methods
        function obj = MyInt(value)
            obj = obj@int8(value);
        end
    end
end

Import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.ObjectComparator
import matlab.unittest.constraints.AbsoluteTolerance

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Use an ObjectComparator instance to compare two MyInt objects that are constructed with the same input value. The test passes.

testCase.verifyThat(MyInt(10),IsEqualTo(MyInt(10), ...
    "Using",ObjectComparator))
Verification passed.

Compare two MyInt objects that are constructed with different input values. The test fails.

testCase.verifyThat(MyInt(11),IsEqualTo(MyInt(10), ...
    "Using",ObjectComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> ObjectComparator failed.
        --> The objects are not equal using "isequaln".
        
        Actual Value:
          MyInt:
        
          int8 data:
           11
        Expected Value:
          MyInt:
        
          int8 data:
           10
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingObjectComparatorExample.m (CompareValuesUsingObjectComparatorExample) at 30

For the test to pass, specify that values must be equal within an absolute tolerance of 1. The value of the tolerance must be of the same class as the actual and expected values.

testCase.verifyThat(MyInt(11),IsEqualTo(MyInt(10), ...
    "Using", ObjectComparator("Within",AbsoluteTolerance(MyInt(1)))))
Verification passed.

Tips

  • In most cases, you are not required to use an ObjectComparator instance. The IsEqualTo class creates a constraint to test for the equality of various data types, including MATLAB and Java object arrays.

    Use an ObjectComparator instance when you need to override the comparison performed by the IsEqualTo class. For example, if you want the comparison to fail when actual and expected values are not MATLAB object arrays, include an ObjectComparator instance in your test. You also can use ObjectComparator to restrict the values contained in cell arrays, structures, dictionaries, tables, and public properties of MATLAB object arrays. In this example, MATLAB throws an error because the actual and expected values are numeric scalars.

    import matlab.unittest.TestCase
    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.ObjectComparator
    
    testCase = TestCase.forInteractiveUse;
    exp = 5; 
    act = exp;
    testCase.verifyThat(act,IsEqualTo(exp,"Using",ObjectComparator))
    

Version History

Introduced in R2013a