Main Content

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

satisfiedBy

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

値が制約を満たすかどうかの判別

説明

TF = satisfiedBy(constObj,actVal) は、actVal が制約 constObj を満たすかどうかを判別します。このメソッドは、制約が満たされる場合は true (logical 1)、制約が満たされない場合は false (logical 0) を返します。カスタム制約を作成する場合は、このメソッドに比較ロジックを配置しなければなりません。

一般的に、satisfiedBy メソッドはテストにパスするケースに使用されます。したがって、エラー発生後のより詳細な解析は、getDiagnosticFor メソッドなどの他のメソッドを使用した方が効率的に処理できます。

入力引数

constObj

Constraint インスタンス

actVal

制約に対して評価する値

すべて展開する

指定された値と期待される値のサイズが同じかどうかを判定するカスタム制約を作成します。satisfiedBy メソッドに比較ロジックを含めます。

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