Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

matlab.unittest.constraints.IsOfClass クラス

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

値のクラスが指定のクラスであるかどうかをテスト

説明

matlab.unittest.constraints.IsOfClass クラスは、値のクラスが指定のクラスであるかどうかをテストするための制約を提供します。

IsOfClass 制約では、クラスが正確に一致するかどうかをテストします。クラス階層に含まれるかどうかをテストするには、IsInstanceOf 制約を使用します。

作成

説明

c = matlab.unittest.constraints.IsOfClass(class) は、値のクラスが class であるかどうかをテストするための制約を作成し、Class プロパティを設定します。この制約は、値のクラスが class である場合に満たされます。値が class からの派生である場合は満たされません。

プロパティ

すべて展開する

期待されるクラス。文字ベクトルとして返されます。このプロパティの値は、制約の作成時に string スカラー、文字ベクトル、または meta.class インスタンスとして指定します。

属性:

GetAccess
public
SetAccess
private

すべて折りたたむ

IsOfClass 制約を使用して、数値をテストします。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

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

testCase = TestCase.forInteractiveUse;

数値 5 のクラスが double であることを検証します。

testCase.verifyThat(5,IsOfClass("double"))
Verification passed.

string の代わりに meta.class インスタンスを使用して、このテストをもう一度実行します。

testCase.verifyThat(5,IsOfClass(?double))
Verification passed.

0 が logical 値でないことを検証します。

testCase.verifyThat(0,~IsOfClass("logical"))
Verification passed.

IsOfClass 制約を使用して、関数ハンドルをテストします。

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

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

testCase = TestCase.forInteractiveUse;

@sin が関数ハンドルであることを検証します。

testCase.verifyThat(@sin,IsOfClass(?function_handle))
Verification passed.

関数名 "sin" を使用して、テストをもう一度実行します。テストは失敗します。

testCase.verifyThat("sin",IsOfClass(?function_handle))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsOfClass failed.
    --> The value's class is incorrect.
        
        Actual Class:
            string
        Expected Class:
            function_handle
    
    Actual Value:
        "sin"

IsOfClass 制約を使用して、派生クラスのインスタンスをテストします。

現在のフォルダー内のファイルに、ExampleHandle ハンドル クラスを作成します。

classdef ExampleHandle < handle
    properties
        Number = 1;
    end
end

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

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

testCase = TestCase.forInteractiveUse;

IsOfClass 制約を使用して、ExampleHandle オブジェクトをテストします。テストはパスします。

actual = ExampleHandle;
testCase.verifyThat(actual,IsOfClass(?ExampleHandle))
Verification passed.

ハンドル クラスを使用して、テストをもう一度実行します。actual はハンドル クラスから派生していますが、handleactual の正確なクラスでないため、テストは失敗します。

testCase.verifyThat(actual,IsOfClass(?handle))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsOfClass failed.
    --> The value's class is incorrect.
        
        Actual Class:
            ExampleHandle
        Expected Class:
            handle
    
    Actual Value:
      ExampleHandle with properties:
    
        Number: 1

IsOfClass 制約を使用して、関数の出力をテストします。

現在のフォルダー内のファイルに、関数 add5 を作成します。この関数は、数値入力を受け入れて 5 だけインクリメントします。

function y = add5(x)
% add5 - Increment input by 5
if ~isa(x,"numeric")
    error("add5:InputMustBeNumeric","Input must be numeric.")
end
y = x + 5;
end

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsOfClass

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

testCase = TestCase.forInteractiveUse;

有効な入力を指定して関数を呼び出します。その後、戻り値のクラスが double であるかどうかをテストします。テストはパスします。

actual = add5(1);
testCase.verifyThat(actual,IsOfClass(?double))
Verification passed.

バージョン履歴

R2013a で導入