Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

matlab.unittest.constraints.PublicPropertyComparator class

Package: matlab.unittest.constraints

Comparator for public properties of MATLAB objects

Description

The PublicPropertyComparator compares public properties of MATLAB® objects.

The PublicPropertyComparator supports MATLAB objects or arrays of objects and recursively compares data structures contained in the public properties. The PublicPropertyComparator is different from the isequal function because it examines only the public properties of the objects.

Typically you construct a PublicPropertyComparator using the supportingAllValues static method.

Construction

PublicPropertyComparator creates a comparator for public properties of MATLAB objects. This comparator supports only objects with no public properties.

PublicPropertyComparator(compObj) indicates a comparator, compObj, that defines the comparator used to compare public properties. This comparator supports recursion only in the data types supported by compObj.

PublicPropertyComparator(compObj,Name,Value) provides a comparator with additional options specified by one or more Name,Value pair arguments.

Methods

supportingAllValuesComparator for public properties that supports any value in recursion

Input Arguments

expand all

compObj

Comparator object

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 single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

expand all

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

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

Setting for whether comparator operates recursively, specified as false or true (logical 0 or 1). When this value is false, the comparator does not operate recursively on its data.

When the value is true, the data types that the public property comparator supports are fully supported in recursion.

Properties

IgnoredProperties

Properties to ignore during object comparison, specified in the name-value pair argument, 'IgnoringProperties'.

Recursive

Indicator of whether comparator operates recursively, specified in the name-value pair argument, 'Recursively'.

Limitations

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

Copy Semantics

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

Examples

expand all

In a file in your working folder, construct this Employee class.

classdef Employee
    properties (SetAccess=immutable)
        Name
    end
    properties (Access=private)
        Location
    end
    methods
        function obj = Employee(name,location)
            obj.Name = name;
            obj.Location = location;
        end
    end
end

At the command prompt, create two instances of the Employee class.

e1 = Employee('sam','Building A');
e2 = Employee('Sam','Building B');

Create a test case for interactive testing.

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

testCase = TestCase.forInteractiveUse;

Construct a comparator and verify that e1 and e2 are equal.

compObj = PublicPropertyComparator;
testCase.verifyThat(e1, IsEqualTo(e2,'Using',compObj))
Error using matlab.unittest.internal.constraints.ContainerComparator/throwUnsupportedElement (line 220)
The matlab.unittest.constraints.PublicPropertyComparator found no comparator which supports the element below:

Element (char):
    Sam

Error in matlab.unittest.internal.constraints.ContainerComparator/getComparatorFor (line 175)
            comparator.throwUnsupportedElement(expElement);

Error in matlab.unittest.internal.constraints.ContainerComparator/haveEqualValues (line 185)
                comp = comparator.getComparatorFor(expElement);

Error in matlab.unittest.internal.constraints.ContainerComparator/satisfiedBy (line 94)
                   comparator.haveEqualValues(actContainer,expContainer);

Error in matlab.unittest.constraints.IsEqualTo/satisfiedBy (line 176)
            bool = constraint.Comparator.satisfiedBy(actual, constraint.Expected);

Error in matlab.unittest.internal.qualifications.QualificationDelegate/qualifyThat (line 62)
                result = constraint.satisfiedBy(actual);

Error in matlab.unittest.qualifications.Verifiable/verifyThat (line 227)
            qualifyThat(verifiable.VerificationDelegate, ...

The test fails because, by default, the PublicPropertyComparator does not support character vectors.

Construct a comparator that supports character vectors. Specify that the comparison is not case-sensitive.

compObj = PublicPropertyComparator(StringComparator);
testCase.verifyThat(e1, IsEqualTo(e2,'Using',compObj, 'IgnoringCase',true))
Interactive verification passed.

The test passes even though e1.Location and e2.Location are not the same. Since Location is a private property, the comparator does not compare its contents.

Introduced in R2014a

Was this topic helpful?