i15r 0.4.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,11 @@
1
1
  # encoding: UTF-8
2
- require "i15r"
2
+ require 'spec_helper'
3
+ require 'i15r'
3
4
 
4
- describe I15R::Base do
5
+ describe I15R do
5
6
 
6
7
  before do
7
- @i15r = I15R::Base.new
8
+ @i15r = I15R::Fixture.new
8
9
  end
9
10
 
10
11
  describe "converting file paths to message prefixes" do
@@ -30,87 +31,101 @@ describe I15R::Base do
30
31
  end
31
32
 
32
33
  it "should raise if path does not contain any Rails app directories" do
34
+ #FIXME: Why should we deal only with Rails apps?
33
35
  path = "projects/doodle.rb"
34
- lambda { @i15r.file_path_to_message_prefix(path) }.should raise_error(AppFolderNotFound)
36
+ lambda { @i15r.file_path_to_message_prefix(path) }.should raise_error(I15R::AppFolderNotFound)
35
37
  end
36
38
  end
37
39
 
38
- describe "dry_run?" do
39
- it "should return true when in dry run mode" do
40
- @i15r.options.dry_run = true
41
- @i15r.dry_run?.should == true
42
- end
43
- it "should return false when not in dry run mode" do
44
- @i15r.options.dry_run = false
45
- @i15r.dry_run?.should == false
40
+ describe "writing the changed file" do
41
+ let(:path) { "app/views/users/new.html.erb" }
42
+ let(:reader) { mock(:read => %Q{<label for="user-name">Name</label>}) }
43
+ let(:writer) { mock("writer") }
44
+ let(:printer) { mock("printer") }
45
+
46
+ subject { I15R.new(reader, writer, printer) }
47
+
48
+ specify do
49
+ writer.should_receive(:write)
50
+ .with(path, %Q{<label for="user-name"><%= I18n.t("users.new.name", :default => "Name") %></label>\n})
51
+ printer.should_receive(:println).with("app/views/users/new.html.erb:")
52
+ printer.should_receive(:println).with("")
53
+ printer.should_receive(:print_diff)
54
+ .with(%Q{<label for="user-name">Name</label>},
55
+ %Q{<label for="user-name"><%= I18n.t("users.new.name", :default => "Name") %></label>})
56
+ subject.internationalize_file(path)
46
57
  end
47
58
  end
48
59
 
49
- describe "turning plain messages into i18n message strings" do
50
- it "should downcase a single word" do
51
- I15R::Base.get_i18n_message_string("Name", "users.new").should == "users.new.name"
52
- end
53
60
 
54
- it "should replace spaces with underscores" do
55
- I15R::Base.get_i18n_message_string("New name", "users.index").should == "users.index.new_name"
61
+ describe "generating the prefix" do
62
+ let(:reader) { mock(:read => %Q{<label for="user-name">Name</label>}) }
63
+ let(:writer) { mock("writer") }
64
+
65
+ subject { I15R.new(reader, writer, I15R::NullPrinter.new) }
66
+
67
+ describe "for a view" do
68
+ let(:path) { "app/views/users/new.html.erb" }
69
+ specify do
70
+ writer.should_receive(:write)
71
+ .with(path, %Q{<label for="user-name"><%= I18n.t("users.new.name", :default => "Name") %></label>\n})
72
+ subject.internationalize_file(path)
73
+ end
56
74
  end
57
75
 
58
- it "should not rip out a non-english letter" do
59
- I15R::Base.get_i18n_message_string("Mañana", "users.index").should == "users.index.mañana"
76
+ describe "for a partial" do
77
+ let(:path) { "app/views/users/_badge.html.erb" }
78
+ specify do
79
+ writer.should_receive(:write)
80
+ .with(path, %Q{<label for="user-name"><%= I18n.t("users.badge.name", :default => "Name") %></label>\n})
81
+ subject.internationalize_file(path)
82
+ end
60
83
  end
61
84
 
