Main Content

matlab.unittest.constraints.IssuesWarnings クラス

名前空間: matlab.unittest.constraints
スーパークラス: matlab.unittest.constraints.Constraint

関数が指定された警告を発行するかどうかをテスト

説明

matlab.unittest.constraints.IssuesWarnings クラスは、関数ハンドルが指定された警告を発行するかどうかをテストするための制約を提供します。

matlab.unittest.constraints.IssuesWarnings クラスは handle クラスです。

作成

説明

c = matlab.unittest.constraints.IssuesWarnings(identifiers) は、関数ハンドルが指定された識別子の警告を発行するかどうかをテストするための制約を作成します。実際の値がテスト フレームワークでの呼び出し時に指定された警告を発行する関数ハンドルであれば、制約が満たされます。既定では、警告が発生する回数、警告が発生する順序、および関数ハンドルが未指定の警告を発行するかどうかは制約で無視されます。

c = matlab.unittest.constraints.IssuesWarnings(identifiers,Name,Value) は、1 つ以上の名前と値の引数を使用してオプションを指定します。たとえば、c = matlab.unittest.constraints.IssuesWarnings(identifiers,"WhenNargoutIs",2) は、関数ハンドルが 2 つの出力引数を指定して呼び出されたときに指定された警告を発行するかどうかをテストするための制約を作成します。

入力引数

すべて展開する

警告識別子。string 配列または文字ベクトルの cell 配列として指定します。

この引数は ExpectedWarnings プロパティを設定します。

例: "MATLAB:MKDIR:DirectoryExists"

例: ["MyComponent:FirstID" "MyComponent:SecondID"]

名前と値の引数

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

例: c = matlab.unittest.constraints.IssuesWarnings(identifiers,WhenNargoutIs=2)

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

例: c = matlab.unittest.constraints.IssuesWarnings(identifiers,"WhenNargoutIs",2)

関数ハンドルを呼び出すときに制約が要求する出力数。非負の整数スカラーとして指定します。

この引数は Nargout プロパティを設定します。

データ型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

発行される警告と期待される警告を比較するときに、期待される警告のセットを考慮に入れるオプション。数値または logical 0 (false) または 1 (true) として指定します。値が true の場合、関数ハンドルが発行する警告に identifiers にリストされていないものがあると制約が満たされません。既定では、関数ハンドルによる未指定の警告の発行については制約で無視されます。

この引数は RespectSet プロパティを設定します。

発行される警告と期待される警告を比較するときに回数を考慮に入れるオプション。数値または logical 0 (false) または 1 (true) として指定します。値が true の場合、関数ハンドルが期待される警告を発行する回数と identifiers にリストされている警告の回数が異なっていると制約が満たされません。既定では、警告が発生する回数は制約で無視されます。

この引数は RespectCount プロパティを設定します。

発行される警告と期待される警告を比較するときに順序を考慮に入れるオプション。数値または logical 0 (false) または 1 (true) として指定します。値が true の場合、関数ハンドルが警告を発行する順序と identifiers にリストされているそれらの順序が一致しないと制約が満たされません。既定では、警告が発生する順序は制約で無視されます。

制約では、与えられた警告プロファイルを重複した隣接警告のないプロファイルに調整することで順序を決定します。たとえば、順序についてテストするときは、警告プロファイル {id1,id1,id2,id3,id3,id3,id1,id1}{id1,id2,id3,id1} になります。また、順序についてテストするときは、identifiers にリストされていない警告の発行については制約で無視されます。

この引数は RespectOrder プロパティを設定します。

発行される警告と期待される警告を比較するときに完全に一致するかをテストするオプション。数値または logical 0 (false) または 1 (true) として指定します。値が true の場合、発行される警告と期待される警告のプロファイルが完全に同じでないと制約が満たされません。既定では、厳密な比較は制約で実行されません。

この引数は Exact プロパティを設定します。

プロパティ

すべて展開する

期待される警告識別子。文字ベクトルの cell 配列として返されます。このプロパティの値は、制約の作成時に指定します。

このプロパティは入力引数 identifiers によって設定されます。

属性:

GetAccess
public
SetAccess
immutable

テスト フレームワークによって呼び出されたときに関数ハンドルが生成した出力。cell 配列として返されます。

属性:

