カスタム boolean 制約の作成
この例では、指定された値と期待される値のサイズが同じかどうかを判定するカスタムの boolean 制約を作成する方法を示します。
現在のフォルダー内のファイルに、matlab.unittest.constraints.BooleanConstraint クラスから派生する IsSameSizeAs という名前のクラスを作成します。クラス コンストラクターは期待値を受け入れ、そのサイズが実際の値のサイズと比較されます。期待値は ValueWithExpectedSize プロパティに格納されます。BooleanConstraint の実装を変更不可にすることが推奨されるため、プロパティの SetAccess 属性を immutable に設定します。
classdef IsSameSizeAs < matlab.unittest.constraints.BooleanConstraint 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.BooleanConstraint クラスは matlab.unittest.constraints.Constraint クラスのサブクラスです。したがって、BooleanConstraint クラスから派生するクラスは、Constraint クラスのメソッドを実装しなければなりません。methods ブロック内に satisfiedBy メソッドと getDiagnosticFor メソッドを実装します。satisfiedBy 実装は比較ロジックを含み、logical 値を返さなければなりません。getDiagnosticFor 実装は、制約に対して実際の値を評価し、Diagnostic オブジェクトを提供しなければなりません。この例では、getDiagnosticFor は StringDiagnostic オブジェクトを返します。
methods function tf = satisfiedBy(constraint,actual) tf = constraint.sizeMatchesExpected(actual); end 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
BooleanConstraint から派生したクラスは、getNegativeDiagnosticFor メソッドを実装しなければなりません。制約が打ち消された場合、このメソッドは Diagnostic オブジェクトを提供しなければなりません。protected アクセスが指定された methods ブロック内で getNegativeDiagnosticFor を実装します。
methods (Access=protected) 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
必要なメソッドを実装することで、制約は適切な and、or、および not のオーバーロードを継承し、他の BooleanConstraint オブジェクトとの組み合わせや打ち消しができるようになります。
IsSameSizeAs クラスの定義
以下は、IsSameSizeAs クラスの完全なコードです。
classdef IsSameSizeAs < matlab.unittest.constraints.BooleanConstraint 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) 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) 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) function tf = sizeMatchesExpected(constraint,actual) tf = isequal(size(actual), ... size(constraint.ValueWithExpectedSize)); end end end
期待されるサイズのテスト
コマンド プロンプトで、対話型テスト用にテスト ケースを作成します。
import matlab.unittest.TestCase import matlab.unittest.constraints.HasLength testCase = TestCase.forInteractiveUse;
パスのケースをテストします。or 条件の 1 つ、HasLength(5) が true であるためテストはパスします。
testCase.verifyThat(zeros(5),HasLength(5) | ~IsSameSizeAs(repmat(1,5)))
Verification passed.
失敗のケースをテストします。and 条件の 1 つ、~IsSameSizeAs(repmat(1,5)) が false であるためテストは失敗します。
testCase.verifyThat(zeros(5),HasLength(5) & ~IsSameSizeAs(repmat(1,5)))
Verification failed.
---------------------
Framework Diagnostic:
---------------------
AndConstraint failed.
--> + [First Condition]:
| HasLength passed.
|
| Actual Value:
| 0 0 0 0 0
| 0 0 0 0 0
| 0 0 0 0 0
| 0 0 0 0 0
| 0 0 0 0 0
| Expected Length:
| 5
--> AND
+ [Second Condition]:
| Negated ISameSizeAs failed.
| Actual and expected sizes were the same ([5 5]) but should not have been.
-+---------------------
参考
クラス
matlab.unittest.constraints.BooleanConstraint|matlab.unittest.constraints.Constraint|matlab.automation.diagnostics.StringDiagnostic|matlab.automation.diagnostics.Diagnostic