メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

matlab.unittest.constraints.BooleanConstraint クラス

R2026a

名前空間: matlab.unittest.constraints
スーパークラス: matlab.unittest.constraints.Constraint

論理演算をサポートする制約の基本的なインターフェイス

説明

matlab.unittest.constraints.BooleanConstraint クラスは、and (&)、or (|)、および not (~) の各演算子を使用して組み合わせや打ち消しができる制約を作成するために使用できるインターフェイスを提供します。

論理演算をサポートするカスタム制約クラスを作成するには、matlab.unittest.constraints.BooleanConstraint からクラスを派生させ、必要な抽象メソッドを実装します。

  • satisfiedBy メソッドを実装して比較ロジックをエンコードします。BooleanConstraint クラスは matlab.unittest.constraints.Constraint からこのメソッドを継承します。

  • getDiagnosticFor メソッドを実装して、テスト フレームワークが制約に照らして実際の値を評価したときに診断情報を生成します。BooleanConstraint クラスは Constraint クラスからこのメソッドを継承します。

  • getNegativeDiagnosticFor メソッドを実装して、フレームワークが打ち消された制約に照らして実際の値を評価したときに診断情報を生成します。制約が打ち消された場合は、標準の (打ち消し以外の) 使用が発生したときに表示される形式とは異なる形式で診断を記述しなければなりません。

BooleanConstraint クラスは Constraint クラスから派生しているため、BooleanConstraint サブクラスは Constraint サブクラスで提供されている機能をサポートしています。たとえば、matlab.unittest.qualifications 名前空間の assertThat、assumeThat、fatalAssertThat、および verifyThat の検定メソッドと共に使用できます。また、BooleanConstraint オブジェクトを打ち消したり、他の BooleanConstraint オブジェクトと組み合わせたりすることができます。

クラスの属性

抽象型
true

クラス属性の詳細については、クラスの属性を参照してください。

メソッド

すべて展開する

例

すべて折りたたむ

HasElementCount や HasLength などの一部の組み込みの制約は BooleanConstraint のサブクラスです。テストでこれらの制約の組み合わせおよび打ち消しを行うことができます。

最初に、この例で使用するクラスをインポートします。

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;

値 3 が実数であること、およびその値が 3 以上であることを検証します。

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

値 3 が 4 と等しくないことを検証します。

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

行列 [1 2 3; 4 5 6] について長さが 6 または要素数が 6 であるかどうかをテストします。or 条件の 1 つが true であるため、テストはパスします。

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

ベクトル [3 NaN 5] に NaN 値と無限値の両方があるかどうかをテストします。and 条件の 1 つが false であるためテストは失敗します。

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
        -+---------------------

値と期待される値のサイズが同じかどうかを判定する boolean 制約を作成します。

現在のフォルダー内のファイルに、matlab.unittest.constraints.BooleanConstraint から派生させた IsSameSizeAs という名前のクラスを作成し、satisfiedBy、getDiagnosticFor、および getNegativeDiagnosticFor の各メソッドを実装します。

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;

IsSameSizeAs boolean 制約を使用することで、0 から成る 5 行 5 列の行列が 1 から成る 5 行 5 列の行列と同じサイズであり、かつ 1 から成る 1 行 5 列のベクトルと同じサイズでないことを検証します。

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

バージョン履歴

R2013a で導入