GetAccess
public
SetAccess
private

関数ハンドルを呼び出すときに制約が要求した出力数。非負の整数スカラーとして返されます。

このプロパティは名前と値の引数 WhenNargoutIs によって設定されます。

属性:

GetAccess
public
SetAccess
private

発行される警告と期待される警告を比較するときに期待される警告のセットを考慮に入れるオプション。データ型 logical0 または 1 として返されます。既定では、関数ハンドルによる未指定の警告の発行については制約で無視されます。

このプロパティは名前と値の引数 RespectingSet によって設定されます。

属性:

GetAccess
public
SetAccess
private

発行される警告と期待される警告を比較するときに回数を考慮に入れるオプション。データ型 logical0 または 1 として返されます。既定では、警告が発生する回数は制約で無視されます。

このプロパティは名前と値の引数 RespectingCount によって設定されます。

属性:

GetAccess
public
SetAccess
private

発行される警告と期待される警告を比較するときに順序を考慮に入れるオプション。データ型 logical0 または 1 として返されます。既定では、警告が発生する順序は制約で無視されます。

このプロパティは名前と値の引数 RespectingOrder によって設定されます。

属性:

GetAccess
public
SetAccess
private

発行される警告と期待される警告を比較するときに完全に一致するかをテストするオプション。データ型 logical0 または 1 として返されます。既定では、厳密な比較は制約で実行されません。

このプロパティは名前と値の引数 Exactly によって設定されます。

属性:

GetAccess
public
SetAccess
private

すべて折りたたむ

実際の値が、指定された警告を発行する関数ハンドルであるかをテストします。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IssuesWarnings

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

testCase = TestCase.forInteractiveUse;

この例では、現在のフォルダーに myFolder という名前のサブフォルダーがあると仮定しています。サブフォルダーが存在しない場合は作成します。

[~,~] = mkdir("myFolder")

関数 mkdir は、既存フォルダーの作成に使用されると警告を発行します。警告識別子が返されます。

mkdir myFolder
[~,id] = lastwarn
Warning: Directory already exists. 

id =

    'MATLAB:MKDIR:DirectoryExists'

関数 mkdir が既存フォルダーの作成に呼び出された場合に警告を発行し、その警告の識別子が "MATLAB:MKDIR:DirectoryExists" であることを検証します。

testCase.verifyThat(@() mkdir("myFolder"), ...
    IssuesWarnings("MATLAB:MKDIR:DirectoryExists"))
Verification passed.

いずれの警告も発行しない関数ハンドルをテストします。テストは失敗します。

testCase.verifyThat(@dir,IssuesWarnings("MATLAB:MKDIR:DirectoryExists"))
.         ..        myFolder  

Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IssuesWarnings failed.
    --> The function handle did not issue a correct warning profile.
        The expected warning profile ignores:
          Set
          Count
          Order
        --> The function handle did not issue any warnings.
        
        Expected Warning Profile:
            --> 'MATLAB:MKDIR:DirectoryExists'
    
    Evaluated Function:
      function_handle with value:
    
        @dir

実際の値が関数ハンドルでない場合、制約が満たされないことを検証します。

testCase.verifyThat(5,IssuesWarnings("MATLAB:MKDIR:DirectoryExists"))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IssuesWarnings failed.
    --> The value must be an instance of the expected type.
        
        Actual Class:
            double
        Expected Type:
            function_handle
    
    Actual Value:
         5

IssuesWarnings 制約を使用して、複数の警告を発行できる関数ハンドルをテストします。

現在のフォルダー内のファイルに、関数 myfun を作成します。この関数は、警告識別子の配列を入力として受け取り、指定された識別子に対応する警告を発行します。

function myfun(id)
arguments
    id {mustBeText,mustBeNonempty}
end

for i = 1:numel(id)
    warning(string(id(i)),"Warning!")
end
end

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IssuesWarnings

IssuesWarnings 制約を試すために、いくつかの警告識別子と対話型テスト用のテスト ケースを作成します。

id1 = "MyComponent:FirstID";
id2 = "MyComponent:SecondID";
id3 = "MyComponent:ThirdID";

testCase = TestCase.forInteractiveUse;

myfun が特定の識別子を指定して呼び出されたときに警告を発行することを検証します。

