2007/03/30

Gaucheの単体テストで、副作用により標準出力に書き出す処理(displayとか)の動作をテストしたい。つまり、こんな単体テストをしたい。
(test* "display test"
"foobar string"
(display "foobar string"))
もちろんこれは失敗する。関数 test は equal? で第2引数と第3引数を比較するだけだから。オプション引数を与えて比較に使うプロシージャを変更することもできるけど、そもそも上記のような display のテストでは第3引数を評価した値が # でしかないので、テストの意味をなさない。

display の動作を脳内シミュレートすると、こんなふうに出力ポートを曲げればうまくいきそう。
(test* "display test"
"foobar string"
(with-output-to-string (lambda () (display "foobar string"))))
どうやらうまくいく。あとはこんなマクロをでっちあげておけばうれしい。
(define-syntax test-with-output*
(syntax-rules ()
((_ e1 e2 e3)
(test* e1 e2 (with-output-to-string (lambda () e3))))
((_ e1 e2 e3 e4)
(test* e1 e2 (with-output-to-string (lambda () e3)) e4))))
結果。
gosh >(test-with-output* "display test"
"foobar string"
(display "foobar string"))

test global conversion 2, expects "foobar string" ==> ok
#<undef>

0 件のコメント: