Main Content

matlab.unittest.constraints.EveryCellOf 类

命名空间: matlab.unittest.constraints

测试元胞数组的每个元素是否满足约束

描述

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

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

创建对象

描述

示例

p = matlab.unittest.constraints.EveryCellOf(actualValue) 创建一个代理来测试指定元胞数组的每个元素是否满足约束并设置 ActualValue 属性。当您使用此代理来测试约束时,如果 actualValue 的每个元素都满足此约束,则测试通过。

属性

全部展开

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

属性:

GetAccess
public
SetAccess
private

示例

全部折叠

使用 EveryCellOf 类测试元胞数组的所有元素是否满足约束。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.EveryCellOf
import matlab.unittest.constraints.HasElementCount
import matlab.unittest.constraints.ContainsSubstring
import matlab.unittest.constraints.IsEmpty
import matlab.unittest.constraints.IsReal

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

testCase = TestCase.forInteractiveUse;

验证 {{'hello','world'},{11,38}} 的每个元胞都包含两个元素。

testCase.verifyThat(EveryCellOf({{'hello','world'},{11,38}}), ...
    HasElementCount(2))
Verification passed.

验证元胞数组 {'Rain','Main','Plain'} 的每个元素都包含子字符串 "ain"

testCase.verifyThat(EveryCellOf({'Rain','Main','Plain'}), ...
    ContainsSubstring("ain"))
Verification passed.

测试元胞数组 {struct([]),''} 的每个元素是否为空。测试通过。

testCase.verifyThat(EveryCellOf({struct([]),''}),IsEmpty)
Verification passed.

测试元胞数组 {0,4i} 的每个元素是否为复数。测试失败,因为其中一个元素是实数值。

testCase.verifyThat(EveryCellOf({0,4i}),~IsReal)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    At least one cell failed.
    
    Failing indices:
        1
    The first failing cell failed because:
    --> Negated IsReal failed.
        --> The value must not be real.
        
        Actual Value:
             0
    
    Actual Value Cell Array:
      1×2 cell array
    
        {[0]}    {[0.000000000000000 + 4.000000000000000i]}

版本历史记录

在 R2013a 中推出