testCase.verifyThat(@() myfun(id1),IssuesWarnings(id1))
Verification passed.

myfun が未指定の警告を発行する場合は制約が満たされないことを検証します。

testCase.verifyThat(@() myfun(id1),IssuesWarnings(id2))
Warning: Warning! 
...

Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IssuesWarnings failed.
    --> The function handle did not issue a correct warning profile.
        The expected warning profile ignores:
          Set
          Count
          Order
        --> The function handle did not issue the correct warning(s).
            
            Missing Warning(s):
                --> 'MyComponent:SecondID'
        
        Actual Warning Profile:
            --> 'MyComponent:FirstID'
        Expected Warning Profile:
            --> 'MyComponent:SecondID'
    
    Evaluated Function:
      function_handle with value:
    
        @()myfun(id1)

関数ハンドルが複数の警告を発行する場合をテストします。myfun が発行する警告がどちらも期待される警告であるため、このテストはパスします。

testCase.verifyThat(@() myfun([id1 id3 id2 id1]), ...
    IssuesWarnings([id2 id1]))
Verification passed.

期待される警告のセットを考慮に入れて、テストをもう一度実行します。myfun が期待される警告に加えて警告 id3 を発行するため、テストは失敗します。

testCase.verifyThat(@() myfun([id1 id3 id2 id1]), ...
    IssuesWarnings([id2 id1],"RespectingSet",true))
Warning: Warning! 
...

Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IssuesWarnings failed.
    --> The function handle did not issue a correct warning profile.
        The expected warning profile respects:
          Set
        The expected warning profile ignores:
          Count
          Order
        --> The function handle did not issue the correct warning(s).
            
            Extra Warning(s):
                --> 'MyComponent:ThirdID'
        
        Actual Warning Profile:
            --> 'MyComponent:FirstID'
            --> 'MyComponent:ThirdID'
            --> 'MyComponent:SecondID'
            --> 'MyComponent:FirstID'
        Expected Warning Profile:
            --> 'MyComponent:SecondID'
            --> 'MyComponent:FirstID'
    
    Evaluated Function:
      function_handle with value:
    
        @()myfun([id1,id3,id2,id1])

警告の回数を考慮に入れて、myfun が指定された警告を発行するかどうかをテストします。関数ハンドルがそれぞれの期待される警告を期待される警告プロファイルと同じ回数だけ発行するため、テストはパスします。

testCase.verifyThat(@() myfun([id2 id1 id3 id2]), ...
    IssuesWarnings([id1 id2 id2],"RespectingCount",true))
Verification passed.

警告の順序を考慮に入れて、myfun が指定された警告を発行することを検証します。

testCase.verifyThat(@() myfun([id1 id2 id2 id3]), ...
    IssuesWarnings([id1 id2],"RespectingOrder",true))
Verification passed.

警告のセット、回数、順序を考慮に入れて、myfun が指定された警告を発行するかどうかをテストします。テストはパスします。

testCase.verifyThat(@() myfun([id1 id1 id2 id1]), ...
    IssuesWarnings([id1 id2 id1 id1], ...
    "RespectingSet",true,"RespectingCount",true,"RespectingOrder",true))
Verification passed.

警告のセット、回数、順序を考慮に入れた前のテストで関数ハンドルは制約を満たしますが、発行される警告と期待される警告のプロファイルが完全に同じではありません。完全に一致するかをテストした場合は制約は満たされません。

testCase.verifyThat(@() myfun([id1 id1 id2 id1]), ...
    IssuesWarnings([id1 id2 id1 id1],"Exactly",true))
Warning: Warning! 
...

Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IssuesWarnings failed.
    --> The function handle did not issue a correct warning profile.
        The expected warning profile must match exactly.
        --> The function handle did not issue the exact warning profile expected.
        
        Actual Warning Profile:
            --> 'MyComponent:FirstID'
            --> 'MyComponent:FirstID'
            --> 'MyComponent:SecondID'
            --> 'MyComponent:FirstID'
        Expected Warning Profile:
            --> 'MyComponent:FirstID'
            --> 'MyComponent:SecondID'
            --> 'MyComponent:FirstID'
            --> 'MyComponent:FirstID'
    
    Evaluated Function:
      function_handle with value:
    
        @()myfun([id1,id1,id2,id1])

バージョン履歴

R2013a で導入