Main Content

matlab.unittest.diagnostics.ConstraintDiagnostic クラス

名前空間: matlab.unittest.diagnostics
スーパークラス: matlab.automation.diagnostics.Diagnostic

制約に共通するフィールドをもつ診断

説明

matlab.unittest.diagnostics.ConstraintDiagnostic クラスは、制約に共通するフィールドをもつ診断を提供します。これらのフィールドには、説明、条件、実際の値、期待される値が含まれます。詳細については、診断フィールドを参照してください。

ConstraintDiagnostic クラスは、カスタム制約で生成される診断に共通の外観を追加するための便利な方法を提供します。ConstraintDiagnostic オブジェクトを使用して、Constraint サブクラスの getDiagnosticFor メソッドや BooleanConstraint サブクラスの getNegativeDiagnosticFor メソッドを実装できます。

matlab.unittest.diagnostics.ConstraintDiagnostic クラスは handle クラスです。

作成

説明

diag = matlab.unittest.diagnostics.ConstraintDiagnosticConstraintDiagnostic オブジェクトが作成されます。その後、このオブジェクトのプロパティを設定して制約診断をカスタマイズできます。

プロパティ

すべて展開する

次のプロパティに加え、ConstraintDiagnostic クラスは Diagnostic クラスから Artifacts プロパティと DiagnosticText プロパティを継承します。

説明

総合的な診断情報。string スカラーまたは文字ベクトルとして指定します。

属性:

GetAccess
public
SetAccess
public

Description プロパティのテキストを表示するオプション。数値または logical 0 (false) または 1 (true) として指定します。既定では、説明は制約に表示されません。

属性:

GetAccess
public
SetAccess
public
条件

書式設定された条件リスト。文字ベクトルとして返されます。フレームワークで診断を表示する際、各条件は新しい行から始まり、矢印 (-->) 区切り記号から開始されます。

条件はテストの失敗の原因に固有の情報を含み、"サブ診断" として機能します。条件は addCondition および addConditionsFrom メソッドを使用してリストに追加します。

属性:

GetAccess
public
SetAccess
private

Conditions プロパティに格納された条件リストの条件の数。非負の整数スカラーとして返されます。

属性:

GetAccess
public
SetAccess
private

データ型: double

Conditions プロパティの条件リストを表示するオプション。数値または logical 0 (false) または 1 (true) として指定します。既定では、条件リストは制約に表示されません。

属性:

GetAccess
public
SetAccess
public
実際の値

テストする実際の値。任意のデータ型の値として指定します。

属性:

GetAccess
public
SetAccess
public

実際の値のヘッダー情報。string スカラーまたは文字ベクトルとして指定します。

属性:

GetAccess
public
SetAccess
public

実際の値とそのヘッダー情報を表示するオプション。数値または logical 0 (false) または 1 (true) として指定します。既定では、実際の値は制約に表示されません。

属性:

GetAccess
public
SetAccess
public
期待される値

期待される値 (該当する場合)。任意のデータ型の値として指定します。

属性:

GetAccess
public
SetAccess
public

期待される値のヘッダー情報。string スカラーまたは文字ベクトルとして指定します。

属性:

GetAccess
public
SetAccess
public

期待される値とそのヘッダー情報を表示するオプション。数値または logical 0 (false) または 1 (true) として指定します。既定では、期待される値は制約に表示されません。

属性:

GetAccess
public
SetAccess
public

メソッド

すべて展開する

すべて折りたたむ

値が期待される値と同じサイズであるかどうかを判定するカスタム制約を作成します。制約の診断情報を生成するために、その getDiagnosticFor メソッドを ConstraintDiagnostic クラスを使用して実装します。

現在のフォルダー内のファイルに、matlab.unittest.constraints.Constraint から派生させた IsSameSizeAs という名前のクラスを作成し、satisfiedBy メソッドおよび getDiagnosticFor メソッドを実装します。getDiagnosticFor メソッドの実装には、ConstraintDiagnostic クラスのインスタンスを使用します。ConstraintDiagnostic クラスは、パスしたテストと失敗したテスト用に各種の診断フィールドをカスタマイズするための便利な方法を提供します。

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 = diagnosticForPassingTest;
            else
                diagnostic = diagnosticForFailingTest(constraint,actual);
            end
        end
    end

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

        function diag = diagnosticForPassingTest(~,~)
            import matlab.unittest.diagnostics.ConstraintDiagnostic
            diag = ConstraintDiagnostic;
            
            diag.DisplayDescription = true;
            diag.Description = "IsSameSizeAs passed.";
        end

        function diag = diagnosticForFailingTest(constraint,actual)
            import matlab.unittest.diagnostics.ConstraintDiagnostic
            diag = ConstraintDiagnostic;
            
            diag.DisplayDescription = true;
            diag.Description = "IsSameSizeAs failed.";

            diag.DisplayConditions = true;
            diag.addCondition("Sizes did not match.")

            diag.DisplayActVal = true;
            diag.ActValHeader = "Actual Size:";
            diag.ActVal = size(actual);

            diag.DisplayExpVal = true;
            diag.ExpValHeader = "Expected Size:";
            diag.ExpVal = size(constraint.ValueWithExpectedSize);
        end
    end
end

対話型テスト用にテスト ケースを作成します。

testCase = matlab.unittest.TestCase.forInteractiveUse;

失敗のケースをテストします。補助メソッド diagnosticForFailingTest で実装された制約診断がフレームワークで表示されます。

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

診断の説明フィールドの前にテキストを挿入する制約診断を作成します。

現在のフォルダー内のファイルに、matlab.unittest.diagnostics.ConstraintDiagnostic から派生させた CustomConstraintDiagnostic という名前のクラスを作成します。オプションである診断の説明フィールドの前にテキストを挿入するために、カスタム診断クラスがそのスーパークラスから継承する getPreDescriptionString メソッドを実装します。

classdef CustomConstraintDiagnostic < ...
        matlab.unittest.diagnostics.ConstraintDiagnostic
    properties (SetAccess=immutable)
        Context
    end

    methods
        function diag = CustomConstraintDiagnostic(context)
            arguments
                context (1,1) string = "Test Outcome Information"
            end
            diag.Context = context;
        end
    end

    methods (Access=protected)
        function str = getPreDescriptionString(diag)
            str = diag.Context;
        end
    end
end

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo

CustomConstraintDiagnostic クラスをインスタンス化して制約診断を作成します。

diagnostic = CustomConstraintDiagnostic;
diagnostic.DisplayDescription = true;
diagnostic.Description = "My Custom Diagnostic";

実際には、カスタム制約の実装には制約診断を使用するのが一般的です。この例では、簡略化するために、対話型テスト用のテスト ケースを作成し、テストの失敗時に CustomConstraintDiagnostic オブジェクトを使用して診断情報を表示します。指定した説明と説明の前に挿入される既定のテキストの両方がフレームワークで表示されます。

testCase = TestCase.forInteractiveUse;
testCase.verifyThat(1,IsEqualTo(2),diagnostic)
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Test Outcome Information
    My Custom Diagnostic
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> NumericComparator failed.
        --> The numeric values are not equal using "isequaln".
        --> Failure table:
                Actual    Expected    Error    RelativeError
                ______    ________    _____    _____________
                                                            
                  1          2         -1          -0.5     
        
        Actual Value:
             1
        Expected Value:
             2

詳細

すべて展開する

バージョン履歴

R2013a で導入