Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 148 lines (116 sloc) 4.963 kb
382c552 Jonas Grimfelt Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
1 # coding: utf-8
2 require File.join(File.dirname(__FILE__), *%w[spec_helper])
3
4 describe 'Formtastic::I18n' do
5
6 FORMTASTIC_KEYS = [:required, :yes, :no, :create, :update].freeze
7
8 it "should be defined" do
9 lambda { ::Formtastic::I18n }.should_not raise_error(::NameError)
10 end
11
12 describe "default translations" do
13 it "should be defined" do
14 lambda { ::Formtastic::I18n::DEFAULT_VALUES }.should_not raise_error(::NameError)
15 ::Formtastic::I18n::DEFAULT_VALUES.is_a?(::Hash).should == true
16 end
17
18 it "should exists for the core I18n lookup keys" do
19 (::Formtastic::I18n::DEFAULT_VALUES.keys & FORMTASTIC_KEYS).size.should == FORMTASTIC_KEYS.size
20 end
21 end
22
23 describe "when I18n locales are available" do
24
25 before do
26 @formtastic_strings = {
27 :yes => 'Default Yes',
28 :no => 'Default No',
29 :create => 'Default Create {{model}}',
30 :update => 'Default Update {{model}}',
31 :custom_scope => {
32 :duck => 'Duck',
33 :duck_pond => '{{ducks}} ducks in a pond'
34 }
35 }
36 ::I18n.backend.store_translations :en, :formtastic => @formtastic_strings
37 end
731b792 Morton Jonuschat Refactored I18n handling in spec testsuite
yabawock authored
38
39 after do
40 ::I18n.backend.reload!
41 end
382c552 Jonas Grimfelt Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
42
43 it "should translate core strings correctly" do
731b792 Morton Jonuschat Refactored I18n handling in spec testsuite
yabawock authored
44 ::I18n.backend.store_translations :en, {:formtastic => {:required => 'Default Required'}}
382c552 Jonas Grimfelt Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
45 ::Formtastic::I18n.t(:required).should == "Default Required"
46 ::Formtastic::I18n.t(:yes).should == "Default Yes"
47 ::Formtastic::I18n.t(:no).should == "Default No"
48 ::Formtastic::I18n.t(:create, :model => 'Post').should == "Default Create Post"
49 ::Formtastic::I18n.t(:update, :model => 'Post').should == "Default Update Post"
50 end
51
52 it "should all belong to scope 'formtastic'" do
53 ::Formtastic::I18n.t(:duck, :scope => [:custom_scope]).should == 'Duck'
54 end
55
56 it "should override default I18n lookup args if these are specified" do
57 ::Formtastic::I18n.t(:duck_pond, :scope => [:custom_scope], :ducks => 15).should == '15 ducks in a pond'
58 end
59
60 it "should be possible to override default values" do
61 ::Formtastic::I18n.t(:required, :default => 'Nothing found!').should == 'Nothing found!'
62 end
63
64 end
65
66 describe "when no I18n locales are available" do
67
68 before do
731b792 Morton Jonuschat Refactored I18n handling in spec testsuite
yabawock authored
69 ::I18n.backend.reload!
382c552 Jonas Grimfelt Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
70 end
731b792 Morton Jonuschat Refactored I18n handling in spec testsuite
yabawock authored
71
382c552 Jonas Grimfelt Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
72 it "should use default strings" do
73 (::Formtastic::I18n::DEFAULT_VALUES.keys).each do |key|
74 ::Formtastic::I18n.t(key, :model => '{{model}}').should == ::Formtastic::I18n::DEFAULT_VALUES[key]
75 end
76 end
77
78 end
79
616c42a Jonas Grimfelt Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
80 describe "I18n string lookups" do
81
82 include FormtasticSpecHelper
83
84 before do
85 @output_buffer = ''
86 mock_everything
87
88 ::I18n.backend.store_translations :en, :formtastic => {
89 :labels => {
90 :title => "Hello world!",
91 :post => {:title => "Hello post!"},
6b53be0 Toni Tuominen Translations for nested models.
tjtuom authored
92 :project => {:title => "Hello project!", :task => {:name => "Hello task name!"}},
93 :line_item => {:name => "Hello line item name!"}
616c42a Jonas Grimfelt Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
94 }
95 }
96 ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
97
98 @new_post.stub!(:title)
99 @new_post.stub!(:column_for_attribute).with(:title).and_return(mock('column', :type => :string, :limit => 255))
100 end
101
102 after do
731b792 Morton Jonuschat Refactored I18n handling in spec testsuite
yabawock authored
103 ::I18n.backend.reload!
616c42a Jonas Grimfelt Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
104 ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
105 end
106
f22b7a1 Jonas Grimfelt I18N_SCOPES => I18n::SCOPES
grimen authored
107 it "lookup scopes should be defined" do
108 lambda { ::Formtastic::I18n::SCOPES }.should_not raise_error(::NameError)
109 end
110
616c42a Jonas Grimfelt Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
111 it "should be able to translate with namespaced object" do
112 semantic_form_for(@new_post) do |builder|
113 concat(builder.input(:title))
114 end
115 output_buffer.should have_tag("form label", /Hello post!/)
116 end
117
118 it "should be able to translate without form-object" do
119 semantic_form_for(:project, :url => 'http://test.host') do |builder|
120 concat(builder.input(:title))
121 end
122 output_buffer.should have_tag("form label", /Hello project!/)
123 end
124
6b53be0 Toni Tuominen Translations for nested models.
tjtuom authored
125 it 'should be able to translate nested objects with nested translations' do
126 semantic_form_for(:project, :url => 'http://test.host') do |builder|
127 builder.semantic_fields_for(:task) do |f|
128 concat(f.input(:name))
129 end
130 end
131 output_buffer.should have_tag("form label", /Hello task name!/)
132 end
133
134 it 'should be able to translated nested objects with top level translations' do
135 semantic_form_for(:order, :url => 'http://test.host') do |builder|
136 builder.semantic_fields_for(:line_item) do |f|
137 concat(f.input(:name))
138 end
139 end
140 output_buffer.should have_tag("form label", /Hello line item name!/)
141 end
142
143
616c42a Jonas Grimfelt Removed redundant stuff with tiny refactoring, and adding a general I18n...
grimen authored
144 # TODO: Add spec for namespaced models?
145
146 end
147
382c552 Jonas Grimfelt Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes #1...
grimen authored
148 end
Something went wrong with that request. Please try again.