62
- it "should replace a ' with an underscore" do
63
- I15R::Base.get_i18n_message_string("C'est ça", "users.index").should == "users.index.cest_ça"
85
+ describe "when there is an explicit prefix" do
86
+ let(:path) { "app/views/users/_badge.html.erb" }
87
+
88
+ subject { I15R.new(reader, writer, I15R::NullPrinter.new, :prefix => "nice") }
89
+
90
+ specify do
91
+ writer.should_receive(:write).with(path, %Q{<label for="user-name"><%= I18n.t("nice.name", :default => "Name") %></label>\n})
92
+ subject.internationalize_file(path)
93
+ end
64
94
  end
65
95
  end
66
96
 
67
- describe "when substituting the plain contents with i18n message strings" do
68
- before do
69
- @i15r.options.prefix = nil
70
- @file_path = "app/views/users/new.html.erb"
71
- @i15r.stub(:get_content_from).and_return(%q{<label for=\"user-name\">Name</label>})
72
- end
97
+ describe "the add_default option" do
98
+ let(:path) { "app/users/views/index.html.haml" }
99
+ let(:patter_matcher) { mock("pattern matcher", :run => "") }
100
+ let(:i15r) { I15R::Fixture.new }
101
+
102
+ subject { I15R::Fixture.new }
73
103
 
74
- describe "and in dry-run mode" do
104
+ describe "when true" do
75
105
  before do
76
- @i15r.stub!(:dry_run?).and_return(true)
77
- end
78
- it "should not touch any files" do
79
- @i15r.should_not_receive(:write_content_to)
80
- @i15r.internationalize_file(@file_path)
106
+ subject.config = { :add_default => true }
81
107
  end
82
- it "should display the diff" do
83
- @i15r.should_receive(:show_diff)
84
- @i15r.internationalize_file(@file_path)
108
+ specify do
109
+ I15R::PatternMatcher.should_receive(:new)
110
+ .with(anything, anything, hash_including(:add_default => true))
111
+ .and_return(patter_matcher)
112
+ subject.internationalize_file(path)
85
113
  end
86
114
  end
87
115
 
88
- describe "and not in dry-run mode" do
116
+ describe "when false" do
89
117
  before do
90
- @i15r.stub!(:dry_run?).and_return(false)
118
+ subject.config = { :add_default => false }
91
119
  end
92
- it "should write the files" do
93
- @i15r.should_receive(:write_content_to)
94
- @i15r.internationalize_file(@file_path)
120
+ specify do
121
+ I15R::PatternMatcher.should_receive(:new)
122
+ .with(anything, anything, hash_including(:add_default => false))
123
+ .and_return(patter_matcher)
124
+ subject.internationalize_file(path)
95
125
  end
96
126
  end
97
127
  end
98
128
 
99
- describe "when no prefix option was given" do
100
- it "should correctly internationalize messages using a prefix derived from the path" do
101
- message_prefix = "users.new"
102
- plain_snippet = <<-EOS
103
- <label for="user-name">Name</label>
104
- <input type="text" id="user-name" name="user[name]" />
105
- EOS
106
- i18ned_snippet = <<-EOS
107
- <label for="user-name"><%= I18n.t("#{message_prefix}.name") %></label>
108
- <input type="text" id="user-name" name="user[name]" />
109
- EOS
110
- @i15r.sub_plain_strings(plain_snippet, message_prefix, :erb).should == i18ned_snippet
111
- end
112
- end # "when no prefix option was given"
113
-
114
129
  describe "when an explicit prefix option was given" do
115
130
  it "should correctly internationalize messages using the prefix" do
116
131
  prefix_option = "mysite"
@@ -119,7 +134,7 @@ describe I15R::Base do
119
134
  <input type="text" id="user-name" name="user[name]" />
120
135
  EOS
121
136
  i18ned_snippet = <<-EOS
122
- <label for="user-name"><%= I18n.t("#{prefix_option}.name") %></label>
137
+ <label for="user-name"><%= I18n.t("#{prefix_option}.name", :default => "Name") %></label>
123
138
  <input type="text" id="user-name" name="user[name]" />
124
139
  EOS
125
140
 
@@ -127,17 +142,4 @@ describe I15R::Base do
127
142
  end
128
143
  end # "when an explicit prefix option was given"
129
144
 
130
- describe "when running the internationalization for an ERB file" do
131
- before do
132
- @i15r.stub!(:write_content_to).and_return(true)
133
- @file_path = "app/views/users/new.html.erb"
134
- @i15r.stub(:get_content_from).and_return(%q{<label for=\"user-name\">Name</label>})
135
- end
136
-
137
- it "should only run ERB matchers" do
138
- @i15r.should_receive(:sub_plain_strings).with(anything, anything, :erb)
139
- @i15r.internationalize_file(@file_path)
140
- end
141
- end
142
-
143
145
  end
@@ -1,23 +1,199 @@
1
1
  # encoding: UTF-8
2
+ require 'spec_helper'
2
3
  require 'i15r/pattern_matcher'
3
4
 
4
- describe I15R::PatternMatchers::Base do
5
- it "should not replace a simple haml div tag with an id" do
6
- plain = %(#main)
7
- I15R::PatternMatchers::Base.run(plain, "users.new", :haml).should == plain
8
- end
5
+ describe I15R::PatternMatcher do
6
+
7
+ subject { pattern_matcher }
8
+
9
+ describe "in erb templates" do
10
+ let(:pattern_matcher) { I15R::PatternMatcher.new("users.new", :erb) }
11
+
12
+ describe "in tag content" do
13
+ it { should internationalize('<h1>New flight</h1>')
14
+ .to('<h1><%= I18n.t("users.new.new_flight") %></h1>') }
15
+ it { should internationalize(%(<label for="user-name">Name</label>))
16
+ .to(%(<label for="user-name"><%= I18n.t("users.new.name") %></label>)) }
17
+
18
+ it { should internationalize(%(<label for="user-name">First name</label>))
19
+ .to(%(<label for="user-name"><%= I18n.t("users.new.first_name") %></label>)) }
20
+
21
+ it { should internationalize(%(<label for="user-name">Got friends? A friend's name</label>))
22
+ .to(%(<label for="user-name"><%= I18n.t("users.new.got_friends_a_friends_name") %></label>)) }
23
+
24
+ it { should internationalize(%(<label for="user-name">A friend's name:</label>))
25
+ .to(%(<label for="user-name"><%= I18n.t("users.new.a_friends_name") %></label>)) }
26
+
27
+ it { should internationalize(%(<label for="user-name"> Name </label>))
28
+ .to(%(<label for="user-name"><%= I18n.t("users.new.name") %></label>)) }
29
+
30
+ it { should internationalize(%(<label for="when">Mañana</label>))
31
+ .to(%(<label for="when"><%= I18n.t("users.new.mañana") %></label>)) }
32
+
33
+ describe "when the default option is given" do
34
+ let(:pattern_matcher) { I15R::PatternMatcher.new("users.new", :erb, :add_default => true) }
35
+
36
+ it { should internationalize('<h1>New flight</h1>')
37
+ .to('<h1><%= I18n.t("users.new.new_flight", :default => "New flight") %></h1>') }
38
+ it { should internationalize(%( <%= f.label :name %><br />))
39
+ .to(%( <%= f.label I18n.t("users.new.name", :default => "name") %><br />)) }
40
+ end
41
+
42
+ describe "when a line is already international" do
43
+ it { should internationalize(%( <%= f.label I18n.t("users.new.name") %>)).to_the_same }
44
+ it { should internationalize(%( <%= f.label t("users.new.name") %>)).to_the_same }
45
+ end
46
+ end
47
+
48
+ describe "in tag attributes" do
49
+ it { should internationalize(%(<a title="site root" href="/"><img src="site_logo.png" /></a>))
50
+ .to(%(<a title="<%= I18n.t("users.new.site_root") %>" href="/"><img src="site_logo.png" /></a>)) }
51
+
52
+ it { should internationalize(%(<a title="site root" href="/"><img src="site_logo.png" /></a>))
53
+ .to(%(<a title="<%= I18n.t("users.new.site_root") %>" href="/"><img src="site_logo.png" /></a>)) }
54
+ end
55
+
56
+ describe "Rails helper methods" do
57
+ let(:pattern_matcher) { I15R::PatternMatcher.new("users.index", :erb) }
58
+
59
+ it { should internationalize(%(<p class="highlighted"><%= link_to 'New user', new_user_path %>?</p>))
60
+ .to(%(<p class="highlighted"><%= link_to I18n.t("users.index.new_user"), new_user_path %>?</p>)) }
61
+
62
+ it { should internationalize(%(<p><%= link_to "Create a new user", new_user_path, { :class => "add" } -%></p>))
63
+ .to(%(<p><%= link_to I18n.t("users.index.create_a_new_user"), new_user_path, { :class => "add" } -%></p>)) }
64
+
65
+ it { should internationalize(%(<%= f.label :name, "Name" %>))
66
+ .to(%(<%= f.label :name, I18n.t("users.index.name") %>)) }
67
+
68
+ it { should internationalize(%( <%= f.label :name %><br />))
69
+ .to(%( <%= f.label I18n.t("users.index.name") %><br />)) }
70
+
71
+ it { should internationalize(%( <%= label_tag :name, "Real Name" %><br />))
72
+ .to(%( <%= label_tag :name, I18n.t("users.index.real_name") %><br />)) }
73
+
74
+ it { should internationalize(%(<%= f.submit "Create user" %>))
75
+ .to(%(<%= f.submit I18n.t("users.index.create_user") %>)) }
9
76
 
10
- it "should properly replace a line with two matches" do
11
- plain = %(This is it: <a title="site root" href="/"><img src="site_logo.png" /></a>)
12
- i18ned = %(<%= I18n.t("users.new.this_is_it") %>: <a title="<%= I18n.t("users.new.site_root") %>" href="/"><img src="site_logo.png" /></a>)
13
- I15R::PatternMatchers::Base.run(plain, "users.new", :erb).should == i18ned
77
+ it { should internationalize(%(<%= submit_tag "Create user" %>))
78
+ .to(%(<%= submit_tag I18n.t("users.index.create_user") %>)) }
79
+
80
+ describe "when the default option is given" do
81
+ let(:pattern_matcher) { I15R::PatternMatcher.new("users.index", :erb, :add_default => true) }
82
+
83
+ it { should internationalize(%(<%= submit_tag "Create user" %>))
84
+ .to(%(<%= submit_tag I18n.t("users.index.create_user", :default => "Create user") %>)) }
85
+ end
86
+
87
+ describe "when text has non-ascii characters" do
88
+ it { should internationalize(%(<p class="highlighted"><%= link_to 'Új felhasználó', new_user_path %>?</p>))
89
+ .to(%(<p class="highlighted"><%= link_to I18n.t("users.index.Új_felhasználó"), new_user_path %>?</p>)) }
90
+
91
+ it { should internationalize(%(<p><%= link_to "Új felhasználó létrehozása", new_user_path, { :class => "add" } -%></p>))
92
+ .to(%(<p><%= link_to I18n.t("users.index.Új_felhasználó_létrehozása"), new_user_path, { :class => "add" } -%></p>)) }
93
+
94
+ it { should internationalize(%(<%= f.label :name, "Név" %>))
95
+ .to(%(<%= f.label :name, I18n.t("users.index.név") %>)) }
96
+
97
+ it { should internationalize(%(<%= label_tag :name, "Név" %>))
98
+ .to(%(<%= label_tag :name, I18n.t("users.index.név") %>)) }
99
+
100
+ it { should internationalize(%( <%= label_tag :name, "Real Name" %><br />))
101
+ .to(%( <%= label_tag :name, I18n.t("users.index.real_name") %><br />)) }
102
+
103
+ it { should internationalize(%(<%= f.submit "Új felhasználó" %>))
104
+ .to(%(<%= f.submit I18n.t("users.index.Új_felhasználó") %>)) }
105
+
106
+ it { should internationalize(%(<%= submit_tag "Új felhasználó" %>))
107
+ .to(%(<%= submit_tag I18n.t("users.index.Új_felhasználó") %>)) }
108
+
109
+ it { should internationalize(%(<%= f.submit :create_user %>))
110
+ .to(%(<%= f.submit I18n.t("users.index.create_user") %>)) }
111
+
112
+ it { should internationalize(%( <%= f.submit :create_user %>))
113
+ .to(%( <%= f.submit I18n.t("users.index.create_user") %>)) }
114
+ end
115
+ end
14
116
  end
15
117
 
16
- #1.8fail
17
- it "should properly replace a line with two matches that have non-ascii chars" do
18
- plain = %(C'est ça: <a title="site root" href="/"><img src="site_logo.png" /></a>)
19
- i18ned = %(<%= I18n.t("users.new.cest_ça") %>: <a title="<%= I18n.t("users.new.site_root") %>" href="/"><img src="site_logo.png" /></a>)
20
- I15R::PatternMatchers::Base.run(plain, "users.new", :erb).should == i18ned
118
+ describe "in haml templates" do
119
+ let(:pattern_matcher) { I15R::PatternMatcher.new("users.show", :haml) }
120
+
121
+ it { should internationalize('#main').to_the_same }
122
+
123
+ it { should internationalize(%(#form_head My account))
124
+ .to(%(#form_head= I18n.t("users.show.my_account"))) }
125
+
126
+ it { should internationalize(%(%p Please check your inbox and click on the activation link.))
127
+ .to(%(%p= I18n.t("users.show.please_check_your_inbox_and_click_on_the_activation_link"))) }
128
+
129
+ it { should internationalize("please visit").to('= I18n.t("users.show.please_visit")') }
130
+ it { should internationalize("Mañana").to('= I18n.t("users.show.mañana")') }
131
+ it { should internationalize("C'est ça").to('= I18n.t("users.show.cest_ça")') }
132
+ it { should internationalize(%(%p Do not close/reload while loading))
133
+ .to(%(%p= I18n.t("users.show.do_not_close_reload_while_loading"))) }
134
+ it { should internationalize(%( .field)).to(%( .field)) }
135
+ it { should internationalize('%p').to_the_same }
136
+ it { should internationalize(%(%h2 Resend unlock instructions))
137
+ .to(%(%h2= I18n.t("users.show.resend_unlock_instructions"))) }
138
+ it { should internationalize(%(%i (we need your password to confirm your changes)))
139
+ .to(%(%i= I18n.t("users.show.we_need_your_password_to_confirm_your_changes"))) }
140
+ it { should internationalize('= yield').to_the_same }
141
+ it { should internationalize('/ Do not remove the next line').to_the_same }
142
+ it { should internationalize(%(#form_head Türkçe))
143
+ .to(%(#form_head= I18n.t("users.show.türkçe"))) }
144
+ it { should internationalize(%(%p Egy, kettő, három, négy, öt.))
145
+ .to(%(%p= I18n.t("users.show.egy_kettő_három_négy_öt"))) }
146
+ it { should internationalize(%(Türkçe)).to(%(= I18n.t("users.show.türkçe"))) }
147
+ it { should internationalize(%( %h3 Top Scorers))
148
+ .to(%( %h3= I18n.t("users.show.top_scorers"))) }
149
+ it { should internationalize(%( %div{ :class => "field option", :style => "float:left" })).to_the_same }
150
+ it { should internationalize(%( %div{ :class => "field option", :style => "float:left" } Name))
151
+ .to(%( %div{ :class => "field option", :style => "float:left" }= I18n.t("users.show.name"))) }
152
+ it { should internationalize(%( %div(class: "field option", style: "float:left") Name))
153
+ .to(%( %div(class: "field option", style: "float:left")= I18n.t("users.show.name"))) }
154
+
155
+ describe "when the default option is given" do
156
+ let(:pattern_matcher) { I15R::PatternMatcher.new("users.show", :haml, :add_default => true) }
157
+ it { should internationalize(%( %h3 Top Scorers))
158
+ .to(%( %h3= I18n.t("users.show.top_scorers", :default => "Top Scorers"))) }
159
+ end
160
+
161
+ describe "when already evaluated" do
162
+ it { should internationalize(%(%p= link_to 'New user', new_user_path))
163
+ .to(%(%p= link_to I18n.t("users.show.new_user"), new_user_path)) }
164
+ it { should internationalize(%(= f.label :password, "Password"))
165
+ .to(%(= f.label :password, I18n.t("users.show.password"))) }
166
+ it { should internationalize(%( = f.label :password, "Password"))
167
+ .to(%( = f.label :password, I18n.t("users.show.password"))) }
168
+ it { should internationalize(%(%p= link_to 'Új felhasználó', new_user_path))
169
+ .to(%(%p= link_to I18n.t("users.show.Új_felhasználó"), new_user_path)) }
170
+ it { should internationalize(%(#new_user_link= link_to 'Új felhasználó', new_user_path))
171
+ .to(%(#new_user_link= link_to I18n.t("users.show.Új_felhasználó"), new_user_path)) }
172
+ it { should internationalize(%(= f.label :password, "Contraseña"))
173
+ .to(%(= f.label :password, I18n.t("users.show.contraseña"))) }
174
+ it { should internationalize(%( = f.label :password, "Contraseña"))
175
+ .to(%( = f.label :password, I18n.t("users.show.contraseña"))) }
176
+ it { should internationalize(%( = f.label :name))
177
+ .to(%( = f.label I18n.t("users.show.name"))) }
178
+
179
+ it { should internationalize(%(= f.submit "Create user"))
180
+ .to(%(= f.submit I18n.t("users.show.create_user"))) }
181
+ it { should internationalize(%(= submit_tag "Create user"))
182
+ .to(%(= submit_tag I18n.t("users.show.create_user"))) }
183
+ it { should internationalize(%( = f.submit :user_details %>))
184
+ .to(%( = f.submit I18n.t("users.show.user_details") %>)) }
185
+ it { should internationalize(%( = submit_tag :user_details %>))
186
+ .to(%( = submit_tag I18n.t("users.show.user_details") %>)) }
187
+ end
188
+
189
+ describe "evaluated ruby code" do
190
+ it { should internationalize('- if foo == :bar').to_the_same }
191
+ end
192
+
193
+ describe "when a line is already international" do
194
+ it { should internationalize(%( = f.label I18n.t("users.new.name") )).to_the_same }
195
+ it { should internationalize(%( = f.label t("users.new.name") )).to_the_same }
196
+ end
21
197
  end
22
198
 
23
199
  end
@@ -1,4 +1,39 @@
1
- $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib")
1
+ current_dir = File.expand_path(File.dirname(__FILE__))
2
+ $:.unshift File.join(current_dir, "..", "lib")
2
3
 
3
4
  require 'i15r'
4
- require 'i15r/pattern_matchers/haml'
5
+ require 'i15r/file_reader'
6
+
7
+ Dir["#{current_dir}/support/**/*.rb"].each do |file|
8
+ require file
9
+ end
10
+
11
+ class I15R
12
+ class StringReader
13
+ def read(path)
14
+ "norf"
15
+ end
16
+ end
17
+
18
+ class NullWriter
19
+ def write(path, content); end
20
+ end
21
+ class NullPrinter
22
+ def print_diff(old_row, new_row); end
23
+ def println(text); end
24
+ end
25
+
26
+ class Fixture < I15R
27
+ def initialize(reader=StringReader.new,
28
+ writer=NullWriter.new,
29
+ printer=NullPrinter.new,
30
+ config={})
31
+ super(reader, writer, printer, config)
32
+ end
33
+
34
+ def self.with_config(config)
35
+ new(StringReader.new, NullWriter.new, NullPrinter.new, config)
36
+ end
37
+ end
38
+ end
39
+