puttext 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,327 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'unindent'
5
+
6
+ describe PutText::POEntry do
7
+ describe '#initialize' do
8
+ context 'creating a simple PO entry' do
9
+ let(:entry) { described_class.new(msgid: 'An error occurred!') }
10
+
11
+ it 'sets the passed msgid attribute' do
12
+ expect(entry.msgid).to eq('An error occurred!')
13
+ end
14
+
15
+ it 'sets msgid_plural to nil' do
16
+ expect(entry.msgid_plural).to be nil
17
+ end
18
+
19
+ it 'sets msgctxt to nil' do
20
+ expect(entry.msgctxt).to be nil
21
+ end
22
+
23
+ it 'sets references to an empty array' do
24
+ expect(entry.references).to eq([])
25
+ end
26
+ end
27
+
28
+ context 'creating a pluralized entry' do
29
+ let(:entry) do
30
+ described_class.new(
31
+ msgid: 'An error occurred!',
32
+ msgid_plural: '%d errors occurred!'
33
+ )
34
+ end
35
+
36
+ it 'sets the passed msgid attribute' do
37
+ expect(entry.msgid).to eq('An error occurred!')
38
+ end
39
+
40
+ it 'sets the passed msgid_plural attribute' do
41
+ expect(entry.msgid_plural).to eq('%d errors occurred!')
42
+ end
43
+ end
44
+
45
+ context 'creating an entry with a context' do
46
+ let(:entry) do
47
+ described_class.new(
48
+ msgid: 'An error occurred!',
49
+ msgctxt: 'Error modal'
50
+ )
51
+ end
52
+
53
+ it 'sets the passed msgid attribute' do
54
+ expect(entry.msgid).to eq('An error occurred!')
55
+ end
56
+
57
+ it 'sets the passed msgctxt attribute' do
58
+ expect(entry.msgctxt).to eq('Error modal')
59
+ end
60
+ end
61
+
62
+ context 'creating an entry with references' do
63
+ let(:entry) do
64
+ described_class.new(
65
+ msgid: 'An error occurred!',
66
+ references: ['errors.rb:15']
67
+ )
68
+ end
69
+
70
+ it 'sets the passed msgid attribute' do
71
+ expect(entry.msgid).to eq('An error occurred!')
72
+ end
73
+
74
+ it 'sets the passed msgctxt attribute' do
75
+ expect(entry.references).to eq(['errors.rb:15'])
76
+ end
77
+ end
78
+
79
+ context 'creating an entry with a context in msgid' do
80
+ context 'context is separated by the default separator' do
81
+ let(:entry) do
82
+ described_class.new(
83
+ msgid: 'Error modal|An error occurred!'
84
+ )
85
+ end
86
+
87
+ it 'sets the correct separated msgid' do
88
+ expect(entry.msgid).to eq('An error occurred!')
89
+ end
90
+
91
+ it 'sets the passed msgctxt attribute' do
92
+ expect(entry.msgctxt).to eq('Error modal')
93
+ end
94
+ end
95
+
96
+ context 'context is separated by a custom separator' do
97
+ let(:entry) do
98
+ described_class.new(
99
+ msgid: 'Error modal;;An error occurred!',
100
+ separator: ';;'
101
+ )
102
+ end
103
+
104
+ it 'sets the correct separated msgid' do
105
+ expect(entry.msgid).to eq('An error occurred!')
106
+ end
107
+
108
+ it 'sets the passed msgctxt attribute' do
109
+ expect(entry.msgctxt).to eq('Error modal')
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ describe '#references?' do
116
+ context 'entry has references' do
117
+ let(:entry) do
118
+ described_class.new(
119
+ msgid: 'An error occurred!',
120
+ references: ['errors.rb:15']
121
+ )
122
+ end
123
+
124
+ it 'returns true' do
125
+ expect(entry.references?).to be true
126
+ end
127
+ end
128
+
129
+ context 'entry does not have references' do
130
+ let(:entry) { described_class.new(msgid: 'An error occurred!') }
131
+
132
+ it 'returns false' do
133
+ expect(entry.references?).to be false
134
+ end
135
+ end
136
+ end
137
+
138
+ describe '#plural?' do
139
+ context 'entry is a pluralized entry' do
140
+ let(:entry) do
141
+ described_class.new(
142
+ msgid: 'An error occurred!',
143
+ msgid_plural: '%d errors occurred!'
144
+ )
145
+ end
146
+
147
+ it 'returns true' do
148
+ expect(entry.plural?).to be true
149
+ end
150
+ end
151
+
152
+ context 'entry is not pluralized entry' do
153
+ let(:entry) { described_class.new(msgid: 'An error occurred!') }
154
+
155
+ it 'returns false' do
156
+ expect(entry.plural?).to be false
157
+ end
158
+ end
159
+ end
160
+
161
+ describe '#merge' do
162
+ let(:entry) do
163
+ described_class.new(
164
+ msgid: 'An error occurred!',
165
+ references: ['errors.rb:15']
166
+ )
167
+ end
168
+
169
+ let(:other_entry) do
170
+ described_class.new(
171
+ msgid: 'An error occurred!',
172
+ references: [
173
+ 'subfolder/random_file.rb:5',
174
+ 'subfolder/another_file.rb:168'
175
+ ]
176
+ )
177
+ end
178
+
179
+ let(:merged_entry) { entry.merge(other_entry) }
180
+
181
+ it 'merges references of both entries' do
182
+ expect(merged_entry.references).to eq(
183
+ [
184
+ 'errors.rb:15',
185
+ 'subfolder/random_file.rb:5',
186
+ 'subfolder/another_file.rb:168'
187
+ ]
188
+ )
189
+ end
190
+ end
191
+
192
+ describe '#unique_key' do
193
+ let(:entry_1) { described_class.new(msgid: 'An error occurred!') }
194
+ let(:entry_2) { described_class.new(msgid: 'Another message') }
195
+
196
+ let(:ctxt_entry_1) do
197
+ described_class.new(
198
+ msgid: 'An error occurred!',
199
+ msgctxt: 'Error modal'
200
+ )
201
+ end
202
+
203
+ let(:ctxt_entry_2) do
204
+ described_class.new(
205
+ msgid: 'An error occurred!',
206
+ msgctxt: 'Error modal'
207
+ )
208
+ end
209
+
210
+ context 'comparing the key from the same entry' do
211
+ it 'returns the same key' do
212
+ expect(entry_1.unique_key).to eq(entry_1.unique_key)
213
+ end
214
+ end
215
+
216
+ context 'comparing keys of entries with the same message and context' do
217
+ it 'returns the same key' do
218
+ expect(ctxt_entry_1.unique_key).to eq(ctxt_entry_2.unique_key)
219
+ end
220
+ end
221
+
222
+ context 'comparing keys of entries with different messages' do
223
+ it 'returns different keys' do
224
+ expect(entry_1.unique_key).not_to eq(entry_2.unique_key)
225
+ end
226
+ end
227
+
228
+ context 'comparing keys of entries with same message, different contexts' do
229
+ it 'returns different keys' do
230
+ expect(entry_1.unique_key).not_to eq(ctxt_entry_1.unique_key)
231
+ end
232
+ end
233
+ end
234
+
235
+ describe '#to_s' do
236
+ context 'with a simple PO entry' do
237
+ let(:entry) { described_class.new(msgid: 'An error occurred!') }
238
+
239
+ it 'generates correct string' do
240
+ expect(entry.to_s).to eq(<<-PO.unindent)
241
+ msgid "An error occurred!"
242
+ msgstr ""
243
+ PO
244
+ end
245
+ end
246
+
247
+ context 'with a pluralized entry' do
248
+ let(:entry) do
249
+ described_class.new(
250
+ msgid: 'An error occurred!',
251
+ msgid_plural: '%d errors occurred!'
252
+ )
253
+ end
254
+
255
+ it 'generates correct string' do
256
+ expect(entry.to_s).to eq(<<-PO.unindent)
257
+ msgid "An error occurred!"
258
+ msgid_plural "%d errors occurred!"
259
+ msgstr[0] ""
260
+ msgstr[1] ""
261
+ PO
262
+ end
263
+ end
264
+
265
+ context 'with an entry with a context' do
266
+ let(:entry) do
267
+ described_class.new(
268
+ msgid: 'An error occurred!',
269
+ msgctxt: 'Error modal'
270
+ )
271
+ end
272
+
273
+ it 'generates correct string' do
274
+ expect(entry.to_s).to eq(<<-PO.unindent)
275
+ msgctxt "Error modal"
276
+ msgid "An error occurred!"
277
+ msgstr ""
278
+ PO
279
+ end
280
+ end
281
+
282
+ context 'with an entry with references' do
283
+ let(:entry) do
284
+ described_class.new(
285
+ msgid: 'An error occurred!',
286
+ references: ['errors.rb:15', 'another/file.rb:168']
287
+ )
288
+ end
289
+
290
+ it 'generates correct string' do
291
+ expect(entry.to_s).to eq(<<-PO.unindent)
292
+ #: errors.rb:15 another/file.rb:168
293
+ msgid "An error occurred!"
294
+ msgstr ""
295
+ PO
296
+ end
297
+ end
298
+
299
+ context 'with multiline msgid' do
300
+ let(:entry) do
301
+ described_class.new(
302
+ msgid: "An error occurred!\nHorrible error description"
303
+ )
304
+ end
305
+
306
+ it 'generates correct string' do
307
+ expect(entry.to_s).to eq(<<-PO.unindent)
308
+ msgid ""
309
+ "An error occurred!\\n"
310
+ "Horrible error description"
311
+ msgstr ""
312
+ PO
313
+ end
314
+ end
315
+
316
+ context 'with characters in msgid that need to be escaped' do
317
+ let(:entry) { described_class.new(msgid: "\tAn \"error\" occurred!") }
318
+
319
+ it 'generates correct string' do
320
+ expect(entry.to_s).to eq(<<-PO.unindent)
321
+ msgid "\\tAn \\"error\\" occurred!"
322
+ msgstr ""
323
+ PO
324
+ end
325
+ end
326
+ end
327
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'unindent'
5
+
6
+ describe PutText::POFile do
7
+ describe '#initialize' do
8
+ let(:entry_1) { PutText::POEntry.new(msgid: 'One error occurred') }
9
+ let(:entry_2) { PutText::POEntry.new(msgid: 'Another error occurred') }
10
+ let(:file) { described_class.new([entry_1, entry_2]) }
11
+
12
+ it 'creates a POFile with the specified entries' do
13
+ expect(file.entries).to eq([entry_1, entry_2])
14
+ end
15
+ end
16
+
17
+ describe '#to_s' do
18
+ let(:entry_1) do
19
+ PutText::POEntry.new(
20
+ msgid: 'Error #1 occurred',
21
+ references: ['error1.rb:1']
22
+ )
23
+ end
24
+
25
+ let(:entry_2) do
26
+ PutText::POEntry.new(
27
+ msgid: 'Error #2 occurred',
28
+ references: ['error2.rb:2']
29
+ )
30
+ end
31
+
32
+ let(:entry_3) do
33
+ PutText::POEntry.new(
34
+ msgid: 'Error #2 occurred',
35
+ references: ['error2.rb:8']
36
+ )
37
+ end
38
+
39
+ let(:file) { described_class.new([entry_1, entry_2, entry_3]) }
40
+
41
+ it 'generates correct string' do
42
+ expect(file.to_s).to eq(<<-PO.unindent)
43
+ #: error1.rb:1
44
+ msgid "Error #1 occurred"
45
+ msgstr ""
46
+
47
+ #: error2.rb:2 error2.rb:8
48
+ msgid "Error #2 occurred"
49
+ msgstr ""
50
+ PO
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,199 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puttext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mantas Norvaiša
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.4.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 2.4.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: slim
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.5'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.5'
75
+ - !ruby/object:Gem::Dependency
76
+ name: unindent
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 0.46.0
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 0.46.0
103
+ - !ruby/object:Gem::Dependency
104
+ name: simplecov
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: codeclimate-test-reporter
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 1.0.0
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 1.0.0
131
+ description:
132
+ email: mntnorv@gmail.com
133
+ executables:
134
+ - puttext
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".codeclimate.yml"
139
+ - ".gitattributes"
140
+ - ".gitignore"
141
+ - ".rspec"
142
+ - ".rubocop.yml"
143
+ - ".ruby-version"
144
+ - ".travis.yml"
145
+ - Gemfile
146
+ - Gemfile.lock
147
+ - LICENSE
148
+ - README.md
149
+ - Rakefile
150
+ - bin/puttext
151
+ - lib/puttext.rb
152
+ - lib/puttext/cmdline.rb
153
+ - lib/puttext/extractor.rb
154
+ - lib/puttext/parser/base.rb
155
+ - lib/puttext/parser/ruby.rb
156
+ - lib/puttext/parser/slim.rb
157
+ - lib/puttext/po_entry.rb
158
+ - lib/puttext/po_file.rb
159
+ - puttext.gemspec
160
+ - spec/fixtures/extractor_fixtures/another_file.php
161
+ - spec/fixtures/extractor_fixtures/file_1.rb
162
+ - spec/fixtures/extractor_fixtures/file_2.rb
163
+ - spec/fixtures/extractor_fixtures/random_file.txt
164
+ - spec/fixtures/extractor_fixtures/subfolder/subfile.py
165
+ - spec/fixtures/extractor_fixtures/subfolder/subfile_1.rb
166
+ - spec/fixtures/extractor_fixtures/subfolder/subfile_2.rb
167
+ - spec/fixtures/parser_base_shared_fixture.rb
168
+ - spec/shared/parser_base_shared.rb
169
+ - spec/spec_helper.rb
170
+ - spec/unit/extractor_spec.rb
171
+ - spec/unit/parser/ruby_spec.rb
172
+ - spec/unit/parser/slim_spec.rb
173
+ - spec/unit/po_entry_spec.rb
174
+ - spec/unit/po_file_spec.rb
175
+ homepage: https://github.com/mntnorv/puttext
176
+ licenses:
177
+ - MIT
178
+ metadata: {}
179
+ post_install_message:
180
+ rdoc_options: []
181
+ require_paths:
182
+ - lib
183
+ required_ruby_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: 2.0.0
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ requirements: []
194
+ rubyforge_project:
195
+ rubygems_version: 2.6.11
196
+ signing_key:
197
+ specification_version: 4
198
+ summary: Extract gettext strings from Ruby source
199
+ test_files: []