/
translation_spec.rb
324 lines (275 loc) · 11.8 KB
/
translation_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
require 'spec_helper'
describe "DataMagic translations" do
context "when delivering data" do
let(:example) { (Class.new { include DataMagic }).new }
def set_field_value(value)
expect(DataMagic).to receive(:yml).twice.and_return({'key' => {'field' => value}})
end
it "should deliver the hash from the yaml" do
set_field_value 'value'
expect(example.data_for('key')).to have_field_value 'value'
end
it "should allow you to use a symbol for the key" do
set_field_value 'value'
expect(example.data_for(:key)).to have_field_value 'value'
end
it "should default to use a file named 'default.yml'" do
hsh = double('hash')
DataMagic.yml_directory = 'test'
expect(File).to receive(:read).with("test/default.yml")
expect(ERB).to receive(:new).and_return hsh
expect(hsh).to receive(:result)
expect(YAML).to receive(:load).and_return(test: 'test')
expect(DataMagic).to receive(:yml).and_return(nil)
expect(DataMagic).to receive(:yml).and_return({'key' => {'field' => 'value'}})
expect(example.data_for('key')).to have_field_value 'value'
end
it "should clone the data returned so it can be resued" do
yaml = double('yaml')
expect(yaml).to receive(:merge).and_return(yaml)
expect(DataMagic).to receive(:yml).twice.and_return(yaml)
expect(yaml).to receive(:[]).and_return(yaml)
expect(yaml).to receive(:deep_copy).and_return({'field' => 'value'})
expect(example.data_for('key')).to have_field_value 'value'
end
it "should merge the provided data with the yaml data" do
yaml = double('yaml')
expect(DataMagic).to receive(:yml).twice.and_return(yaml)
expect(yaml).to receive(:[]).and_return(yaml)
expect(yaml).to receive(:merge).and_return(yaml)
expect(yaml).to receive(:deep_copy).and_return({'field' => 'value'})
expect(example.data_for('key')).to have_field_value 'value'
end
context "translating random names" do
it "should add a name" do
expect(Faker::Name).to receive(:name).and_return('Joseph')
set_field_value '~full_name'
expect(example.data_for('key')).to have_field_value 'Joseph'
end
it "should add first name" do
expect(Faker::Name).to receive(:first_name).and_return('Sam')
set_field_value '~first_name'
expect(example.data_for('key')).to have_field_value 'Sam'
end
it "should add last name" do
expect(Faker::Name).to receive(:last_name).and_return('Smith')
set_field_value '~last_name'
expect(example.data_for('key')).to have_field_value 'Smith'
end
it "should add name prefix" do
expect(Faker::Name).to receive(:prefix).and_return("Mr")
set_field_value '~name_prefix'
expect(example.data_for('key')).to have_field_value 'Mr'
end
it "should add name suffix" do
expect(Faker::Name).to receive(:suffix).and_return('Jr')
set_field_value '~name_suffix'
expect(example.data_for('key')).to have_field_value 'Jr'
end
end
context "translating random addresses" do
it "should add a street address" do
expect(Faker::Address).to receive(:street_address).and_return("123 Main")
set_field_value '~street_address'
expect(example.data_for('key')).to have_field_value '123 Main'
end
it "should add a city" do
expect(Faker::Address).to receive(:city).and_return('Cleveland')
set_field_value '~city'
expect(example.data_for('key')).to have_field_value 'Cleveland'
end
it "should add a state" do
expect(Faker::Address).to receive(:state).and_return('Ohio')
set_field_value '~state'
expect(example.data_for('key')).to have_field_value 'Ohio'
end
it "should add a state abbreviation" do
expect(Faker::Address).to receive(:state_abbr).and_return('OH')
set_field_value '~state_abbr'
expect(example.data_for('key')).to have_field_value 'OH'
end
it "should add a zip code" do
expect(Faker::Address).to receive(:zip).and_return('11111')
set_field_value '~zip_code'
expect(example.data_for('key')).to have_field_value '11111'
end
it "should add a country" do
expect(Faker::Address).to receive(:country).and_return("United States")
set_field_value '~country'
expect(example.data_for('key')).to have_field_value 'United States'
end
it "should add a secondary address" do
expect(Faker::Address).to receive(:secondary_address).and_return('2nd floor')
set_field_value '~secondary_address'
expect(example.data_for('key')).to have_field_value '2nd floor'
end
end
context "translating company names" do
it "should add a company name" do
expect(Faker::Company).to receive(:name).and_return('LeanDog')
set_field_value '~company_name'
expect(example.data_for('key')).to have_field_value 'LeanDog'
end
end
context "translating business" do
it "should add a credit card number" do
expect(Faker::Business).to receive(:credit_card_number).and_return('123')
set_field_value '~credit_card_number'
expect(example.data_for('key')).to have_field_value '123'
end
it "should add credit card type" do
expect(Faker::Business).to receive(:credit_card_type).
and_return('visa')
set_field_value '~credit_card_type'
expect(example.data_for('key')).to have_field_value 'visa'
end
end
context "translating internet names" do
it "should add an email address" do
expect(Faker::Internet).to receive(:email).and_return('buddy@example.com')
set_field_value '~email_address'
expect(example.data_for('key')).to have_field_value 'buddy@example.com'
end
it "should add a domain name" do
expect(Faker::Internet).to receive(:domain_name).and_return("google.com")
set_field_value '~domain_name'
expect(example.data_for('key')).to have_field_value 'google.com'
end
it "should add a user name" do
expect(Faker::Internet).to receive(:user_name).and_return('very_cheezy')
set_field_value '~user_name'
expect(example.data_for('key')).to have_field_value 'very_cheezy'
end
end
context "translating phone numbers" do
it "shold add a phone number" do
expect(Faker::PhoneNumber).to receive(:phone_number).and_return('555-555-5555')
set_field_value '~phone_number'
expect(example.data_for('key')).to have_field_value '555-555-5555'
end
end
context "translating random phrases" do
it "should add a catch phrase" do
expect(Faker::Company).to receive(:catch_phrase).and_return('Ruby is cool')
set_field_value '~catch_phrase'
expect(example.data_for('key')).to have_field_value 'Ruby is cool'
end
it "should add random words" do
expect(Faker::Lorem).to receive(:words).and_return(['random', 'words'])
set_field_value '~words'
expect(example.data_for('key')).to have_field_value 'random words'
end
it "should default to returning 3 words" do
set_field_value '~words'
expect(example.data_for('key')['field'].split.size).to eql 3
end
it "should allow you to specify the number of words" do
set_field_value '~words(4)'
expect(example.data_for('key')['field'].split.size).to eql 4
end
it "should add a random sentence" do
expect(Faker::Lorem).to receive(:sentence).and_return('a sentence')
set_field_value '~sentence'
expect(example.data_for('key')).to have_field_value 'a sentence'
end
it "should default to returning a minimum of 4 words" do
set_field_value '~sentence'
expect(example.data_for('key')['field'].split.size).to be >= 4
end
it "should allow you to specify a minimum word count" do
set_field_value '~sentence(20)'
expect(example.data_for('key')['field'].split.size).to be >= 20
end
it "should add sentences" do
expect(Faker::Lorem).to receive(:sentences).and_return(['this is sentences'])
set_field_value '~sentences'
expect(example.data_for('key')).to have_field_value 'this is sentences'
end
it "should default to returning a default of 3 sentences" do
set_field_value '~sentences'
expect(example.data_for('key')['field'].split('.').size).to be >= 3
end
it "should allow you to specify the number of sentences" do
set_field_value '~sentences(10)'
expect(example.data_for('key')['field'].split('.').size).to be >= 10
end
it "should add a paragraphs" do
expect(Faker::Lorem).to receive(:paragraphs).and_return(['this is a paragraph'])
set_field_value '~paragraphs'
expect(example.data_for('key')).to have_field_value 'this is a paragraph'
end
it "should return 3 paragraphs by default" do
set_field_value '~paragraphs'
expect(example.data_for('key')['field'].split('\n\n').size).to eql 3
end
it "should allow you to specify the number of paragraphs" do
set_field_value '~paragraphs(10)'
expect(example.data_for('key')['field'].split('\n\n').size).to eql 10
end
it "should add characters" do
expect(Faker::Lorem).to receive(:characters).and_return('abcdefg')
set_field_value '~characters'
expect(example.data_for('key')).to have_field_value 'abcdefg'
end
end
context "translating boolean values" do
it "should resolve true" do
set_field_value true
expect(example.data_for('key')).to have_field_value true
end
it "should resolve false" do
set_field_value false
expect(example.data_for('key')).to have_field_value false
end
end
context "with numeric values" do
it "doesn't translate values" do
set_field_value(1)
expect(example.data_for("key")).to have_field_value 1
end
end
context "with values not in the yaml" do
it "throws a ArgumentError" do
expect { example.data_for("inexistant_key") }.to raise_error ArgumentError
end
end
context "providing date values" do
it "should provide today's date" do
set_field_value '~today'
expect(example.data_for('key')).to have_field_value Date.today.strftime('%D')
end
it "should provide tomorrow's date" do
set_field_value '~tomorrow'
tomorrow = Date.today + 1
expect(example.data_for('key')).to have_field_value tomorrow.strftime('%D')
end
it "should provide yesterday's date" do
set_field_value '~yesterday'
yesterday = Date.today - 1
expect(example.data_for('key')).to have_field_value yesterday.strftime('%D')
end
it "should provide a date that is some number of days from now" do
set_field_value '~5.days_from_today'
the_date = Date.today + 5
expect(example.data_for('key')).to have_field_value the_date.strftime('%D')
end
it "should provide a date that is some number of days ago" do
set_field_value '~5.days_ago'
the_date = Date.today - 5
expect(example.data_for('key')).to have_field_value the_date.strftime('%D')
end
end
context "should fail when translation call methos is not defined" do
it "should fail if method does not exist" do
set_field_value '~non_existing_method'
expect{example.data_for('key')}.to raise_error(/non_existing_method/)
end
end
context "array translation test" do
it "should be able to translate " do
set_field_value ["~'user' + 'name'", 'second']
expect(example.data_for('key')).to have_field_value ['username','second']
end
end
end
end