# matlab.unittest.constraints.BooleanConstraint 类

## 描述

`matlab.unittest.constraints.BooleanConstraint` 类提供接口，您可以使用该接口创建约束，可以使用 `and` (`&`)、`or` (`|`) 和 `not` (`~`) 运算符对这些约束进行组合和求反。

• 实现 `satisfiedBy` 方法以编写比较逻辑。`BooleanConstraint` 类从 `matlab.unittest.constraints.Constraint` 继承此方法。

• 实现 `getDiagnosticFor` 方法，用于在测试框架根据约束评估实际值时产生诊断信息。`BooleanConstraint` 类从 `Constraint` 类继承此方法。

• 实现 `getNegativeDiagnosticFor` 方法，以便在框架根据求反约束评估实际值时产生诊断信息。当对约束求反时，必须使用与标准（非求反）用法不同的形式写入诊断信息。

### 类属性

 `Abstract` `true`

## 示例

```import matlab.unittest.TestCase import matlab.unittest.constraints.HasElementCount import matlab.unittest.constraints.HasLength import matlab.unittest.constraints.HasInf import matlab.unittest.constraints.HasNaN import matlab.unittest.constraints.IsEqualTo import matlab.unittest.constraints.IsGreaterThanOrEqualTo import matlab.unittest.constraints.IsReal```

`testCase = TestCase.forInteractiveUse;`

```testCase.verifyThat(3,IsReal & IsGreaterThanOrEqualTo(3)) ```
`Verification passed.`

```testCase.verifyThat(3,~IsEqualTo(4)) ```
`Verification passed.`

```testCase.verifyThat([1 2 3; 4 5 6],HasLength(6) | HasElementCount(6)) ```
`Verification passed.`

`testCase.verifyThat([3 NaN 5],HasNaN & HasInf)`
```Verification failed. --------------------- Framework Diagnostic: --------------------- AndConstraint failed. --> + [First Condition]: | HasNaN passed. | --> Indices that have NaN values: | 2 | | Actual Value: | 3 NaN 5 --> AND + [Second Condition]: | HasInf failed. | --> At least one element must be Inf or -Inf. | | Actual Value: | 3 NaN 5 -+---------------------```

```classdef IsSameSizeAs < matlab.unittest.constraints.BooleanConstraint properties (SetAccess=immutable) ValueWithExpectedSize end methods % Class constructor function constraint = IsSameSizeAs(value) constraint.ValueWithExpectedSize = value; end % Determine if the actual value satisfies the constraint function tf = satisfiedBy(constraint,actual) tf = constraint.sizeMatchesExpected(actual); end % Produce a diagnostic for the constraint function diagnostic = getDiagnosticFor(constraint,actual) import matlab.automation.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(actual) diagnostic = StringDiagnostic("IsSameSizeAs passed."); else diagnostic = StringDiagnostic( ... "IsSameSizeAs failed." + newline + "Actual Size: [" ... + int2str(size(actual)) + "]" + newline ... + "Expected Size: [" ... + int2str(size(constraint.ValueWithExpectedSize)) ... + "]"); end end end methods (Access=protected) % Produce a diagnostic for the negated constraint function diagnostic = getNegativeDiagnosticFor(constraint,actual) import matlab.automation.diagnostics.StringDiagnostic if constraint.sizeMatchesExpected(actual) diagnostic = StringDiagnostic( ... "Negated IsSameSizeAs failed." + newline + ... "Actual and expected sizes were the same ([" ... + int2str(size(actual)) + ... "]) but should not have been."); else diagnostic = StringDiagnostic( ... "Negated IsSameSizeAs passed."); end end end methods (Access=private) % Determine if the actual and expected values are the same size function tf = sizeMatchesExpected(constraint,actual) tf = isequal(size(actual), ... size(constraint.ValueWithExpectedSize)); end end end```

`testCase = matlab.unittest.TestCase.forInteractiveUse;`

```testCase.verifyThat(zeros(5), ... IsSameSizeAs(ones(5)) & ~IsSameSizeAs(ones(1,5)))```
`Verification passed.`