=encoding utf-8 =head1 NAME =begin original Test::Exception - Test exception based code =end original Test::Exception - 例外を伴うコードのテスト =head1 SYNOPSIS =begin original use Test::More tests => 5; use Test::Exception; # or if you don't need Test::More use Test::Exception tests => 5; # then... # Check that the stringified exception matches given regex throws_ok { $foo->method } qr/division by zero/, 'zero caught okay'; # Check an exception of the given class (or subclass) is thrown throws_ok { $foo->method } 'Error::Simple', 'simple error thrown'; # all Test::Exceptions subroutines are guaranteed to preserve the state # of $@ so you can do things like this after throws_ok and dies_ok like $@, 'what the stringified exception should look like'; # Check that something died - we do not care why dies_ok { $foo->method } 'expecting to die'; # Check that something did not die lives_ok { $foo->method } 'expecting to live'; # Check that a test runs without an exception lives_and { is $foo->method, 42 } 'method is 42'; # or if you don't like prototyped functions throws_ok( sub { $foo->method }, qr/division by zero/, 'zero caught okay' ); throws_ok( sub { $foo->method }, 'Error::Simple', 'simple error thrown' ); dies_ok( sub { $foo->method }, 'expecting to die' ); lives_ok( sub { $foo->method }, 'expecting to live' ); lives_and( sub { is $foo->method, 42 }, 'method is 42' ); =end original use Test::More tests => 5; use Test::Exception; # Test::Moreは特に必要でないので以下のように書けます. use Test::Exception tests => 5; # そうして.. # 文字化された例外が与えられた正規表現とマッチするかチェックする throws_ok { $foo->method } qr/division by zero/, 'zero caught okay'; # 与えられたクラス(もしくはサブクラス)が例外として投げられたかをチェックする # Check an exception of the given class (or subclass) is thrown throws_ok { $foo->method } 'Error::Simple', 'simple error thrown'; # Test::Exceptionsのすべての関数は $@の状態を保持することを保証します, # なので, throws_okと dies_okの後にこのようなことができます. like $@, 'what the stringified exception should look like'; # 何かしら dieすることをチェックする. - なぜかは気にしない dies_ok { $foo->method } 'expecting to die'; # dieしないことをチェックする lives_ok { $foo->method } 'expecting to live'; # テストが例外を発生せずに実行されることをチェックする lives_and { is $foo->method, 42 } 'method is 42'; # プロトタイプ形式を好まない場合は以下のように書けます. throws_ok( sub { $foo->method }, qr/division by zero/, 'zero caught okay' ); throws_ok( sub { $foo->method }, 'Error::Simple', 'simple error thrown' ); dies_ok( sub { $foo->method }, 'expecting to die' ); lives_ok( sub { $foo->method }, 'expecting to live' ); lives_and( sub { is $foo->method, 42 }, 'method is 42' ); =head1 DESCRIPTION =begin original This module provides a few convenience methods for testing exception based code. It is built with L and plays happily with L and friends. If you are not already familiar with L now would be the time to go take a look. You can specify the test plan when you C in the same way as C. See L for details. NOTE: Test::Exception only checks for exceptions. It will ignore other methods of stopping program execution - including exit(). If you have an exit() in evalled code Test::Exception will not catch this with any of its testing functions. =over 4 =item B Tests to see that a specific exception is thrown. throws_ok() has two forms: throws_ok BLOCK REGEX, TEST_DESCRIPTION throws_ok BLOCK CLASS, TEST_DESCRIPTION In the first form the test passes if the stringified exception matches the give regular expression. For example: throws_ok { read_file( 'unreadable' ) } qr/No file/, 'no file'; If your perl does not support C you can also pass a regex-like string, for example: throws_ok { read_file( 'unreadable' ) } '/No file/', 'no file'; The second form of throws_ok() test passes if the exception is of the same class as the one supplied, or a subclass of that class. For example: throws_ok { $foo->bar } "Error::Simple", 'simple error'; Will only pass if the C method throws an Error::Simple exception, or a subclass of an Error::Simple exception. You can get the same effect by passing an instance of the exception you want to look for. The following is equivalent to the previous example: my $SIMPLE = Error::Simple->new; throws_ok { $foo->bar } $SIMPLE, 'simple error'; Should a throws_ok() test fail it produces appropriate diagnostic messages. For example: not ok 3 - simple error # Failed test (test.t at line 48) # expecting: Error::Simple exception # found: normal exit Like all other Test::Exception functions you can avoid prototypes by passing a subroutine explicitly: throws_ok( sub {$foo->bar}, "Error::Simple", 'simple error' ); A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any). A description of the exception being checked is used if no optional test description is passed. NOTE: Rememeber when you C perl will automatically add the current script line number, input line number and a newline. This will form part of the string that throws_ok regular expressions match against. =item B Checks that a piece of code dies, rather than returning normally. For example: sub div { my ( $a, $b ) = @_; return $a / $b; }; dies_ok { div( 1, 0 ) } 'divide by zero detected'; # or if you don't like prototypes dies_ok( sub { div( 1, 0 ) }, 'divide by zero detected' ); A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any). Remember: This test will pass if the code dies for any reason. If you care about the reason it might be more sensible to write a more specific test using throws_ok(). The test description is optional, but recommended. =item B Checks that a piece of code doesn't die. This allows your test script to continue, rather than aborting if you get an unexpected exception. For example: sub read_file { my $file = shift; local $/; open my $fh, '<', $file or die "open failed ($!)\n"; $file = ; return $file; }; my $file; lives_ok { $file = read_file('test.txt') } 'file read'; # or if you don't like prototypes lives_ok( sub { $file = read_file('test.txt') }, 'file read' ); Should a lives_ok() test fail it produces appropriate diagnostic messages. For example: not ok 1 - file read # Failed test (test.t at line 15) # died: open failed (No such file or directory) A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any). The test description is optional, but recommended. =item B Run a test that may throw an exception. For example, instead of doing: my $file; lives_ok { $file = read_file('answer.txt') } 'read_file worked'; is $file, "42", 'answer was 42'; You can use lives_and() like this: lives_and { is read_file('answer.txt'), "42" } 'answer is 42'; # or if you don't like prototypes lives_and(sub {is read_file('answer.txt'), "42"}, 'answer is 42'); Which is the same as doing is read_file('answer.txt'), "42\n", 'answer is 42'; unless C dies, in which case you get the same kind of error as lives_ok() not ok 1 - answer is 42 # Failed test (test.t at line 15) # died: open failed (No such file or directory) A true value is returned if the test succeeds, false otherwise. On exit $@ is guaranteed to be the cause of death (if any). The test description is optional, but recommended. =back =end original このモジュールは例外を伴うコードのテストのために, 少数の有用なメソッドを提供します. このモジュールは Lを用いて作成されており, Lやその他の類似のモジュールと相性が良いです. Test::Moreに慣れ親しんでいない場合, 今が調べる機会になるでしょう. Cした場合のテスト数の指定は Cした場合と同じです. 詳しくは Lを参照してください. 注記: Test::Exceptionは例外のみをチェックします. exit()を含むようなプログラムを停止するようなメソッドは対象外です. もし評価するコードに exit()があれば, Test::Exceptionのいかなる関数もこれを補足することはできません. =over 4 =item B 特定の例外が投げられることをテストします. throws_ok()には二つの形式があります. throws_ok BLOCK REGEX, TEST_DESCRIPTION throws_ok BLOCK CLASS, TEST_DESCRIPTION 最初の形式は文字列化された例外が与えられた正規表現にマッチした場合にテストがパスします. 例えば. throws_ok { read_file( 'unreadable' ) } qr/No file/, 'no file'; あなたの使う perlが Cをサポートしていなければ, 正規表現風の文字列で書くこともできます. 例えば. throws_ok { read_file( 'unreadable' ) } '/No file/', 'no file'; throws_ok()の二つ目の形式は例外が与えられたクラスと同じかサブクラスであればテストがパスします. 例えば. throws_ok { $foo->bar } "Error::Simple", 'simple error'; Cメソッドが例外 Error::Simpleもしくは Error::Simpleのサブクラスを投げるときのみパスとなります. 調べたい例外のインスタンスを渡すことで同様の効果を得ることができます. 以下のコードは先ほどの例と等価です. my $SIMPLE = Error::Simple->new; throws_ok { $foo->bar } $SIMPLE, 'simple error'; throws_ok()テストが失敗した場合は, 適切な診断メッセージが出力されます. 例えば: not ok 3 - simple error # Failed test (test.t at line 48) # expecting: Error::Simple exception # found: normal exit 他のすべての Test::Exceptionの関数と同様にプロトタイプ形式を利用せずに関数に引数を明示的に渡すことができます. throws_ok( sub {$foo->bar}, "Error::Simple", 'simple error' ); テストが成功した場合は Trueが返り, その他は Falseが返ります. テスト終了時 $@は dieした原因であることが保証します. (もし $@に格納されるものがあれば) もしオプショナルであるテストの説明が引数に渡されなければ, チェックされる例外の説明が使われます. 注記: 改行を含まない文字列で dieした場合, perlは自動的に現在の行数を追加し, 行数と 改行を挿入します. これによって構築された文字列と throws_okの引数である正規表現との マッチが行われます. =item B コードが正常にリターンするのではなく, dieするかをチェックします. 例えば sub div { my ( $a, $b ) = @_; return $a / $b; }; dies_ok { div( 1, 0 ) } 'divide by zero detected'; # or if you don't like prototypes dies_ok( sub { div( 1, 0 ) }, 'divide by zero detected' ); テストが成功した場合は Trueが返り, その他は Falseが返ります. その他は Falseが返ります. テスト終了時 $@は dieした原因であることが保証します. (もし $@に格納されるものがあれば) 覚書: このテストは何らかの理由でコードが dieしたらパスします. もし dieの原因についても気にするのであれば, throws_ok()を使ってより具体的なテストを記述することができます. テストの説明はオプショナルですが, 書くことを推奨します. =item B コードが dieしないことをチェックします. 予期せぬ例外でアボートせず, テストスクリプトが走行しつづけることを意味します. 例えば, sub read_file { my $file = shift; local $/; open my $fh, '<', $file or die "open failed ($!)\n"; $file = ; return $file; }; my $file; lives_ok { $file = read_file('test.txt') } 'file read'; # プロトタイプ形式を好まない場合は以下のようにも書けます. lives_ok( sub { $file = read_file('test.txt') }, 'file read' ); lives_ok()テストが失敗した場合は, 適切な診断メッセージが出力されます. 例えば: not ok 1 - file read # Failed test (test.t at line 15) # died: open failed (No such file or directory) テストが成功した場合は Trueが返り, その他は Falseが返ります. テスト終了時 $@は dieした原因であることが保証します. (もし $@に格納されるものがあれば) テストの説明はオプショナルですが, 書くことを推奨します. =item B 例外を投げるテストの実行. 例えば, このようなことをする場合. my $file; lives_ok { $file = read_file('answer.txt') } 'read_file worked'; is $file, "42", 'answer was 42'; lives_and()を使う場合はこのようになります. lives_and { is read_file('answer.txt'), "42" } 'answer is 42'; # プロトタイプ形式を好まない場合は以下のようにも書けます. lives_and(sub {is read_file('answer.txt'), "42"}, 'answer is 42'); これは以下のようにすることと同じです. is read_file('answer.txt'), "42\n", 'answer is 42'; Cが dieしなければ, lives_okと同様の種類のエラーを受け取ります. not ok 1 - answer is 42 # Failed test (test.t at line 15) # died: open failed (No such file or directory) テストが成功した場合は Trueが返り, その他は Falseが返ります. テスト終了時 $@は dieした原因であることが保証します. (もし $@に格納されるものがあれば) テストの説明はオプショナルですが, 書くことを推奨します. =back =head1 SKIPPING TEST::EXCEPTION TESTS =begin original Sometimes we want to use Test::Exception tests in a test suite, but don't want to force the user to have Test::Exception installed. One way to do this is to skip the tests if Test::Exception is absent. You can do this with code something like this: use strict; use warnings; use Test::More; BEGIN { eval "use Test::Exception"; plan skip_all => "Test::Exception needed" if $@; } plan tests => 2; # ... tests that need Test::Exception ... Note that we load Test::Exception in a C block ensuring that the subroutine prototypes are in place before the rest of the test script is compiled. =end original テストスイートの中で Test::Exceptionを使いたい場合がありますが, Test::Exceptionをインストールして使わせることを強制したくありません. これを行うための一つの方法に Test::Exceptionがインストールされていなければテストをスキップすることが挙げられます. このことを行うために, 以下のようにコードを書きます. use strict; use warnings; use Test::More; BEGIN { eval "use Test::Exception"; plan skip_all => "Test::Exception needed" if $@; } plan tests => 2; # ... Test::Exceptionが必要となるテスト ... Test::Exceptionを Cブロックでロードすることで, テストスクリプトの残り部分がコンパイルされるよりも前に関数プロトタイプが準備されることが保証されます. =head1 BUGS =begin original There are some edge cases in Perl's exception handling where Test::Exception will miss exceptions thrown in DESTROY blocks. See the RT bug L for details, along with the t/edge-cases.t in the distribution test suite. These will be addressed in a future Test::Exception release. If you find any more bugs please let me know by e-mail, or report the problem with L. =end original Perlの例外ハンドリングのエッジケースで, DESTROYブロックで Test::Exceptionが例外を見逃す場合があります. 詳細は RT bug Lと配布物のテストスイートに含まれる t/edge-cases.tを参照してください. これらは将来的な Test::Exceptionのリリースで解決する予定です. もしさらなるバグを見つけた場合はメールか Lにレポートしてください. =head1 COMMUNITY =begin original =over 4 =item perl-qa If you are interested in testing using Perl I recommend you visit L and join the excellent perl-qa mailing list. See L for details on how to subscribe. =item perlmonks You can find users of Test::Exception, including the module author, on L. Feel free to ask questions on Test::Exception there. =item CPAN::Forum The CPAN Forum is a web forum for discussing Perl's CPAN modules. The Test::Exception forum can be found at L. =item AnnoCPAN AnnoCPAN is a web site that allows community annotations of Perl module documentation. The Test::Exception annotations can be found at L. =back =end original =over 4 =item perl-qa もし Perlを使ったテストに興味があるのなら, L を訪問し, 素晴らしい perl-qaメーリングリストに参加することを推奨します. 購読方法については L を参照してください. =item perlmonks Lで Test::Exceptionのユーザ(モジュール作者を含む)を見つけることができるでしょう. そこでは遠慮せずに Test::Exceptionについて質問してください. =item CPAN::Forum CPANフォーラムは Perlの CPANモジュールについて議論を行うウェブフォーラムです. Test::Exceptionのフォーラムは Lになります. =item AnnoCPAN AnonCPANは Perlモジュールのドキュメントに注釈を書くことができるウェブサイトです. Test::Exceptionの注釈は Lで見ることができます. =back =head1 TO DO =begin original If you think this module should do something that it doesn't (or does something that it shouldn't) please let me know. You can see my current to do list at L, with an RSS feed of changes at L. =end original もしこのモジュールがすべきだができていないこと(もしくはすべきでないことができてしまっている)があれば, 知らせてください. 私の現在の TODOリストが L で確認でき, RSSフィードは L で確認できます. =head1 ACKNOWLEDGMENTS =begin original Thanks to chromatic and Michael G Schwern for the excellent Test::Builder, without which this module wouldn't be possible. Thanks to Adam Kennedy, Andy Lester, Aristotle Pagaltzis, Ben Prew, Cees Hek, Chris Dolan, chromatic, Curt Sampson, David Cantrell, David Golden, David Tulloh, David Wheeler, J. K. O'Brien, Janek Schleicher, Jim Keenan, Jos I. Boumans, Joshua ben Jore, Jost Krieger, Mark Fowler, Michael G Schwern, Nadim Khemir, Paul McCann, Perrin Harkins, Peter Rabbitson, Peter Scott, Ricardo Signes, Rob Muhlestein, Scott R. Godin, Steve Purkis, Steve, Tim Bunce, and various anonymous folk for comments, suggestions, bug reports and patches. =end original chromaticと Michael G Schwernの素晴らしい Test::Builderがなければ, このモジュールはできなかったことでしょう. 以下の人に感謝します. Adam Kennedy, Andy Lester, Aristotle Pagaltzis, Ben Prew, Cees Hek, Chris Dolan, chromatic, Curt Sampson, David Cantrell, David Golden, David Tulloh, David Wheeler, J. K. O'Brien, Janek Schleicher, Jim Keenan, Jos I. Boumans, Joshua ben Jore, Jost Krieger, Mark Fowler, Michael G Schwern, Nadim Khemir, Paul McCann, Perrin Harkins, Peter Rabbitson, Peter Scott, Ricardo Signes, Rob Muhlestein, Scott R. Godin, Steve Purkis, Steve, Tim Bunce, そしてコメント, 提案, バグレポート, パッチをくれた多くの人々に感謝します. =head1 AUTHOR =begin original Adrian Howard If you can spare the time, please drop me a line if you find this module useful. =end original Adrian Howard もし時間を割いてくれるのであれば, あなたが思うこのモジュールの価値をお知らせください. =head1 SEE ALSO =begin original =over 4 =item L Delicious links on Test::Exception. =item L & L Modules to help test warnings. =item L Support module for building test libraries. =item L & L Basic utilities for writing tests. =item L Overview of some of the many testing modules available on CPAN. =item L Delicious links on perl testing. =back =end original =over 4 =item L Test::Exceptionの素敵なリンク集 =item L & L 警告に関するテストをサポートするモジュール =item L テストライブラリの構築をサポートするモジュール =item L & L テストを書く際の基本ユーティリティ =item L CPANで利用できる多くのテストモジュールの概観 =item L Perlのテストの素敵なリンク集 =back =head1 LICENCE Copyright 2002-2007 Adrian Howard, All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1;