Skip to content

Commit

Permalink
Refactored I18n handling in spec testsuite
Browse files Browse the repository at this point in the history
* Make use of I18n.backend.reload! to clear/reinitialize translation strings
* Start with a minimal set of translation strings and add expected ones in the test
* Reinitialize the translation backend after each test to start with a known state
  • Loading branch information
mjonuschat committed May 24, 2010
1 parent f07059c commit 731b792
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 62 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Expand Up @@ -3,8 +3,9 @@
* Changed date/time inputs to default to nil instead of Time.now when the object has no value (due to deprecation warning, #240)
* Changed the behaviour of associations with a :class_name option to be more consistent with what Rails expects
* Fixed issues with Ruby 1.9.1 and Haml
* Add the :disabled option to check_boxes input
* Fixed inputs_for_nested_attributes only appending to the output buffer
* Add the :disabled option to check_boxes input
* Changed I18n handling in spec tests to use I18n.backend.reload! instead of assigning nil

0.9.8

Expand Down
75 changes: 36 additions & 39 deletions spec/commit_button_spec.rb
Expand Up @@ -140,7 +140,7 @@
end

after do
::I18n.backend.store_translations :en, :formtastic => {:submit => nil}
::I18n.backend.reload!
end

it 'should render an input with default I18n-localized label (fallback)' do
Expand All @@ -157,33 +157,34 @@
:formtastic => {
:actions => {
:submit => 'Custom Submit',
:post => {
:submit => 'Custom Submit {{model}}'
}
}
}
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
end

it 'should render an input with localized label (I18n)' do
semantic_form_for(:post, :url => 'http://example.com') do |builder|
concat(builder.commit_button)
end
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Submit Post"][@class~="submit"]})
after do
::I18n.backend.reload!
end

it 'should render an input with anoptional localized label (I18n) - if first is not set' do
it 'should render an input with localized label (I18n)' do
::I18n.backend.store_translations :en,
:formtastic => {
:actions => {
:post => {
:submit => nil
:submit => 'Custom Submit {{model}}'
}
}
}
semantic_form_for(:post, :url => 'http://example.com') do |builder|
concat(builder.commit_button)
end
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Submit Post"][@class~="submit"]})
end

it 'should render an input with anoptional localized label (I18n) - if first is not set' do
semantic_form_for(:post, :url => 'http://example.com') do |builder|
concat(builder.commit_button)
end
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Submit"][@class~="submit"]})
end

Expand Down Expand Up @@ -213,7 +214,7 @@
end

after do
::I18n.backend.store_translations :en, :formtastic => {:create => nil}
::I18n.backend.reload!
end

it 'should render an input with default I18n-localized label (fallback)' do
Expand All @@ -230,40 +231,35 @@
:formtastic => {
:actions => {
:create => 'Custom Create',
:post => {
:create => 'Custom Create {{model}}'
}
}
}
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
end

after do
::I18n.backend.store_translations :en, :formtastic => nil
::I18n.backend.reload!
end

it 'should render an input with localized label (I18n)' do
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button)
end
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Create Post"][@class~="create"]})
end

it 'should render an input with anoptional localized label (I18n) - if first is not set' do
::I18n.backend.store_translations :en,
:formtastic => {
:actions => {
:post => {
:create => nil
:create => 'Custom Create {{model}}'
}
}
}
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button)
end
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Create"][@class~="create"]})
::I18n.backend.store_translations :en, :formtastic => nil

output_buffer.should have_tag(%Q{li.commit input[@value="Custom Create Post"][@class~="create"]})
end

it 'should render an input with anoptional localized label (I18n) - if first is not set' do
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button)
end
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Create"][@class~="create"]})
end

end
Expand Down Expand Up @@ -292,7 +288,7 @@
end

after do
::I18n.backend.store_translations :en, :formtastic => {:update => nil}
::I18n.backend.reload!
end

it 'should render an input with default I18n-localized label (fallback)' do
Expand All @@ -309,33 +305,34 @@
:formtastic => {
:actions => {
:update => 'Custom Save',
:post => {
:update => 'Custom Save {{model}}'
}
}
}
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
end

it 'should render an input with localized label (I18n)' do
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button)
end
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Save Post"][@class~="update"]})
after do
::I18n.backend.reload!
end

it 'should render an input with anoptional localized label (I18n) - if first is not set' do
it 'should render an input with localized label (I18n)' do
::I18n.backend.store_translations :en,
:formtastic => {
:actions => {
:post => {
:update => nil
:update => 'Custom Save {{model}}'
}
}
}
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button)
end
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Save Post"][@class~="update"]})
end

it 'should render an input with anoptional localized label (I18n) - if first is not set' do
semantic_form_for(@new_post) do |builder|
concat(builder.commit_button)
end
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Save"][@class~="update"]})
::I18n.backend.store_translations :en, :formtastic => {}
end
Expand Down
19 changes: 8 additions & 11 deletions spec/i18n_spec.rb
Expand Up @@ -24,7 +24,6 @@

before do
@formtastic_strings = {
:required => 'Default Required',
:yes => 'Default Yes',
:no => 'Default No',
:create => 'Default Create {{model}}',
Expand All @@ -36,8 +35,13 @@
}
::I18n.backend.store_translations :en, :formtastic => @formtastic_strings
end

after do
::I18n.backend.reload!
end

it "should translate core strings correctly" do
::I18n.backend.store_translations :en, {:formtastic => {:required => 'Default Required'}}
::Formtastic::I18n.t(:required).should == "Default Required"
::Formtastic::I18n.t(:yes).should == "Default Yes"
::Formtastic::I18n.t(:no).should == "Default No"
Expand All @@ -54,7 +58,6 @@
end

it "should be possible to override default values" do
::I18n.backend.store_translations :en, {:formtastic => {:required => nil}}
::Formtastic::I18n.t(:required, :default => 'Nothing found!').should == 'Nothing found!'
end

Expand All @@ -63,15 +66,9 @@
describe "when no I18n locales are available" do

before do
::I18n.backend.store_translations :en, :formtastic => {
:required => nil,
:yes => nil,
:no => nil,
:create => nil,
:update => nil
}
::I18n.backend.reload!
end

it "should use default strings" do
(::Formtastic::I18n::DEFAULT_VALUES.keys).each do |key|
::Formtastic::I18n.t(key, :model => '{{model}}').should == ::Formtastic::I18n::DEFAULT_VALUES[key]
Expand Down Expand Up @@ -102,7 +99,7 @@
end

after do
::I18n.backend.store_translations :en, :formtastic => nil
::I18n.backend.reload!
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
end

Expand Down
23 changes: 12 additions & 11 deletions spec/input_spec.rb
Expand Up @@ -528,34 +528,35 @@ def should_be_required(options)
:formtastic => {
:hints => {
:title => @default_localized_hint_text,
:post => {
:title => @localized_hint_text
}
}
}
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
end

after do
::I18n.backend.reload!
end

describe 'when provided value (hint value) is set to TRUE' do
it 'should render a hint paragraph containing a localized hint (I18n)' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :hint => true))
end
output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
end

it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
::I18n.backend.store_translations :en,
:formtastic => {
:hints => {
:post => {
:title => nil
:title => @localized_hint_text
}
}
}
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :hint => true))
end
output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
end

it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :hint => true))
end
output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text)
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/inputs/time_input_spec.rb
Expand Up @@ -12,6 +12,7 @@

describe "general" do
before do
::I18n.backend.reload!
output_buffer.replace ''
end

Expand Down

0 comments on commit 731b792

Please sign in to comment.