Main Content

matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues

类: matlab.unittest.constraints.PublicPropertyComparator
命名空间: matlab.unittest.constraints

在递归中支持所有值的公共属性比较器

说明

示例

c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues 为 MATLAB® 对象数组的公共属性创建一个比较器。此比较器以递归方式运算,支持公共属性中包含的所有数据类型。

示例

c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues(Name,Value) 使用一个或多个名称-值参量设置其他选项。例如,c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues("IgnoringCase",true) 创建一个不区分大小写的比较器。

输入参数

全部展开

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

示例: c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues(IgnoringCase=true)

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

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

比较文本值时是否忽略大小写,指定为数值或逻辑值 0 (false) 或 1 (true)。默认情况下,比较器区分大小写。

比较文本值时是否忽略空白,指定为数值或逻辑值 0 (false) 或 1 (true)。默认情况下,比较器区分空白字符。空白字符包括空格 (' ')、换页符 ('\f')、换行符 ('\n')、回车符 ('\r')、水平制表符 ('\t') 和垂直制表符 ('\v')。

比较结构体数组时要忽略的字段,指定为字符串数组或字符向量元胞数组。比较器不比较指定字段中的值。

示例: "IgnoringFields","field1"

比较 MATLAB 对象数组时要忽略的属性,指定为字符串数组或字符向量元胞数组。比较器不比较指定属性的值。

示例: "IgnoringProperties","Property1"

比较期间要使用的容差,指定为 matlab.unittest.constraints.Tolerance 对象。

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

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

属性

Statictrue

要了解方法的属性,请参阅方法属性

示例

全部展开

使用支持所有数据类型的比较器比较两个对象的公共属性。

在当前文件夹内一个名为 Student.m 的文件中创建 Student 类。该类有两个公共属性和一个私有属性。

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 matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.PublicPropertyComparator

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

创建两个 Student 对象,并使用 IsEqualTo 约束比较它们。在此示例中,因为私有属性中的值不同,测试失败。

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

使用 PublicPropertyComparator 实例重复该测试。由于 NameAge 属性属于不同类型,请使用支持所有数据类型的比较器来执行比较。尽管 s1.Fields2.Field 具有不同的值,测试仍通过,因为比较器只检查 s1s2 的公共属性。

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

创建一个新的 Student 对象,并将其与 s1 进行比较。测试失败,因为 s1.Names3.Name 的值不同。

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

要使测试通过,请使用忽略大小写的比较器。

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

您也可以指示比较器在比较期间忽略 Name 属性。

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

局限性

版本历史记录

在 R2014a 中推出