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)
IgnoringCase
— 是否忽略大小写
false
或 0
(默认) | true
或 1
比较文本值时是否忽略大小写,指定为数值或逻辑值 0
(false
) 或 1
(true
)。默认情况下,比较器区分大小写。
IgnoringWhitespace
— 是否忽略空白
false
或 0
(默认) | true
或 1
比较文本值时是否忽略空白,指定为数值或逻辑值 0
(false
) 或 1
(true
)。默认情况下,比较器区分空白字符。空白字符包括空格 (' '
)、换页符 ('\f'
)、换行符 ('\n'
)、回车符 ('\r'
)、水平制表符 ('\t'
) 和垂直制表符 ('\v'
)。
IgnoringFields
— 比较结构体数组时要忽略的字段
字符串数组 | 字符向量元胞数组
比较结构体数组时要忽略的字段,指定为字符串数组或字符向量元胞数组。比较器不比较指定字段中的值。
示例: "IgnoringFields","field1"
IgnoringProperties
— 比较对象数组时要忽略的属性
字符串数组 | 字符向量元胞数组
比较 MATLAB 对象数组时要忽略的属性,指定为字符串数组或字符向量元胞数组。比较器不比较指定属性的值。
示例: "IgnoringProperties","Property1"
Within
— 比较期间要使用的容差
matlab.unittest.constraints.Tolerance
对象
比较期间要使用的容差,指定为 matlab.unittest.constraints.Tolerance
对象。
示例: "Within",matlab.unittest.constraints.AbsoluteTolerance(1)
示例: "Within",matlab.unittest.constraints.AbsoluteTolerance(1) | matlab.unittest.constraints.RelativeTolerance(0.1)
示例
比较不同类型的公共属性
使用支持所有数据类型的比较器比较两个对象的公共属性。
在当前文件夹内一个名为 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
实例重复该测试。由于 Name
和 Age
属性属于不同类型,请使用支持所有数据类型的比较器来执行比较。尽管 s1.Field
和 s2.Field
具有不同的值,测试仍通过,因为比较器只检查 s1
和 s2
的公共属性。
testCase.verifyThat(s1,IsEqualTo(s2, ... "Using",PublicPropertyComparator.supportingAllValues))
Verification passed.
创建一个新的 Student
对象,并将其与 s1
进行比较。测试失败,因为 s1.Name
和 s3.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.
局限性
PublicPropertyComparator
类不支持重载subsref
、numel
或properties
函数的对象的公共属性。
版本历史记录
在 R2014a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)