Rails3で自分専用のブログを作る(17) ─ featureを日本語化する

Cucumberのfeatureを日本語で記述したいので、web_steps.rbを日本語化するweb_ja_steps.rbを作成しました。翻訳はid:moroさんのmoro-misoを参考にしました。

# coding: UTF-8
Given /^"([^\"]*)"ページを表示している$/ do |page_name|
  Given /^I am on #{page_name}$/
end

When /^"([^\"]*)"ページを表示する$/ do |page_name|
  When /^(?:|I )go to #{page_name}$/
end

When /^(?:"([^\"]*)"の)?"([^\"]*)"ボタンをクリックする$/ do |selector ,button|
  When /^(?:|I )press "#{button}"(?: within "#{selector}")?$/
end

When /^(?:"([^\"]*)"の)?"([^\"]*)"リンクをクリックする$/ do |selector, link|
  When /^(?:|I )follow "#{link}"(?: within "#{selector}")?$/
end

When /^(?:"([^\"]*)"の)?"([^\"]*)"に"([^\"]*)"と入力する$/ do |selector, field, value|
  When /^(?:|I )fill in "#{field}" with "#{value}"(?: within "#{selector}")?$/
end

#When /^(?:|I )fill in "([^"]*)" for "([^"]*)"(?: within "([^"]*)")?$/ do |value, field, selector|
#  with_scope(selector) do
#    fill_in(field, :with => value)
#  end
#end

# Use this to fill in an entire form with data from a table. Example:
#
#   When I fill in the following:
#     | Account Number | 5002       |
#     | Expiry date    | 2009-11-01 |
#     | Note           | Nice guy   |
#     | Wants Email?   |            |
#
# TODO: Add support for checkbox, select og option
# based on naming conventions.
#
When /^(?:"([^\"]*)"の中に)?以下の項目を入力する:$/ do |selector, fields|
  When /^(?:|I )fill in the following(?: within "#{selector}")?:$/
end

When /^(?:"([^\"]*)"の)?"([^\"]*)"から"([^\"]*)"を選択する$/ do |selector, field, value|
  When /^(?:|I )select "#{value}" from "#{field}"(?: within "#{selector}")?$/
end

When /^(?:"([^\"]*)"の)?"([^\"]*)"をチェックする$/ do |selector, field|
  When /^(?:|I )check "#{field}"(?: within "#{selector}")?$/
end

When /^(?:"([^\"]*)"の中の)?"([^\"]*)"のチェックを外す$/ do |selector, field|
  When /^(?:|I )uncheck "#{field}"(?: within "#{selector}")?$/
end

When /^(?:"([^\"]*)"の中の)?"([^\"]*)"を選択する$/ do |selector, field|
  When /^(?:|I )choose "#{field}"(?: within "#{selector}")?$/
end

When /^(?:"([^\"]*)"の中の)?"([^\"]*)"としてファイル"([^\"]*)"を選択する$/ do |selector, field, path|
  When /^(?:|I )attach the file "#{path}" to "#{field}"(?: within "selector")?$/
end

Then /^次のJSONがえられること:$/ do |expected_json|
  Then /^(?:|I )should see JSON:$/
end

Then /^(?:"([^\"]*)"に)?"([^\"]*)"と表示されていること$/ do |selector, text|
  Then /^(?:|I )should see "#{text}"(?: within "#{selector}")?$/
end

Then /^(?:"([^\"]*)"に)?\/([^\/]*)\/と表示されていること$/ do |selector, regexp|
  Then /^(?:|I )should see \/#{regexp}\/(?: within "#{scope}")?$/
end

Then /^(?:"([^\"]*)"に)?"([^\"]*)"と表示されていないこと$/ do |selector, text|
  Then /^(?:|I )should not see "#{text}"(?: within "#{selector}")?$/
end

Then /^(?:"([^\"]*)"に)?\/([^\/]*)\/と表示されていないこと$/ do |selector, regexp|
  Then /^(?:|I )should not see \/#{regexp}\/(?: within "#{selector}")?$/
end

Then /^(?:"([^\"]*)"の中の)?入力項目"([^\"]*)"に"([^\"]*)"と表示されていること$/ do |selector, field, value|
  Then /^the "#{field}" field(?: within "#{selector}")? should contain "#{value}"$/
end

Then /^(?:"([^\"]*)"の中の)?入力項目"([^\"]*)"に"([^\"]*)"と表示されていないこと$/ do |selector, field, value|
  Then /^the "#{field}" field(?: within "#{selector}")? should not contain "#{value}"$/
end

Then /^(?:"([^\"]*)"の中の)?"([^\"]*)"がチェックされていること$/ do |selector, label|
  Then /^the "#{label}" checkbox(?: within "#{selector}")? should be checked$/
end

Then /^(?:"([^\"]*)"の中の)?"([^\"]*)"がチェックされていないこと$/ do |selector, label|
  Then /^the "#{label}" checkbox(?: within "#{selector}")? should not be checked$/
end

Then /^"([^\"]*)"ページを表示していること$/ do |page_name|
  Then /^(?:|I )should be on #{page_name}$/
end

Then /^次のクエリストリングが得られること:$/ do |expected_pairs|
  Then /^(?:|I )should have the following query string:$/
end

Then /^ページを表示する$/ do
  Then /^show me the page$/
end

また、featureジェネレータが作成したarticles_steps.rbにも追記を行いました。

Given /^次の記事があるとき:$/ do |articles|
  Given /^the following articles:$/
end

When /^(\d+)番目の記事を削除する$/ do |pos|
  When /^I delete the #{pos}(?:st|nd|rd|th) article$/
end

Then /^次の記事が表示されること:$/ do |expected_articles_table|
  Then /^I should see the following articles:$/
end

これでfeatureも日本語化できます。manage_articles_ja.featureというファイル名にしました。

# language: ja
フィーチャ: 記事を管理する
  [ゴール]に到達するために
  [ステークホルダ]は
  [振る舞い]が欲しい

  シナリオ: 新しい記事を登録する
    "前提"    "the new article page"ページを表示している
    "もし"    "title 1"に"Title"と入力する
    "かつ"    "body 1"に"Body"と入力する
    "かつ"    "Create"ボタンをクリックする
    "ならば"  "title 1"と表示されていること
    "かつ"    "body 1"と表示されていること

  シナリオ: 記事を削除する
    "前提"    次の記事があるとき:
      |title|body|
      |title 1|body 1|
      |title 2|body 2|
      |title 3|body 3|
      |title 4|body 4|
    "もし"    3番目の記事を削除する
    "ならば"  次の記事が表示されること:
      |Title|Body|
      |title 1|body 1|
      |title 2|body 2|
      |title 4|body 4|

「前提」、「もし」、「ならば」、「かつ」などをダブルクォーテーションで括っているのは、括らないとエラーになるため。以前は引用符なしでも大丈夫だったのですが、Ruby 1.9正規表現の動作(単語の境界の検出)が変わったのが原因かもしれません。それにしても、「フィーチャ」や「シナリオ」が大丈夫なのは変ですがコロンで分離されているからでしょうか。