blog.8-p.info

加藤和良 (1984生まれ、会社員) の個人的なブログです。

UnittestJS でクロスブラウザテスト

2009-01-04 15:27

Prototype

Prototype のテストまわりのライブラリが、12月頃 UnittestJS に置き換えられた。「置き換え」というか「既存のテストまわりに名前をつけて独立させた」というほうが正確。レポジトリも独立した

Prototype のテストを実行する

UnittestJS がどんなものかは Prototype のテストを実行してみるのがわかりやすい。

% git clone git://github.com/sstephenson/prototype.git
...
% cd prototype
% git submodule init
Submodule 'vendor/caja_builder' (git://github.com/tobie/unittest_js_caja_builder.git) registered for path 'vendor/caja_builder'
Submodule 'vendor/unittest_js' (git://github.com/tobie/unittest_js.git) registered for path 'vendor/unittest_js'
% git submodule update
Initialized empty Git repository in /Users/kzys/prototype/vendor/caja_builder/.git/
...
Submodule path 'vendor/caja_builder': checked out 'aeda517c2e82db92bc88d56ed68fa4ce05f487a9'
Initialized empty Git repository in /Users/kzys/prototype/vendor/unittest_js/.git/
...
Submodule path 'vendor/unittest_js': checked out 'd0d28f58f127796c9cf916bd4f1c95f90b9a3fdc'
% rake test
(in /Users/kzys/prototype)

Skipping Chrome, not supported on this OS.

Started tests in Firefox.
......................
Finished in 34.785718 seconds.
349 tests, 2238 assertions, 0 failures, 0 errors.

Skipping Internet Explorer, not supported on this OS.

Skipping Konqueror, not supported on this OS.

Started tests in Opera.
.....F..............F.
Finished in 15.284949 seconds.
  Failures: /tmp/dom_test.html, /tmp/selector_test.html
311 tests, 2005 assertions, 6 failures, 0 errors.

Started tests in Safari.
......................
Finished in 12.315652 seconds.
358 tests, 2299 assertions, 0 failures, 0 errors.

%

端末上はこんな感じだけど、実際に実行してみるとブラウザ (Mac だと Firefox, Opera, Safari) が順に起動して、localhost に Web サーバーが起動して、サーバーからテストが記述された HTML を読み込んで、テストの結果をサーバーに投げて、とかなりさわがしい。

そのさわがしいクロスブラウザでの JavaScript の実行が「rake test」だけでできるのは便利そうだ。

自分のコードをテストする

自分のコードのテストもこの上で実行したい、というわけで独立した UnittestJS を使ってみる。

Prototype の Rakefile をみながら、サーバーを起動する Ruby スクリプトを書く。これを a.rb とする。

require 'unittest_js'

runner = UnittestJS::WEBrickRunner::Runner.new(:test_dir => '.')
runner.add_test('a.html')
UnittestJS::Browser::SUPPORTED.each do |browser|
  runner.add_browser(browser.to_sym)
end
runner.run

a.html はこんなかんじ。成功するものをひとつ、失敗するものをひとつ書いておく。

<html>
<body>
  <div id="testlog"></div>
  <script src="assets/prototype.js" type="text/javascript"></script>
  <script src="assets/unittest.js" type="text/javascript"></script>
  <script type="text/javascript">
    new Test.Unit.Runner({
        testFoo: function(){
            this.assertEqual('foo', 'foo');
        },
        testBar: function(){
            this.assertEqual('bar', 'foo');
        }
    });
  </script>
</body>
</html>

実行してみる。

% git clone git://github.com/tobie/unittest_js.git
...
% cd unittest_js
% vi a.rb
...
% vi a.html
...
% ls
LICENSE         a.html          assets/         templates/
README          a.rb            lib/
% ruby -I lib a.rb

Skipping Chrome, not supported on this OS.

Started tests in Firefox.
F
Finished in 0.545872 seconds.
  Failures: /unittest_js/a.html
2 tests, 1 assertions, 1 failures, 0 errors.

Skipping Internet Explorer, not supported on this OS.

Skipping Konqueror, not supported on this OS.

Started tests in Opera.
F
Finished in 0.599746 seconds.
  Failures: /unittest_js/a.html
2 tests, 1 assertions, 1 failures, 0 errors.

Started tests in Safari.
F
Finished in 0.334798 seconds.
  Failures: /unittest_js/a.html
2 tests, 1 assertions, 1 failures, 0 errors.

%

2テスト x 3ブラウザが無事実行できた。

テストは Ruby に依存しません

ちなみに、Ruby とかを持ち出さず a.html をそのままブラウザで開いてもテストは実行される。この場合テスト結果が端末に出力されたりはしない。

Ruby から UnittestJS::WEBrickRunner::Runner を起動した場合、ブラウザ側では「http://localhost:4711/unittest_js/a.html?t=1231051048.106773&resultsURL=http://localhost:4711/results」のような URI が読み込まれる。unittest.js の Test.Unit.Runner は、この resultsURL が与えられた場合だけ、テスト結果をそこに投げている。

それ Selenium で

できるんでしょうか? 調べていないです。

コマンドが失敗したらプロンプトを赤くする (1)

2009-01-02 22:14

Prompt

しばらく前から、コマンドの終了ステータスをプロンプトの色に反映させるようにしている。

終了ステータスとは

C言語なら

int main(int argc, char* argv[])
{
    ...
    return 0;
}

この return している 0 が終了ステータス。この整数値の下位 8bit がプロセスの終了時に親 (そのプロセスを起動したプロセス) にわたる。

終了ステータスの指定方法はいろいろある。C言語の場合 main 関数の戻り値以外にも exit 関数の引数でも指定できる。Ruby, Perl の場合、そもそも main 関数は無くて exit 関数に引数を与える方法だけがつかえる。

この値はシェル上で $? で参照できる。

% perl -e 'exit(0xdead)'; echo $?
173
%

慣例として、正常終了のときは 0 を返すことになっている。

% ls /etc/passwd
/etc/passwd
% echo $?
0
% ls /etc/passwdddd
ls: /etc/passwdddd: No such file or directory
% echo $?
1
%

ファイルが存在して無事表示できたので 0, ファイルが存在しなかったときは 1 を返しているのがわかる。ここではこの二種類しか観測できなかったけど、例えば grep だと 2 もあります。

% echo hello | grep hello
hello
% echo $?
0
% echo hello | grep helo
% echo $?
1
% echo hello | grep
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
% echo $?
2
%

パターンにマッチする行が見つかったら 0, 見つからなければ 1, そもそもそれ以前の問題 (引数が足りない) の場合 2 を返している。

プロンプトの色を変える

やっと本題。zsh だと ~/.zshrc にこんな感じで書くと良い。

autoload -U colors
colors
PROMPT="%{$fg[green]%}%#%{$reset_color%} "

precmd () {
  PROMPT="%{%(?.$fg[green].$fg[red])%}%#%{$reset_color%} "
}

%(?.foo.bar) ってところで条件分岐しているだけです。くわしくは zshmisc (1) の “Conditional substrings” を参照。なので、色以外も切り替えられます。

PROMPT="('-')/ "
precmd () {
  PROMPT="(%(?.'-'.;_;))/ "
}

うん。

('-')/ echo hello
hello
('-')/ echoo hello
zsh: command not found: echoo
(;_;)/ echo hello
hello
('-')/

かわいすぎた。

追記

はてなブックマーク - @lukesilvia.bookmarks

RPROMPT 使っている場合は、%{$reset_color%} のしないと、RPROMPT が手前にずれる

というコメントを読んで、PROMPT 中のエスケープシーケンスを %{ … %} でかこっていない部分に気づいたので「プロンプトの色を変える」の最初の例を修正しました。

% echo -n $fg[green] | hexdump -c
0000000 033   [   3   2   m
0000005
% echo -n $fg[red] | hexdump -c
0000000 033   [   3   1   m
0000005
% echo -n $reset_color | hexdump -c
0000000 033   [   0   0   m
0000005
%

ここらへんは全部エスケープ (033) が含まれてます。

Blogging in English (4)

2009-01-02 21:33

This year, I decided to write some articles in English.

In 2007, Chris Messina wrote history of GreaseKit :

In broken English he expressed his uncertainty and so I went about hacking it myself.

Well, I see…

It’s my practice. If you find a mistake, Please notice to inform me. I think “Given enough eyeballs, all bugs are shallow“.

2009 (2)

2009-01-01 00:26

あけましておめでとうございます。今年は WordPress を使うことにしました。

2008年に書いたものは http://blog.8-p.info/2008/ からたどれます。

Powered by WordPress