メインコンテンツ

カスタム制約の作成

R2026b

この例では、指定された値と期待される値のサイズが同じかどうかを判定するカスタムの制約を作成する方法を示します。

現在のフォルダー内のファイルに、matlab.unittest.constraints.Constraint クラスから派生する IsSameSizeAs という名前のクラスを作成します。クラス コンストラクターは期待値を受け入れ、そのサイズが実際の値のサイズと比較されます。期待値は ValueWithExpectedSize プロパティに格納されます。Constraint の実装を変更不可にすることが推奨されるため、プロパティの SetAccess 属性を immutable に設定します。

classdef IsSameSizeAs < matlab.unittest.constraints.Constraint
    properties (SetAccess=immutable)
        ValueWithExpectedSize
    end
    
    methods
        function constraint = IsSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end
    end 
end

private アクセスが指定された methods ブロックで、実際の値と期待される値が同じサイズであるかどうかを判定する補助メソッド sizeMatchesExpected を定義します。このメソッドは他の制約メソッドによって呼び出されます。

    methods (Access=private)
        function tf = sizeMatchesExpected(constraint,actual)
            tf = isequal(size(actual), ...
                size(constraint.ValueWithExpectedSize));
        end
    end

matlab.unittest.constraints.Constraint クラスから派生するクラスで satisfiedBy メソッドを実装する必要があります。このメソッドは、比較ロジックを含み、logical 値を返さなければなりません。methods ブロック内で、補助メソッドを呼び出すことにより satisfiedBy を実装します。実際のサイズと想定サイズが等しい場合、このメソッドは true を返します。

    methods
        function tf = satisfiedBy(constraint,actual)
            tf = constraint.sizeMatchesExpected(actual);
        end
    end

matlab.unittest.constraints.Constraint クラスから派生するクラスで getDiagnosticFor メソッドも実装する必要があります。このメソッドは、制約に対して実際の値を評価し、Diagnostic オブジェクトを提供しなければなりません。この例では、getDiagnosticFor は ConstraintDiagnostic オブジェクトを返します。matlab.unittest.constraints.Constraint クラスの便利なメソッド createPassingDiagnostic および createFailingDiagnostic は、制約の書式設定済みの診断を返します。 (R2026b 以降)

    methods
        function diagnostic = getDiagnosticFor(constraint,actual)
            if constraint.sizeMatchesExpected(actual)
                diagnostic = constraint.createPassingDiagnostic;
            else
                diagnostic = constraint.createFailingDiagnostic( ...
                    ActualValue=size(actual), ...
                    ExpectedValue=size(constraint.ValueWithExpectedSize));
                diagnostic.ActValHeader = "Actual Size:";
                diagnostic.ExpValHeader = "Expected Size:";
            end
        end
    end

IsSameSizeAs クラスの定義

以下は、IsSameSizeAs クラスの完全なコードです。

classdef IsSameSizeAs < matlab.unittest.constraints.Constraint
    properties (SetAccess=immutable)
        ValueWithExpectedSize
    end

    methods
        function constraint = IsSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end

        function tf = satisfiedBy(constraint,actual)
            tf = constraint.sizeMatchesExpected(actual);
        end

        function diagnostic = getDiagnosticFor(constraint,actual)
            if constraint.sizeMatchesExpected(actual)
                diagnostic = constraint.createPassingDiagnostic;
            else
                diagnostic = constraint.createFailingDiagnostic( ...
                    ActualValue=size(actual), ...
                    ExpectedValue=size(constraint.ValueWithExpectedSize));
                diagnostic.ActValHeader = "Actual Size:";
                diagnostic.ExpValHeader = "Expected Size:";
            end
        end
    end

    methods (Access=private)
        function tf = sizeMatchesExpected(constraint,actual)
            tf = isequal(size(actual), ...
                size(constraint.ValueWithExpectedSize));
        end
    end
end

期待されるサイズのテスト

コマンド プロンプトで、対話型テスト用にテスト ケースを作成します。

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

パスのケースをテストします。

testCase.verifyThat(zeros(5),IsSameSizeAs(repmat(1,5)))
Verification passed.

失敗のケースをテストします。

testCase.verifyThat(zeros(5),IsSameSizeAs(ones(1,5)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSameSizeAs failed.
    
    Actual Size:
         5     5
    Expected Size:
         1     5

参考

クラス

トピック