このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
matlab.unittest.constraints.IsOfClass クラス
名前空間: matlab.unittest.constraints
スーパークラス: matlab.unittest.constraints.BooleanConstraint
値のクラスが指定のクラスであるかどうかをテスト
説明
matlab.unittest.constraints.IsOfClass
クラスは、値のクラスが指定のクラスであるかどうかをテストするための制約を提供します。
IsOfClass
制約では、クラスが正確に一致するかどうかをテストします。クラス階層に含まれるかどうかをテストするには、IsInstanceOf
制約を使用します。
作成
プロパティ
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
はハンドル クラスから派生していますが、handle
が actual
の正確なクラスでないため、テストは失敗します。
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 で導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)