Main Content

matlab.unittest.constraints.AnyCellOf 类

命名空间: matlab.unittest.constraints

测试元胞数组是否有任一元素满足约束

描述

matlab.unittest.constraints.AnyCellOf 类提供实际值的代理,因此您可以测试元胞数组中是否有至少一个元素满足给定的约束。当您在测试中包含代理时,测试框架会逐元素对约束进行检查。

您可以在使用鉴定方法 assertThatassumeThatfatalAssertThatverifyThat 执行的测试中使用此类的实例。该类不修改提供的实际值。它仅充当执行约束分析的包装器。

创建对象

描述

示例

p = matlab.unittest.constraints.AnyCellOf(actualValue) 创建一个代理来测试指定元胞数组是否有任一元素满足约束并设置 ActualValue 属性。当您使用此代理根据约束进行测试时,如果 actualValue 中有至少一个元素满足约束,则测试通过。

属性

全部展开

要根据约束进行测试的实际值,以任何数据类型的值的形式返回。在创建代理的过程中指定此属性的值。尽管您能够指定任何数据类型的值,但如果实际值不是非空元胞数组,测试仍会失败。

属性:

GetAccess
public
SetAccess
private

示例

全部折叠

使用 AnyCellOf 类测试元胞数组中是否有至少一个元素满足约束。

首先,导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.AnyCellOf
import matlab.unittest.constraints.HasElementCount
import matlab.unittest.constraints.IsFinite
import matlab.unittest.constraints.IsLessThan
import matlab.unittest.constraints.Matches
import matlab.unittest.constraints.IsEmpty

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

testCase = TestCase.forInteractiveUse;

验证 {42,[11 38],1:5} 至少有一个元胞包含五个元素。

testCase.verifyThat(AnyCellOf({42,[11 38],1:5}),HasElementCount(5))
Verification passed.

验证元胞数组 {NaN,Inf,5} 至少有一个元素是有限值。

testCase.verifyThat(AnyCellOf({NaN,Inf,5}),IsFinite)
Verification passed.

验证元胞数组 {-1,5} 至少有一个元素是负值。

testCase.verifyThat(AnyCellOf({-1,5}),IsLessThan(0))
Verification passed.

测试元胞数组 {'Coffee','Tea','Water'} 是否至少有一个元素与 "tea" 匹配(不考虑大小写)。测试通过。

testCase.verifyThat(AnyCellOf({'Coffee','Tea','Water'}), ...
    Matches("tea","IgnoringCase",true))
Verification passed.

测试元胞数组 {struct([]),''} 是否至少有一个元素为非空。测试失败,因为两个元素都为空。

testCase.verifyThat(AnyCellOf({struct([]),''}),~IsEmpty)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    All cells failed. The first cell failed because:
    --> Negated IsEmpty failed.
        --> The value must not be empty.
        --> The value has a size of [0  0].
        
        Actual Value:
          0×0 empty struct array with no fields.
    
    Actual Value Cell Array:
      1×2 cell array
    
        {0×0 struct}    {0×0 char}

版本历史记录

在 R2013a 中推出