Main Content

matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues

クラス: matlab.unittest.constraints.PublicPropertyComparator
名前空間: matlab.unittest.constraints

再帰ですべての値をサポートする、パブリック プロパティの比較演算子

説明

c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues は、MATLAB® オブジェクト配列のパブリック プロパティの比較演算子を作成します。この比較演算子では、再帰的に演算が行われ、パブリック プロパティに格納されたすべてのデータ型がサポートされます。

c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues(Name,Value) は、1 つ以上の名前と値の引数を使用して追加のオプションを設定します。たとえば、c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues("IgnoringCase",true) は大文字と小文字を区別しない比較演算子を作成します。

入力引数

すべて展開する

名前と値の引数

引数のオプションのペアを Name1=Value1,...,NameN=ValueN として指定します。ここで Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後になければなりませんが、ペアの順序は重要ではありません。

例: c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues(IgnoringCase=true)

R2021a より前では、コンマを使用してそれぞれの名前と値を区切り、Name を引用符で囲みます。

例: c = matlab.unittest.constraints.PublicPropertyComparator.supportingAllValues("IgnoringCase",true)

テキスト値を比較するときに大文字小文字の区別を無視するかどうか。数値または logical 0 (false) または 1 (true) として指定します。既定では、比較演算子で大文字と小文字が区別されます。

テキスト値を比較するときに空白を無視するかどうか。数値または logical 0 (false) または 1 (true) として指定します。既定では、比較演算子で空白文字が区別されます。空白文字は、スペース (' ')、フォーム フィード ('\f')、改行 ('\n')、キャリッジ リターン ('\r')、水平タブ ('\t')、垂直タブ ('\v') です。

構造体配列を比較するときに無視するフィールド。string 配列または文字ベクトルの cell 配列として指定します。指定したフィールドの値は比較演算子で比較されません。

例: "IgnoringFields","field1"

MATLAB オブジェクト配列を比較するときに無視するプロパティ。string 配列または文字ベクトルの cell 配列として指定します。指定したプロパティの値は比較演算子で比較されません。

例: "IgnoringProperties","Property1"

比較時に使用する許容誤差。matlab.unittest.constraints.Tolerance オブジェクトとして指定します。

例: "Within",matlab.unittest.constraints.AbsoluteTolerance(1)

例: "Within",matlab.unittest.constraints.AbsoluteTolerance(1) | matlab.unittest.constraints.RelativeTolerance(0.1)

属性

Statictrue

メソッドの属性の詳細については、メソッドの属性を参照してください。

すべて展開する

すべてのデータ型をサポートする比較演算子を使用して、2 つのオブジェクトのパブリック プロパティを比較します。

現在のフォルダー内の Student.m という名前のファイルに、Student クラスを作成します。このクラスには、2 つのパブリック プロパティと 1 つのプライベート プロパティがあります。

classdef Student
    properties (SetAccess=immutable)
        Name
        Age
    end
    properties (Access=private)
        Field
    end
    methods
        function obj = Student(name,age,field)
            arguments
                name = "";
                age = [];
                field = "";
            end
            obj.Name = name;
            obj.Age = age;
            obj.Field = field;
        end
    end
end

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

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

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

testCase = TestCase.forInteractiveUse;

2 つの Student オブジェクトを作成し、それらを IsEqualTo 制約を使用して比較します。この例では、プライベート プロパティの値が異なるためテストが失敗します。

s1 = Student("Mary Jones",20,"physics");
s2 = Student("Mary Jones",20,"biology");
testCase.verifyThat(s1,IsEqualTo(s2))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> ObjectComparator failed.
        --> The objects are not equal using "isequaln".
        
        Actual Value:
          Student with properties:
        
            Name: "Mary Jones"
             Age: 20
        Expected Value:
          Student with properties:
        
            Name: "Mary Jones"
             Age: 20
    ------------------
    Stack Information:
    ------------------
    In C:\work\ComparePublicPropertiesExample.m (ComparePublicPropertiesExample) at 29

PublicPropertyComparator インスタンスを使用して、このテストをもう一度実行します。Name プロパティと Age プロパティで型が異なるため、すべてのデータ型をサポートする比較演算子を使用して比較を実行します。s1.Fields2.Field の値は異なりますが、この比較演算子で調べられるのは s1s2 のパブリック プロパティのみであるため、テストにパスします。

testCase.verifyThat(s1,IsEqualTo(s2, ...
    "Using",PublicPropertyComparator.supportingAllValues))
Verification passed.

新しい Student オブジェクトを作成し、それを s1 と比較します。s1.Names3.Name の値が異なるため、テストは失敗します。

s3 = Student("mary jones",20,"chemistry");
testCase.verifyThat(s1,IsEqualTo(s3, ...
    "Using",PublicPropertyComparator.supportingAllValues))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>.Name
        --> StringComparator failed.
            --> The strings are not equal.
            
            Actual Value:
                "Mary Jones"
            Expected Value:
                "mary jones"
    
    Actual Value:
      Student with properties:
    
        Name: "Mary Jones"
         Age: 20
    Expected Value:
      Student with properties:
    
        Name: "mary jones"
         Age: 20
    ------------------
    Stack Information:
    ------------------
    In C:\work\ComparePublicPropertiesExample.m (ComparePublicPropertiesExample) at 44

テストにパスするように、大文字小文字の区別を無視する比較演算子を使用します。

testCase.verifyThat(s1,IsEqualTo(s3, ...
    "Using",PublicPropertyComparator.supportingAllValues( ...
    "IgnoringCase",true)))
Verification passed.

代わりに、比較時に Name プロパティを無視するように比較演算子に指示することもできます。

testCase.verifyThat(s1,IsEqualTo(s3, ...
    "Using",PublicPropertyComparator.supportingAllValues( ...
    "IgnoringProperties","Name")))
Verification passed.

制限

  • PublicPropertyComparator クラスでは、関数 subsrefnumel、または properties をオーバーロードするオブジェクトのパブリック プロパティはサポートされません。

バージョン履歴

R2014a で導入