Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 130 lines (101 sloc) 4.326 kb
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes…
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
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
38
39 after do
40 ::I18n.backend.reload!
41 end
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes…
grimen authored
42
43 it "should translate core strings correctly" do
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
44 ::I18n.backend.store_translations :en, {:formtastic => {:required => 'Default Required'}}
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes…
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
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
69 ::I18n.backend.reload!
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes…
grimen authored
70 end
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
71
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes…
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 @grimen Removed redundant stuff with tiny refactoring, and adding a general I…
grimen authored
80 describe "I18n string lookups" do
81
82 include FormtasticSpecHelper
83
84 before do
f9c1a8a @yabawock Revert all the changes introduced in commits 8d17ca4..8d8c50
yabawock authored
85 @output_buffer = ''
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I…
grimen authored
86 mock_everything
87
88 ::I18n.backend.store_translations :en, :formtastic => {
89 :labels => {
90 :title => "Hello world!",
91 :post => {:title => "Hello post!"},
92 :project => {:title => "Hello project!"}
93 }
94 }
95 ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true
96
97 @new_post.stub!(:title)
98 @new_post.stub!(:column_for_attribute).with(:title).and_return(mock('column', :type => :string, :limit => 255))
99 end
100
101 after do
aea23c4 @yabawock Refactored I18n handling in spec testsuite
yabawock authored
102 ::I18n.backend.reload!
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I…
grimen authored
103 ::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
104 end
105
f22b7a1 @grimen I18N_SCOPES => I18n::SCOPES
grimen authored
106 it "lookup scopes should be defined" do
107 lambda { ::Formtastic::I18n::SCOPES }.should_not raise_error(::NameError)
108 end
109
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I…
grimen authored
110 it "should be able to translate with namespaced object" do
d7a729d @yabawock Capture returns from formtastic and append them to the output_buffer …
yabawock authored
111 form = semantic_form_for(@new_post) do |builder|
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I…
grimen authored
112 concat(builder.input(:title))
113 end
d7a729d @yabawock Capture returns from formtastic and append them to the output_buffer …
yabawock authored
114 output_buffer.concat(form) if defined?(ActiveSupport::SafeBuffer)
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I…
grimen authored
115 output_buffer.should have_tag("form label", /Hello post!/)
116 end
117
118 it "should be able to translate without form-object" do
d7a729d @yabawock Capture returns from formtastic and append them to the output_buffer …
yabawock authored
119 form = semantic_form_for(:project, :url => 'http://test.host') do |builder|
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I…
grimen authored
120 concat(builder.input(:title))
121 end
d7a729d @yabawock Capture returns from formtastic and append them to the output_buffer …
yabawock authored
122 output_buffer.concat(form) if defined?(ActiveSupport::SafeBuffer)
616c42a @grimen Removed redundant stuff with tiny refactoring, and adding a general I…
grimen authored
123 output_buffer.should have_tag("form label", /Hello project!/)
124 end
125
126 # TODO: Add spec for namespaced models?
127
128 end
129
382c552 @grimen Refactored I18n stuff to Formtastic::I18n with specs. Cleaner. Closes…
grimen authored
130 end
Something went wrong with that request. Please try again.