Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the 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.

'IgnoringProperties' — Properties to ignore during object comparison cell array of strings

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

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

'Recursively' — Setting for whether comparator operates recursivelyfalse (default) | true

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.

'IgnoringCase' — Setting for comparator insensitivity to casefalse (default) | true

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 only if the contents being compared consist of strings.

    Note:   This name-value pair argument will be removed in a future release. Use the 'IgnoringCase' option with the IsEqualTo constraint instead.

'IgnoringWhitespace' — Setting for comparator insensitivity to whitespace charactersfalse (default) | true

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 strings.

    Note:   This name-value pair argument will be removed in a future release. Use the 'IgnoringWhitespace' option with the IsEqualTo constraint instead.

'Within' — Tolerance to use for numerical comparisoninstance of matlab.unittest.constraints.Tolerance

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.

    Note:   This name-value pair argument will be removed in a future release. Use the 'Within' option with the IsEqualTo constraint instead.

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'.

IgnoreCase

Indicator if the comparator is insensitive to case, specified in the name-value pair argument, 'IgnoringCase'.

    Note:   This property will be removed in a future release.

IgnoreWhitespace

Indicator if the comparator is insensitive to whitespace characters, specified in the name-value pair argument, 'IgnoringWhitespace'.

    Note:   This property will be removed in a future release.

Tolerance

Specific tolerance used in construction of the comparator, specified as a matlab.unittest.constraints.Tolerance object in the name-value pair argument, 'Within'.

    Note:   This property will be removed in a future release.

Limitations

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

Copy Semantics

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

Examples

expand all

Instantiate PublicPropertyComparator Object

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 strings.

Construct a comparator that supports strings. 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?