mongoid_monkey 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,461 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Extensions::BigDecimal do
4
+
5
+ let(:big_decimal) do
6
+ BigDecimal.new("123456.789")
7
+ end
8
+
9
+ describe ".demongoize" do
10
+
11
+ context "when the the value is an empty String" do
12
+
13
+ let(:string) do
14
+ ""
15
+ end
16
+
17
+ it "returns nil" do
18
+ expect(BigDecimal.demongoize(string)).to be_nil
19
+ end
20
+ end
21
+
22
+ context "when the the value is a numeric String" do
23
+
24
+ let(:string) do
25
+ "123456.789"
26
+ end
27
+
28
+ it "returns a BigDecimal" do
29
+ expect(BigDecimal.demongoize(string)).to eq(BigDecimal.new(string))
30
+ end
31
+ end
32
+
33
+ context "when the the value is the numeric String zero" do
34
+
35
+ let(:string) do
36
+ "0.0"
37
+ end
38
+
39
+ it "returns a BigDecimal" do
40
+ expect(BigDecimal.demongoize(string)).to eq(BigDecimal.new(string))
41
+ end
42
+ end
43
+
44
+ context "when the the value is the numeric String negative zero" do
45
+
46
+ let(:string) do
47
+ "-0.0"
48
+ end
49
+
50
+ it "returns a BigDecimal" do
51
+ expect(BigDecimal.demongoize(string)).to eq(BigDecimal.new(string))
52
+ end
53
+ end
54
+
55
+ context "when the the value is a non-numeric String" do
56
+
57
+ let(:string) do
58
+ "1a2"
59
+ end
60
+
61
+ it "returns nil" do
62
+ expect(BigDecimal.demongoize(string)).to be_nil
63
+ end
64
+ end
65
+
66
+ context "when the value is nil" do
67
+
68
+ it "returns nil" do
69
+ expect(BigDecimal.demongoize(nil)).to be_nil
70
+ end
71
+ end
72
+
73
+ context "when the the value is true" do
74
+
75
+ let(:value) do
76
+ true
77
+ end
78
+
79
+ it "returns nil" do
80
+ expect(BigDecimal.demongoize(value)).to be_nil
81
+ end
82
+ end
83
+
84
+ context "when the the value is false" do
85
+
86
+ let(:value) do
87
+ false
88
+ end
89
+
90
+ it "returns nil" do
91
+ expect(BigDecimal.demongoize(value)).to be_nil
92
+ end
93
+ end
94
+
95
+ context "when the value is an Integer" do
96
+
97
+ let(:integer) do
98
+ 123456
99
+ end
100
+
101
+ it "returns an integer" do
102
+ expect(BigDecimal.demongoize(integer)).to eq(integer)
103
+ end
104
+ end
105
+
106
+ context "when the value is a Float" do
107
+
108
+ let(:float) do
109
+ 123456.789
110
+ end
111
+
112
+ it "returns a float" do
113
+ expect(BigDecimal.demongoize(float)).to eq(float)
114
+ end
115
+ end
116
+
117
+ context "when the value is the String 'NaN'" do
118
+
119
+ let(:nan) do
120
+ "NaN"
121
+ end
122
+
123
+ let(:demongoized) do
124
+ BigDecimal.demongoize(nan)
125
+ end
126
+
127
+ it "returns a BigDecimal" do
128
+ expect(demongoized).to be_a(BigDecimal)
129
+ end
130
+
131
+ it "is a NaN BigDecimal" do
132
+ expect(demongoized).to be_nan
133
+ end
134
+ end
135
+
136
+ context "when the value is the String 'Infinity'" do
137
+
138
+ let(:infinity) do
139
+ "Infinity"
140
+ end
141
+
142
+ let(:demongoized) do
143
+ BigDecimal.demongoize(infinity)
144
+ end
145
+
146
+ it "returns a BigDecimal" do
147
+ expect(demongoized).to be_a(BigDecimal)
148
+ end
149
+
150
+ it "is a infinity BigDecimal" do
151
+ expect(demongoized.infinite?).to eq 1
152
+ end
153
+ end
154
+
155
+ context "when the value is the String '-Infinity'" do
156
+
157
+ let(:neg_infinity) do
158
+ "-Infinity"
159
+ end
160
+
161
+ let(:demongoized) do
162
+ BigDecimal.demongoize(neg_infinity)
163
+ end
164
+
165
+ it "returns a BigDecimal" do
166
+ expect(demongoized).to be_a(BigDecimal)
167
+ end
168
+
169
+ it "is a negative infinity BigDecimal" do
170
+ expect(demongoized.infinite?).to eq -1
171
+ end
172
+ end
173
+ end
174
+
175
+ describe ".mongoize" do
176
+
177
+ context "when the value is a BigDecimal" do
178
+
179
+ it "returns a string" do
180
+ expect(BigDecimal.mongoize(big_decimal)).to eq(big_decimal.to_s)
181
+ end
182
+ end
183
+
184
+ context "when the the value is the BigDecimal zero" do
185
+
186
+ let(:big_decimal) do
187
+ BigDecimal.new("0.0")
188
+ end
189
+
190
+ it "returns a BigDecimal" do
191
+ expect(BigDecimal.mongoize(big_decimal)).to eq(big_decimal.to_s)
192
+ end
193
+ end
194
+
195
+ context "when the the value is the BigDecimal negative zero" do
196
+
197
+ let(:big_decimal) do
198
+ BigDecimal.new("-0.0")
199
+ end
200
+
201
+ it "returns a BigDecimal" do
202
+ expect(BigDecimal.mongoize(big_decimal)).to eq(big_decimal.to_s)
203
+ end
204
+ end
205
+
206
+ context "when the the value is an empty String" do
207
+
208
+ let(:string) do
209
+ ""
210
+ end
211
+
212
+ it "returns nil" do
213
+ expect(BigDecimal.mongoize(string)).to be_nil
214
+ end
215
+ end
216
+
217
+ context "when the value is an integer numeric String" do
218
+
219
+ let(:string) do
220
+ "123456"
221
+ end
222
+
223
+ it "returns the String" do
224
+ expect(BigDecimal.mongoize(string)).to eq string
225
+ end
226
+ end
227
+
228
+ context "when the value is a float numeric String" do
229
+
230
+ let(:string) do
231
+ "123456.789"
232
+ end
233
+
234
+ it "returns the String" do
235
+ expect(BigDecimal.mongoize(string)).to eq string
236
+ end
237
+ end
238
+
239
+ context "when the the value is a non-numeric String" do
240
+
241
+ let(:string) do
242
+ "1a2"
243
+ end
244
+
245
+ it "returns nil" do
246
+ expect(BigDecimal.mongoize(string)).to be_nil
247
+ end
248
+ end
249
+
250
+ context "when the value is nil" do
251
+
252
+ it "returns nil" do
253
+ expect(BigDecimal.mongoize(nil)).to be_nil
254
+ end
255
+ end
256
+
257
+ context "when the the value is true" do
258
+
259
+ let(:value) do
260
+ true
261
+ end
262
+
263
+ it "returns nil" do
264
+ expect(BigDecimal.mongoize(value)).to be_nil
265
+ end
266
+ end
267
+
268
+ context "when the the value is false" do
269
+
270
+ let(:value) do
271
+ false
272
+ end
273
+
274
+ it "returns nil" do
275
+ expect(BigDecimal.mongoize(value)).to be_nil
276
+ end
277
+ end
278
+
279
+ context "when the value is an Integer" do
280
+
281
+ it "returns a string" do
282
+ expect(BigDecimal.mongoize(123456)).to eq("123456")
283
+ end
284
+ end
285
+
286
+ context "when the value is a Float" do
287
+
288
+ it "returns a string" do
289
+ expect(BigDecimal.mongoize(123456.789)).to eq("123456.789")
290
+ end
291
+ end
292
+
293
+ context "when the value is String NaN" do
294
+
295
+ let(:nan) do
296
+ "NaN"
297
+ end
298
+
299
+ it "returns a String" do
300
+ expect(BigDecimal.mongoize(nan)).to eq("NaN")
301
+ end
302
+ end
303
+
304
+ context "when the value is String Infinity" do
305
+
306
+ let(:infinity) do
307
+ "Infinity"
308
+ end
309
+
310
+ it "returns a String" do
311
+ expect(BigDecimal.mongoize(infinity)).to eq("Infinity")
312
+ end
313
+ end
314
+
315
+ context "when the value is String negative Infinity" do
316
+
317
+ let(:neg_infinity) do
318
+ "-Infinity"
319
+ end
320
+
321
+ it "returns a String" do
322
+ expect(BigDecimal.mongoize(neg_infinity)).to eq("-Infinity")
323
+ end
324
+ end
325
+
326
+ context "when the value is BigDecimal NaN" do
327
+
328
+ let(:nan) do
329
+ BigDecimal.new("NaN")
330
+ end
331
+
332
+ it "returns a String" do
333
+ expect(BigDecimal.mongoize(nan)).to eq("NaN")
334
+ end
335
+ end
336
+
337
+ context "when the value is BigDecimal Infinity" do
338
+
339
+ let(:infinity) do
340
+ BigDecimal.new("Infinity")
341
+ end
342
+
343
+ it "returns a String" do
344
+ expect(BigDecimal.mongoize(infinity)).to eq("Infinity")
345
+ end
346
+ end
347
+
348
+ context "when the value is BigDecimal negative Infinity" do
349
+
350
+ let(:neg_infinity) do
351
+ BigDecimal.new("-Infinity")
352
+ end
353
+
354
+ it "returns a String" do
355
+ expect(BigDecimal.mongoize(neg_infinity)).to eq("-Infinity")
356
+ end
357
+ end
358
+
359
+ context "when the value is the constant Float::NAN" do
360
+
361
+ let(:nan) do
362
+ Float::NAN
363
+ end
364
+
365
+ it "returns a String" do
366
+ expect(BigDecimal.mongoize(nan)).to eq("NaN")
367
+ end
368
+ end
369
+
370
+ context "when the value is constant Float::INFINITY" do
371
+
372
+ let(:infinity) do
373
+ Float::INFINITY
374
+ end
375
+
376
+ it "returns a String" do
377
+ expect(BigDecimal.mongoize(infinity)).to eq("Infinity")
378
+ end
379
+ end
380
+
381
+ context "when the value is constant Float::INFINITY * -1" do
382
+
383
+ let(:neg_infinity) do
384
+ Float::INFINITY * -1
385
+ end
386
+
387
+ it "returns a String" do
388
+ expect(BigDecimal.mongoize(neg_infinity)).to eq("-Infinity")
389
+ end
390
+ end
391
+ end
392
+
393
+ describe "#mongoize" do
394
+
395
+ it "returns a string" do
396
+ expect(big_decimal.mongoize).to eq(big_decimal.to_s)
397
+ end
398
+ end
399
+
400
+ describe "#numeric?" do
401
+
402
+ it "returns true" do
403
+ expect(big_decimal.numeric?).to eq(true)
404
+ end
405
+ end
406
+
407
+ describe "String#numeric?" do
408
+
409
+ describe "#numeric?" do
410
+
411
+ context "when the string is an integer" do
412
+
413
+ it "returns true" do
414
+ expect("1234").to be_numeric
415
+ end
416
+ end
417
+
418
+ context "when string is a float" do
419
+
420
+ it "returns true" do
421
+ expect("1234.123").to be_numeric
422
+ end
423
+ end
424
+
425
+ context "when the string is has exponents" do
426
+
427
+ it "returns true" do
428
+ expect("1234.123123E4").to be_numeric
429
+ end
430
+ end
431
+
432
+ context "when the string is non numeric" do
433
+
434
+ it "returns false" do
435
+ expect("blah").to_not be_numeric
436
+ end
437
+ end
438
+
439
+ context "when the string is NaN" do
440
+
441
+ it "returns false" do
442
+ expect("NaN").to be_numeric
443
+ end
444
+ end
445
+
446
+ context "when the string is Infinity" do
447
+
448
+ it "returns false" do
449
+ expect("Infinity").to be_numeric
450
+ end
451
+ end
452
+
453
+ context "when the string is -Infinity" do
454
+
455
+ it "returns false" do
456
+ expect("-Infinity").to be_numeric
457
+ end
458
+ end
459
+ end
460
+ end
461
+ end