Main Content

このページは前リリースの情報です。該当の英語のページはこのリリースで削除されています。

getDiagnosticFor

クラス: matlab.unittest.constraints.Constraint
パッケージ: matlab.unittest.constraints

比較した値の診断の生成

説明

diag = getDiagnosticFor(constObj,actVal) は、制約 constObj および値 actValDiagnostic オブジェクト diag を生成します。カスタム制約を作成する場合は、getDiagnosticFor メソッドを実装することにより、制約 constObj に対して値 actVal を解析し、matlab.automation.diagnostics.Diagnostic オブジェクトを返さなければなりません。

通常、テスト フレームワークは、検定エラーが発生した場合にこのメソッドを呼び出します。したがって、getDiagnosticFor では、エラー発生後のより詳細な解析を satisfiedBy メソッドよりも効率的に処理できます。

入力引数

constObj

Constraint インスタンス

actVal

比較の値

すべて展開する

指定された値と期待される値のサイズが同じかどうかを判定するカスタム制約を作成します。getDiagnosticFor メソッドを実装して、制約に対して実際の値を評価し、Diagnostic オブジェクトを提供します。

classdef HasSameSizeAs < matlab.unittest.constraints.Constraint
    
    properties(SetAccess = immutable)
        ValueWithExpectedSize
    end
    
    methods
        % Class constructor
        function constraint = HasSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end
        
        % Determine if the actual value satisfies the constraint
        function bool = satisfiedBy(constraint,actual)
            bool = constraint.sizeMatchesExpected(actual);
        end
        
        % Produce a diagnostic for the constraint
        function diag = getDiagnosticFor(constraint,actual)
            import matlab.automation.diagnostics.StringDiagnostic
            if constraint.sizeMatchesExpected(actual)
                diag = StringDiagnostic('HasSameSizeAs passed.');
            else
                diag = StringDiagnostic(sprintf(...
                    'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...
                    int2str(size(actual)),...
                    int2str(size(constraint.ValueWithExpectedSize))));
            end
        end
    end
    
    methods(Access = private)
        % Determine if the actual and expected values have the same size
        function bool = sizeMatchesExpected(constraint,actual)
            bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));
        end
    end
    
end