lotus-utils 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +2 -1
- data/CHANGELOG.md +39 -0
- data/CONTRIBUTING.md +44 -0
- data/Gemfile +3 -3
- data/{LICENSE.txt → LICENSE.md} +0 -0
- data/README.md +9 -7
- data/Rakefile +7 -0
- data/lib/lotus/utils.rb +1 -1
- data/lib/lotus/utils/class_attribute.rb +2 -0
- data/lib/lotus/utils/kernel.rb +773 -0
- data/lib/lotus/utils/version.rb +1 -1
- data/test/kernel_test.rb +1752 -0
- data/test/test_helper.rb +13 -8
- data/test/version_test.rb +1 -1
- metadata +9 -5
- data/.coveralls.yml +0 -2
data/lib/lotus/utils/version.rb
CHANGED
data/test/kernel_test.rb
ADDED
@@ -0,0 +1,1752 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'bigdecimal'
|
3
|
+
require 'lotus/utils/kernel'
|
4
|
+
|
5
|
+
describe Lotus::Utils::Kernel do
|
6
|
+
describe '.Array' do
|
7
|
+
describe 'successful operations' do
|
8
|
+
before do
|
9
|
+
ResultSet = Struct.new(:records) do
|
10
|
+
def to_a
|
11
|
+
records.to_a.sort
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Response = Struct.new(:status, :headers, :body) do
|
16
|
+
def to_ary
|
17
|
+
[status, headers, body]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
@result = Lotus::Utils::Kernel.Array(input)
|
22
|
+
end
|
23
|
+
|
24
|
+
after do
|
25
|
+
Object.send(:remove_const, :ResultSet)
|
26
|
+
Object.send(:remove_const, :Response)
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'when nil is given' do
|
30
|
+
let(:input) { nil }
|
31
|
+
|
32
|
+
it 'returns an empty array' do
|
33
|
+
@result.must_equal nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'when true is given' do
|
38
|
+
let(:input) { true }
|
39
|
+
|
40
|
+
it 'returns an empty array' do
|
41
|
+
@result.must_equal [true]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'when false is given' do
|
46
|
+
let(:input) { false }
|
47
|
+
|
48
|
+
it 'returns an empty array' do
|
49
|
+
@result.must_equal [false]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'when an object is given' do
|
54
|
+
let(:input) { Object.new }
|
55
|
+
|
56
|
+
it 'returns an array' do
|
57
|
+
@result.must_equal [input]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'when an array is given' do
|
62
|
+
let(:input) { [Object.new] }
|
63
|
+
|
64
|
+
it 'returns an array' do
|
65
|
+
@result.must_equal input
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'when a nested array is given' do
|
70
|
+
let(:input) { [1, [2, 3]] }
|
71
|
+
|
72
|
+
it 'returns a flatten array' do
|
73
|
+
@result.must_equal [1,2,3]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'when an array with nil values is given' do
|
78
|
+
let(:input) { [1, [nil, 3]] }
|
79
|
+
|
80
|
+
it 'returns a compacted array' do
|
81
|
+
@result.must_equal [1,3]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'when an array with duplicated values is given' do
|
86
|
+
let(:input) { [2, [2, 3]] }
|
87
|
+
|
88
|
+
it 'returns an array with uniq values' do
|
89
|
+
@result.must_equal [2,3]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'when a set is given' do
|
94
|
+
let(:input) { Set.new([33,12]) }
|
95
|
+
|
96
|
+
it 'returns an array with uniq values' do
|
97
|
+
@result.must_equal [33, 12]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe 'when a object that implements #to_a is given' do
|
102
|
+
let(:input) { ResultSet.new([2,1,3]) }
|
103
|
+
|
104
|
+
it 'returns an array' do
|
105
|
+
@result.must_equal [1,2,3]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe 'when a object that implements #to_ary is given' do
|
110
|
+
let(:input) { Response.new(200, {}, 'hello') }
|
111
|
+
|
112
|
+
it 'returns an array' do
|
113
|
+
@result.must_equal [200, {}, 'hello']
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe 'failure operations' do
|
119
|
+
describe "when a object that doesn't implements #nil? is given" do
|
120
|
+
let(:input) { BasicObject.new }
|
121
|
+
|
122
|
+
it 'raises an error' do
|
123
|
+
-> { Lotus::Utils::Kernel.Array(input) }.must_raise NoMethodError
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe '.Set' do
|
130
|
+
before do
|
131
|
+
UuidSet = Class.new do
|
132
|
+
def initialize(*uuids)
|
133
|
+
@uuids = uuids
|
134
|
+
end
|
135
|
+
|
136
|
+
def to_set
|
137
|
+
Set.new.tap do |set|
|
138
|
+
@uuids.each {|uuid| set.add(uuid) }
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
BaseObject = Class.new(BasicObject) do
|
144
|
+
def nil?
|
145
|
+
false
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
after do
|
151
|
+
Object.send(:remove_const, :UuidSet)
|
152
|
+
Object.send(:remove_const, :BaseObject)
|
153
|
+
end
|
154
|
+
|
155
|
+
describe 'successful operations' do
|
156
|
+
before do
|
157
|
+
@result = Lotus::Utils::Kernel.Set(input)
|
158
|
+
end
|
159
|
+
|
160
|
+
describe 'when nil is given' do
|
161
|
+
let(:input) { nil }
|
162
|
+
|
163
|
+
it 'returns nil' do
|
164
|
+
@result.must_equal nil
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
describe 'when true is given' do
|
169
|
+
let(:input) { true }
|
170
|
+
|
171
|
+
it 'returns a set' do
|
172
|
+
@result.must_equal Set.new([true])
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe 'when false is given' do
|
177
|
+
let(:input) { false }
|
178
|
+
|
179
|
+
it 'returns a set' do
|
180
|
+
@result.must_equal Set.new([false])
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe 'when an object is given' do
|
185
|
+
let(:input) { Object.new }
|
186
|
+
|
187
|
+
it 'returns an set' do
|
188
|
+
@result.must_equal Set.new([input])
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
describe 'when an array is given' do
|
193
|
+
let(:input) { [1] }
|
194
|
+
|
195
|
+
it 'returns an set' do
|
196
|
+
@result.must_equal Set.new(input)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe 'when an hash is given' do
|
201
|
+
let(:input) { Hash[a: 1] }
|
202
|
+
|
203
|
+
it 'returns an set' do
|
204
|
+
@result.must_equal Set.new([[:a, 1]])
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
describe 'when a set is given' do
|
209
|
+
let(:input) { Set.new([Object.new]) }
|
210
|
+
|
211
|
+
it 'returns self' do
|
212
|
+
@result.must_equal input
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe 'when a nested array is given' do
|
217
|
+
let(:input) { [1, [2, 3]] }
|
218
|
+
|
219
|
+
it 'returns it wraps in a set' do
|
220
|
+
@result.must_equal Set.new([1, [2, 3]])
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
describe 'when an array with nil values is given' do
|
225
|
+
let(:input) { [1, [nil, 3]] }
|
226
|
+
|
227
|
+
it 'returns it wraps in a set' do
|
228
|
+
@result.must_equal Set.new([1, [nil, 3]])
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
describe 'when an set with duplicated values is given' do
|
233
|
+
let(:input) { [2, 3, 3] }
|
234
|
+
|
235
|
+
it 'returns an set with uniq values' do
|
236
|
+
@result.must_equal Set.new([2,3])
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
describe 'when an set with nested duplicated values is given' do
|
241
|
+
let(:input) { [2, [2, 3]] }
|
242
|
+
|
243
|
+
it 'returns it wraps in a set' do
|
244
|
+
@result.must_equal Set.new([2, [2,3]])
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
describe 'when a object that implements #to_set is given' do
|
249
|
+
let(:input) { UuidSet.new(*args) }
|
250
|
+
let(:args) { [SecureRandom.uuid, SecureRandom.uuid] }
|
251
|
+
|
252
|
+
it 'returns an set' do
|
253
|
+
@result.must_equal Set.new(args)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
describe 'failure operations' do
|
259
|
+
describe "when a an object that doesn't implement #nil?" do
|
260
|
+
let(:input) { BasicObject.new }
|
261
|
+
|
262
|
+
it 'raises error' do
|
263
|
+
-> { Lotus::Utils::Kernel.Set(input) }.must_raise(NoMethodError)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
describe "when a an object that doesn't implement #respond_to?" do
|
268
|
+
let(:input) { BaseObject.new }
|
269
|
+
|
270
|
+
it 'raises error' do
|
271
|
+
-> { Lotus::Utils::Kernel.Set(input) }.must_raise(NoMethodError)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
describe '.Hash' do
|
278
|
+
before do
|
279
|
+
Room = Class.new do
|
280
|
+
def initialize(*args)
|
281
|
+
@args = args
|
282
|
+
end
|
283
|
+
|
284
|
+
def to_h
|
285
|
+
Hash[*@args]
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
Record = Class.new do
|
290
|
+
def initialize(attributes = {})
|
291
|
+
@attributes = attributes
|
292
|
+
end
|
293
|
+
|
294
|
+
def to_hash
|
295
|
+
@attributes
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
BaseObject = Class.new(BasicObject) do
|
300
|
+
def nil?
|
301
|
+
false
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
after do
|
307
|
+
Object.send(:remove_const, :Room)
|
308
|
+
Object.send(:remove_const, :Record)
|
309
|
+
Object.send(:remove_const, :BaseObject)
|
310
|
+
end
|
311
|
+
|
312
|
+
describe 'successful operations' do
|
313
|
+
before do
|
314
|
+
@result = Lotus::Utils::Kernel.Hash(input)
|
315
|
+
end
|
316
|
+
|
317
|
+
describe 'when nil is given' do
|
318
|
+
let(:input) { nil }
|
319
|
+
|
320
|
+
it 'returns an empty hash' do
|
321
|
+
@result.must_equal nil
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
describe 'when an hash is given' do
|
326
|
+
let(:input) { Hash[l: 23] }
|
327
|
+
|
328
|
+
it 'returns the input' do
|
329
|
+
@result.must_equal input
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
describe 'when an array is given' do
|
334
|
+
let(:input) { [[:a, 1]] }
|
335
|
+
|
336
|
+
it 'returns an hash' do
|
337
|
+
@result.must_equal Hash[a: 1]
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
describe 'when a set is given' do
|
342
|
+
let(:input) { Set.new([['x', 12]]) }
|
343
|
+
|
344
|
+
it 'returns an hash' do
|
345
|
+
@result.must_equal Hash['x' => 12]
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
describe 'when a object that implements #to_h is given' do
|
350
|
+
let(:input) { Room.new(:key, 123456) }
|
351
|
+
|
352
|
+
it 'returns an hash' do
|
353
|
+
@result.must_equal Hash[key: 123456]
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
describe 'when a object that implements #to_hash is given' do
|
358
|
+
let(:input) { Record.new(name: 'L') }
|
359
|
+
|
360
|
+
it 'returns an hash' do
|
361
|
+
@result.must_equal Hash[name: 'L']
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
describe 'failure operations' do
|
367
|
+
describe "when a an object that doesn't implement #nil?" do
|
368
|
+
let(:input) { BasicObject.new }
|
369
|
+
|
370
|
+
it 'raises error' do
|
371
|
+
-> { Lotus::Utils::Kernel.Hash(input) }.must_raise(NoMethodError)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
describe "when a an object that doesn't implement #respond_to?" do
|
376
|
+
let(:input) { BaseObject.new }
|
377
|
+
|
378
|
+
it 'raises error' do
|
379
|
+
-> { Lotus::Utils::Kernel.Hash(input) }.must_raise(NoMethodError)
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
describe 'when true is given' do
|
384
|
+
let(:input) { true }
|
385
|
+
|
386
|
+
it 'raises error' do
|
387
|
+
-> { Lotus::Utils::Kernel.Hash(input) }.must_raise(TypeError)
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
describe 'when false is given' do
|
392
|
+
let(:input) { false }
|
393
|
+
|
394
|
+
it 'raises error' do
|
395
|
+
-> { Lotus::Utils::Kernel.Hash(input) }.must_raise(TypeError)
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
describe 'when an array with one element is given' do
|
400
|
+
let(:input) { [1] }
|
401
|
+
|
402
|
+
it 'raises error' do
|
403
|
+
-> { Lotus::Utils::Kernel.Hash(input) }.must_raise(TypeError)
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
if RUBY_VERSION >= '2.1'
|
408
|
+
describe 'when an array with two elements is given' do
|
409
|
+
let(:input) { [:a, 1] }
|
410
|
+
|
411
|
+
it 'raises error' do
|
412
|
+
-> { Lotus::Utils::Kernel.Hash(input) }.must_raise(TypeError)
|
413
|
+
end
|
414
|
+
end
|
415
|
+
end
|
416
|
+
|
417
|
+
describe 'when a set of one element is given' do
|
418
|
+
let(:input) { Set.new([1]) }
|
419
|
+
|
420
|
+
it 'raises error' do
|
421
|
+
-> { Lotus::Utils::Kernel.Hash(input) }.must_raise(TypeError)
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
if RUBY_VERSION >= '2.1'
|
426
|
+
describe 'when a set of two elements is given' do
|
427
|
+
let(:input) { Set.new([:a, 1]) }
|
428
|
+
|
429
|
+
it 'raises error' do
|
430
|
+
-> { Lotus::Utils::Kernel.Hash(input) }.must_raise(TypeError)
|
431
|
+
end
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end
|
435
|
+
end
|
436
|
+
|
437
|
+
describe '.Integer' do
|
438
|
+
describe 'successful operations' do
|
439
|
+
before do
|
440
|
+
PersonFavNumber = Struct.new(:name) do
|
441
|
+
def to_int
|
442
|
+
23
|
443
|
+
end
|
444
|
+
end
|
445
|
+
|
446
|
+
@result = Lotus::Utils::Kernel.Integer(input)
|
447
|
+
end
|
448
|
+
|
449
|
+
after do
|
450
|
+
Object.send(:remove_const, :PersonFavNumber)
|
451
|
+
end
|
452
|
+
|
453
|
+
describe 'when nil is given' do
|
454
|
+
let(:input) { nil }
|
455
|
+
|
456
|
+
it 'returns nil' do
|
457
|
+
@result.must_equal nil
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
describe 'when a fixnum given' do
|
462
|
+
let(:input) { 1 }
|
463
|
+
|
464
|
+
it 'returns an integer' do
|
465
|
+
@result.must_equal 1
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
describe 'when a float is given' do
|
470
|
+
let(:input) { 1.2 }
|
471
|
+
|
472
|
+
it 'returns an integer' do
|
473
|
+
@result.must_equal 1
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|
477
|
+
describe 'when a string given' do
|
478
|
+
let(:input) { '23' }
|
479
|
+
|
480
|
+
it 'returns an integer' do
|
481
|
+
@result.must_equal 23
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
describe 'when a string representing a float number is given' do
|
486
|
+
let(:input) { '23.4' }
|
487
|
+
|
488
|
+
it 'returns an integer' do
|
489
|
+
@result.must_equal 23
|
490
|
+
end
|
491
|
+
end
|
492
|
+
|
493
|
+
describe 'when an octal is given' do
|
494
|
+
let(:input) { 011 }
|
495
|
+
|
496
|
+
it 'returns the string representation' do
|
497
|
+
@result.must_equal 9
|
498
|
+
end
|
499
|
+
end
|
500
|
+
|
501
|
+
describe 'when a hex is given' do
|
502
|
+
let(:input) { 0xf5 }
|
503
|
+
|
504
|
+
it 'returns the string representation' do
|
505
|
+
@result.must_equal 245
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
describe 'when a bignum is given' do
|
510
|
+
let(:input) { 13289301283 ** 2 }
|
511
|
+
|
512
|
+
it 'returns an bignum' do
|
513
|
+
@result.must_equal 176605528590345446089
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
describe 'when a bigdecimal is given' do
|
518
|
+
let(:input) { BigDecimal.new('12.0001') }
|
519
|
+
|
520
|
+
it 'returns an bignum' do
|
521
|
+
@result.must_equal 12
|
522
|
+
end
|
523
|
+
end
|
524
|
+
|
525
|
+
describe 'when a complex number is given' do
|
526
|
+
let(:input) { Complex(0.3) }
|
527
|
+
|
528
|
+
it 'returns an integer' do
|
529
|
+
@result.must_equal 0
|
530
|
+
end
|
531
|
+
end
|
532
|
+
|
533
|
+
describe 'when a string representing a complex number is given' do
|
534
|
+
let(:input) { '2.5/1' }
|
535
|
+
|
536
|
+
it 'returns an integer' do
|
537
|
+
@result.must_equal 2
|
538
|
+
end
|
539
|
+
end
|
540
|
+
|
541
|
+
describe 'when a rational number is given' do
|
542
|
+
let(:input) { Rational(0.3) }
|
543
|
+
|
544
|
+
it 'returns an integer' do
|
545
|
+
@result.must_equal 0
|
546
|
+
end
|
547
|
+
end
|
548
|
+
|
549
|
+
describe 'when a string representing a rational number is given' do
|
550
|
+
let(:input) { '2/3' }
|
551
|
+
|
552
|
+
it 'returns an integer' do
|
553
|
+
@result.must_equal 2
|
554
|
+
end
|
555
|
+
end
|
556
|
+
|
557
|
+
describe 'when a time is given' do
|
558
|
+
let(:input) { Time.at(0).utc }
|
559
|
+
|
560
|
+
it 'returns the string representation' do
|
561
|
+
@result.must_equal 0
|
562
|
+
end
|
563
|
+
end
|
564
|
+
|
565
|
+
describe 'when an object that implements #to_int is given' do
|
566
|
+
let(:input) { PersonFavNumber.new('Luca') }
|
567
|
+
|
568
|
+
it 'returns an integer' do
|
569
|
+
@result.must_equal 23
|
570
|
+
end
|
571
|
+
end
|
572
|
+
end
|
573
|
+
|
574
|
+
describe 'failure operations' do
|
575
|
+
describe 'true is given' do
|
576
|
+
let(:input) { true }
|
577
|
+
|
578
|
+
it 'raises error' do
|
579
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(TypeError)
|
580
|
+
end
|
581
|
+
end
|
582
|
+
|
583
|
+
describe 'false is given' do
|
584
|
+
let(:input) { false }
|
585
|
+
|
586
|
+
it 'raises error' do
|
587
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(TypeError)
|
588
|
+
end
|
589
|
+
end
|
590
|
+
|
591
|
+
describe 'when a date is given' do
|
592
|
+
let(:input) { Date.today }
|
593
|
+
|
594
|
+
it 'raises error' do
|
595
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(TypeError)
|
596
|
+
end
|
597
|
+
end
|
598
|
+
|
599
|
+
describe 'when a datetime is given' do
|
600
|
+
let(:input) { DateTime.now }
|
601
|
+
|
602
|
+
it 'raises error' do
|
603
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(TypeError)
|
604
|
+
end
|
605
|
+
end
|
606
|
+
|
607
|
+
describe "when a an object that doesn't implement #nil? " do
|
608
|
+
let(:input) { BasicObject.new }
|
609
|
+
|
610
|
+
it 'raises error' do
|
611
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(NoMethodError)
|
612
|
+
end
|
613
|
+
end
|
614
|
+
|
615
|
+
describe "when a an object that doesn't implement any integer interface" do
|
616
|
+
let(:input) { OpenStruct.new(color: 'purple') }
|
617
|
+
|
618
|
+
it 'raises error' do
|
619
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(TypeError)
|
620
|
+
end
|
621
|
+
end
|
622
|
+
|
623
|
+
describe 'when a bigdecimal infinity is given' do
|
624
|
+
let(:input) { BigDecimal.new('Infinity') }
|
625
|
+
|
626
|
+
it 'raises error' do
|
627
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(FloatDomainError)
|
628
|
+
end
|
629
|
+
end
|
630
|
+
|
631
|
+
describe 'when a bigdecimal NaN is given' do
|
632
|
+
let(:input) { BigDecimal.new('NaN') }
|
633
|
+
|
634
|
+
it 'raises error' do
|
635
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(FloatDomainError)
|
636
|
+
end
|
637
|
+
end
|
638
|
+
|
639
|
+
describe 'when a big complex number is given' do
|
640
|
+
let(:input) { Complex(2,3) }
|
641
|
+
|
642
|
+
it 'raises error' do
|
643
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(RangeError)
|
644
|
+
end
|
645
|
+
end
|
646
|
+
|
647
|
+
describe 'when a big rational number is given' do
|
648
|
+
let(:input) { Rational(-8) ** Rational(1, 3) }
|
649
|
+
|
650
|
+
it 'raises error' do
|
651
|
+
-> { Lotus::Utils::Kernel.Integer(input) }.must_raise(RangeError)
|
652
|
+
end
|
653
|
+
end
|
654
|
+
end
|
655
|
+
end
|
656
|
+
|
657
|
+
describe '.Float' do
|
658
|
+
describe 'successful operations' do
|
659
|
+
before do
|
660
|
+
class Pi
|
661
|
+
def to_f
|
662
|
+
3.14
|
663
|
+
end
|
664
|
+
end
|
665
|
+
|
666
|
+
@result = Lotus::Utils::Kernel.Float(input)
|
667
|
+
end
|
668
|
+
|
669
|
+
after do
|
670
|
+
Object.send(:remove_const, :Pi)
|
671
|
+
end
|
672
|
+
|
673
|
+
describe 'when nil is given' do
|
674
|
+
let(:input) { nil }
|
675
|
+
|
676
|
+
it 'returns nil' do
|
677
|
+
@result.must_equal nil
|
678
|
+
end
|
679
|
+
end
|
680
|
+
|
681
|
+
describe 'when a float is given' do
|
682
|
+
let(:input) { 1.2 }
|
683
|
+
|
684
|
+
it 'returns the argument' do
|
685
|
+
@result.must_equal 1.2
|
686
|
+
end
|
687
|
+
end
|
688
|
+
|
689
|
+
describe 'when a fixnum given' do
|
690
|
+
let(:input) { 1 }
|
691
|
+
|
692
|
+
it 'returns a float' do
|
693
|
+
@result.must_be_kind_of(Float)
|
694
|
+
@result.must_equal 1.0
|
695
|
+
end
|
696
|
+
end
|
697
|
+
|
698
|
+
describe 'when a string given' do
|
699
|
+
let(:input) { '23' }
|
700
|
+
|
701
|
+
it 'returns a float' do
|
702
|
+
@result.must_be_kind_of(Float)
|
703
|
+
@result.must_equal 23.0
|
704
|
+
end
|
705
|
+
end
|
706
|
+
|
707
|
+
describe 'when a string representing a float number is given' do
|
708
|
+
let(:input) { '23.4' }
|
709
|
+
|
710
|
+
it 'returns a float' do
|
711
|
+
@result.must_be_kind_of(Float)
|
712
|
+
@result.must_equal 23.4
|
713
|
+
end
|
714
|
+
end
|
715
|
+
|
716
|
+
describe 'when an octal is given' do
|
717
|
+
let(:input) { 011 }
|
718
|
+
|
719
|
+
it 'returns the base 10 float' do
|
720
|
+
@result.must_equal 9.0
|
721
|
+
end
|
722
|
+
end
|
723
|
+
|
724
|
+
describe 'when a hex is given' do
|
725
|
+
let(:input) { 0xf5 }
|
726
|
+
|
727
|
+
it 'returns the base 10 float' do
|
728
|
+
@result.must_equal 245.0
|
729
|
+
end
|
730
|
+
end
|
731
|
+
|
732
|
+
describe 'when a bignum is given' do
|
733
|
+
let(:input) { 13289301283 ** 2 }
|
734
|
+
|
735
|
+
it 'returns a float' do
|
736
|
+
@result.must_be_kind_of(Float)
|
737
|
+
@result.must_equal 176605528590345446089.0
|
738
|
+
end
|
739
|
+
end
|
740
|
+
|
741
|
+
describe 'when a bigdecimal is given' do
|
742
|
+
let(:input) { BigDecimal.new('12.0001') }
|
743
|
+
|
744
|
+
it 'returns a float' do
|
745
|
+
@result.must_be_kind_of(Float)
|
746
|
+
@result.must_equal 12.0001
|
747
|
+
end
|
748
|
+
end
|
749
|
+
|
750
|
+
describe 'when a bigdecimal infinity is given' do
|
751
|
+
let(:input) { BigDecimal.new('Infinity') }
|
752
|
+
|
753
|
+
it 'returns Infinity' do
|
754
|
+
@result.must_be_kind_of(Float)
|
755
|
+
@result.to_s.must_equal 'Infinity'
|
756
|
+
end
|
757
|
+
end
|
758
|
+
|
759
|
+
describe 'when a bigdecimal NaN is given' do
|
760
|
+
let(:input) { BigDecimal.new('NaN') }
|
761
|
+
|
762
|
+
it 'returns NaN' do
|
763
|
+
@result.must_be_kind_of(Float)
|
764
|
+
@result.to_s.must_equal 'NaN'
|
765
|
+
end
|
766
|
+
end
|
767
|
+
|
768
|
+
describe 'when a complex number is given' do
|
769
|
+
let(:input) { Complex(0.3) }
|
770
|
+
|
771
|
+
it 'returns a float' do
|
772
|
+
@result.must_be_kind_of(Float)
|
773
|
+
@result.must_equal 0.3
|
774
|
+
end
|
775
|
+
end
|
776
|
+
|
777
|
+
describe 'when a string representing a complex number is given' do
|
778
|
+
let(:input) { '2.5/1' }
|
779
|
+
|
780
|
+
it 'returns a float' do
|
781
|
+
@result.must_be_kind_of(Float)
|
782
|
+
@result.must_equal 2.5
|
783
|
+
end
|
784
|
+
end
|
785
|
+
|
786
|
+
describe 'when a rational number is given' do
|
787
|
+
let(:input) { Rational(0.3) }
|
788
|
+
|
789
|
+
it 'returns a float' do
|
790
|
+
@result.must_be_kind_of(Float)
|
791
|
+
@result.must_equal 0.3
|
792
|
+
end
|
793
|
+
end
|
794
|
+
|
795
|
+
describe 'when a string representing a rational number is given' do
|
796
|
+
let(:input) { '2/3' }
|
797
|
+
|
798
|
+
it 'returns a float' do
|
799
|
+
@result.must_be_kind_of(Float)
|
800
|
+
@result.must_equal 2.0
|
801
|
+
end
|
802
|
+
end
|
803
|
+
|
804
|
+
describe 'when a time is given' do
|
805
|
+
let(:input) { Time.at(0).utc }
|
806
|
+
|
807
|
+
it 'returns the float representation' do
|
808
|
+
@result.must_be_kind_of(Float)
|
809
|
+
@result.must_equal 0.0
|
810
|
+
end
|
811
|
+
end
|
812
|
+
|
813
|
+
describe 'when an object that implements #to_int is given' do
|
814
|
+
let(:input) { Pi.new }
|
815
|
+
|
816
|
+
it 'returns a float' do
|
817
|
+
@result.must_be_kind_of(Float)
|
818
|
+
@result.must_equal 3.14
|
819
|
+
end
|
820
|
+
end
|
821
|
+
end
|
822
|
+
|
823
|
+
describe 'failure operations' do
|
824
|
+
describe 'true is given' do
|
825
|
+
let(:input) { true }
|
826
|
+
|
827
|
+
it 'raises error' do
|
828
|
+
-> { Lotus::Utils::Kernel.Float(input) }.must_raise(TypeError)
|
829
|
+
end
|
830
|
+
end
|
831
|
+
|
832
|
+
describe 'false is given' do
|
833
|
+
let(:input) { false }
|
834
|
+
|
835
|
+
it 'raises error' do
|
836
|
+
-> { Lotus::Utils::Kernel.Float(input) }.must_raise(TypeError)
|
837
|
+
end
|
838
|
+
end
|
839
|
+
|
840
|
+
describe 'when a date is given' do
|
841
|
+
let(:input) { Date.today }
|
842
|
+
|
843
|
+
it 'raises error' do
|
844
|
+
-> { Lotus::Utils::Kernel.Float(input) }.must_raise(TypeError)
|
845
|
+
end
|
846
|
+
end
|
847
|
+
|
848
|
+
describe 'when a datetime is given' do
|
849
|
+
let(:input) { DateTime.now }
|
850
|
+
|
851
|
+
it 'raises error' do
|
852
|
+
-> { Lotus::Utils::Kernel.Float(input) }.must_raise(TypeError)
|
853
|
+
end
|
854
|
+
end
|
855
|
+
|
856
|
+
describe "when a an object that doesn't implement #nil? " do
|
857
|
+
let(:input) { BasicObject.new }
|
858
|
+
|
859
|
+
it 'raises error' do
|
860
|
+
-> { Lotus::Utils::Kernel.Float(input) }.must_raise(NoMethodError)
|
861
|
+
end
|
862
|
+
end
|
863
|
+
|
864
|
+
describe "when a an object that doesn't implement any float interface" do
|
865
|
+
let(:input) { OpenStruct.new(color: 'purple') }
|
866
|
+
|
867
|
+
it 'raises error' do
|
868
|
+
-> { Lotus::Utils::Kernel.Float(input) }.must_raise(TypeError)
|
869
|
+
end
|
870
|
+
end
|
871
|
+
|
872
|
+
describe 'when a big complex number is given' do
|
873
|
+
let(:input) { Complex(2,3) }
|
874
|
+
|
875
|
+
it 'raises error' do
|
876
|
+
-> { Lotus::Utils::Kernel.Float(input) }.must_raise(RangeError)
|
877
|
+
end
|
878
|
+
end
|
879
|
+
|
880
|
+
describe 'when a big rational number is given' do
|
881
|
+
let(:input) { Rational(-8) ** Rational(1, 3) }
|
882
|
+
|
883
|
+
it 'raises error' do
|
884
|
+
-> { Lotus::Utils::Kernel.Float(input) }.must_raise(RangeError)
|
885
|
+
end
|
886
|
+
end
|
887
|
+
end
|
888
|
+
end
|
889
|
+
|
890
|
+
describe '.String' do
|
891
|
+
before do
|
892
|
+
Book = Struct.new(:title)
|
893
|
+
|
894
|
+
BaseObject = Class.new(BasicObject) do
|
895
|
+
def nil?
|
896
|
+
false
|
897
|
+
end
|
898
|
+
end
|
899
|
+
|
900
|
+
SimpleObject = Class.new(BaseObject) do
|
901
|
+
def to_s
|
902
|
+
'simple object'
|
903
|
+
end
|
904
|
+
end
|
905
|
+
|
906
|
+
Isbn = Struct.new(:code) do
|
907
|
+
def to_str
|
908
|
+
code.to_s
|
909
|
+
end
|
910
|
+
end
|
911
|
+
end
|
912
|
+
|
913
|
+
after do
|
914
|
+
Object.send(:remove_const, :Book)
|
915
|
+
Object.send(:remove_const, :BaseObject)
|
916
|
+
Object.send(:remove_const, :SimpleObject)
|
917
|
+
Object.send(:remove_const, :Isbn)
|
918
|
+
end
|
919
|
+
|
920
|
+
describe 'successful operations' do
|
921
|
+
before do
|
922
|
+
@result = Lotus::Utils::Kernel.String(input)
|
923
|
+
end
|
924
|
+
|
925
|
+
describe 'when nil is given' do
|
926
|
+
let(:input) { nil }
|
927
|
+
|
928
|
+
it 'returns nil' do
|
929
|
+
@result.must_be_nil
|
930
|
+
end
|
931
|
+
end
|
932
|
+
|
933
|
+
describe 'when true is given' do
|
934
|
+
let(:input) { true }
|
935
|
+
|
936
|
+
it 'returns nil' do
|
937
|
+
@result.must_equal 'true'
|
938
|
+
end
|
939
|
+
end
|
940
|
+
|
941
|
+
describe 'when false is given' do
|
942
|
+
let(:input) { false }
|
943
|
+
|
944
|
+
it 'returns nil' do
|
945
|
+
@result.must_equal 'false'
|
946
|
+
end
|
947
|
+
end
|
948
|
+
|
949
|
+
describe 'when an empty string is given' do
|
950
|
+
let(:input) { '' }
|
951
|
+
|
952
|
+
it 'returns it' do
|
953
|
+
@result.must_equal ''
|
954
|
+
end
|
955
|
+
end
|
956
|
+
|
957
|
+
describe 'when a string is given' do
|
958
|
+
let(:input) { 'ciao' }
|
959
|
+
|
960
|
+
it 'returns it' do
|
961
|
+
@result.must_equal 'ciao'
|
962
|
+
end
|
963
|
+
end
|
964
|
+
|
965
|
+
describe 'when an integer is given' do
|
966
|
+
let(:input) { 23 }
|
967
|
+
|
968
|
+
it 'returns the string representation' do
|
969
|
+
@result.must_equal '23'
|
970
|
+
end
|
971
|
+
end
|
972
|
+
|
973
|
+
describe 'when a float is given' do
|
974
|
+
let(:input) { 3.14 }
|
975
|
+
|
976
|
+
it 'returns the string representation' do
|
977
|
+
@result.must_equal '3.14'
|
978
|
+
end
|
979
|
+
end
|
980
|
+
|
981
|
+
describe 'when an octal is given' do
|
982
|
+
let(:input) { 013 }
|
983
|
+
|
984
|
+
it 'returns the string representation' do
|
985
|
+
@result.must_equal '11'
|
986
|
+
end
|
987
|
+
end
|
988
|
+
|
989
|
+
describe 'when a hex is given' do
|
990
|
+
let(:input) { 0xc0ff33 }
|
991
|
+
|
992
|
+
it 'returns the string representation' do
|
993
|
+
@result.must_equal '12648243'
|
994
|
+
end
|
995
|
+
end
|
996
|
+
|
997
|
+
describe 'when a big decimal is given' do
|
998
|
+
let(:input) { BigDecimal.new(7944.2343, 10) }
|
999
|
+
|
1000
|
+
it 'returns the string representation' do
|
1001
|
+
@result.must_equal '0.79442343E4'
|
1002
|
+
end
|
1003
|
+
end
|
1004
|
+
|
1005
|
+
describe 'when a big decimal infinity is given' do
|
1006
|
+
let(:input) { BigDecimal.new('Infinity') }
|
1007
|
+
|
1008
|
+
it 'returns the string representation' do
|
1009
|
+
@result.must_equal 'Infinity'
|
1010
|
+
end
|
1011
|
+
end
|
1012
|
+
|
1013
|
+
describe 'when a big decimal NaN is given' do
|
1014
|
+
let(:input) { BigDecimal.new('NaN') }
|
1015
|
+
|
1016
|
+
it 'returns the string representation' do
|
1017
|
+
@result.must_equal 'NaN'
|
1018
|
+
end
|
1019
|
+
end
|
1020
|
+
|
1021
|
+
describe 'when a complex is given' do
|
1022
|
+
let(:input) { Complex(11,2) }
|
1023
|
+
|
1024
|
+
it 'returns the string representation' do
|
1025
|
+
@result.must_equal '11+2i'
|
1026
|
+
end
|
1027
|
+
end
|
1028
|
+
|
1029
|
+
describe 'when a rational is given' do
|
1030
|
+
let(:input) { Rational(-22) }
|
1031
|
+
|
1032
|
+
it 'returns the string representation' do
|
1033
|
+
@result.must_equal '-22/1'
|
1034
|
+
end
|
1035
|
+
end
|
1036
|
+
|
1037
|
+
describe 'when an empty array is given' do
|
1038
|
+
let(:input) { [] }
|
1039
|
+
|
1040
|
+
it 'returns the string representation' do
|
1041
|
+
@result.must_equal '[]'
|
1042
|
+
end
|
1043
|
+
end
|
1044
|
+
|
1045
|
+
describe 'when an array of integers is given' do
|
1046
|
+
let(:input) { [1,2,3] }
|
1047
|
+
|
1048
|
+
it 'returns the string representation' do
|
1049
|
+
@result.must_equal '[1, 2, 3]'
|
1050
|
+
end
|
1051
|
+
end
|
1052
|
+
|
1053
|
+
describe 'when an array of strings is given' do
|
1054
|
+
let(:input) { %w[a b c] }
|
1055
|
+
|
1056
|
+
it 'returns the string representation' do
|
1057
|
+
@result.must_equal '["a", "b", "c"]'
|
1058
|
+
end
|
1059
|
+
end
|
1060
|
+
|
1061
|
+
describe 'when an array of objects is given' do
|
1062
|
+
let(:input) { [Object.new] }
|
1063
|
+
|
1064
|
+
it 'returns the string representation' do
|
1065
|
+
@result.must_match '[#<Object:'
|
1066
|
+
end
|
1067
|
+
end
|
1068
|
+
|
1069
|
+
describe 'when an empty hash is given' do
|
1070
|
+
let(:input) { {} }
|
1071
|
+
|
1072
|
+
it 'returns the string representation' do
|
1073
|
+
@result.must_equal '{}'
|
1074
|
+
end
|
1075
|
+
end
|
1076
|
+
|
1077
|
+
describe 'when an hash is given' do
|
1078
|
+
let(:input) { {a: 1, 'b' => 2} }
|
1079
|
+
|
1080
|
+
it 'returns the string representation' do
|
1081
|
+
@result.must_equal '{:a=>1, "b"=>2}'
|
1082
|
+
end
|
1083
|
+
end
|
1084
|
+
|
1085
|
+
describe 'when a symbol is given' do
|
1086
|
+
let(:input) { :lotus }
|
1087
|
+
|
1088
|
+
it 'returns the string representation' do
|
1089
|
+
@result.must_equal 'lotus'
|
1090
|
+
end
|
1091
|
+
end
|
1092
|
+
|
1093
|
+
describe 'when an struct is given' do
|
1094
|
+
let(:input) { Book.new('DDD') }
|
1095
|
+
|
1096
|
+
it 'returns the string representation' do
|
1097
|
+
@result.must_equal '#<struct Book title="DDD">'
|
1098
|
+
end
|
1099
|
+
end
|
1100
|
+
|
1101
|
+
describe 'when an open struct is given' do
|
1102
|
+
let(:input) { OpenStruct.new(title: 'DDD') }
|
1103
|
+
|
1104
|
+
it 'returns the string representation' do
|
1105
|
+
@result.must_equal '#<OpenStruct title="DDD">'
|
1106
|
+
end
|
1107
|
+
end
|
1108
|
+
|
1109
|
+
describe 'when a date is given' do
|
1110
|
+
let(:input) { Date.parse('2014-04-11') }
|
1111
|
+
|
1112
|
+
it 'returns the string representation' do
|
1113
|
+
@result.must_equal '2014-04-11'
|
1114
|
+
end
|
1115
|
+
end
|
1116
|
+
|
1117
|
+
describe 'when a datetime is given' do
|
1118
|
+
let(:input) { DateTime.parse('2014-04-11 09:45') }
|
1119
|
+
|
1120
|
+
it 'returns the string representation' do
|
1121
|
+
@result.must_equal '2014-04-11T09:45:00+00:00'
|
1122
|
+
end
|
1123
|
+
end
|
1124
|
+
|
1125
|
+
describe 'when a time is given' do
|
1126
|
+
let(:input) { Time.at(0).utc }
|
1127
|
+
|
1128
|
+
it 'returns the string representation' do
|
1129
|
+
@result.must_equal '1970-01-01 00:00:00 UTC'
|
1130
|
+
end
|
1131
|
+
end
|
1132
|
+
|
1133
|
+
describe 'when a class is given' do
|
1134
|
+
let(:input) { Fixnum }
|
1135
|
+
|
1136
|
+
it 'returns the string representation' do
|
1137
|
+
@result.must_equal 'Fixnum'
|
1138
|
+
end
|
1139
|
+
end
|
1140
|
+
|
1141
|
+
describe 'when a module is given' do
|
1142
|
+
let(:input) { Lotus }
|
1143
|
+
|
1144
|
+
it 'returns the string representation' do
|
1145
|
+
@result.must_equal 'Lotus'
|
1146
|
+
end
|
1147
|
+
end
|
1148
|
+
|
1149
|
+
describe 'when an object implements #to_s' do
|
1150
|
+
let(:input) { SimpleObject.new }
|
1151
|
+
|
1152
|
+
it 'returns the string representation' do
|
1153
|
+
@result.must_equal 'simple object'
|
1154
|
+
end
|
1155
|
+
end
|
1156
|
+
|
1157
|
+
describe 'when an object implements #to_str' do
|
1158
|
+
let(:input) { Isbn.new(123) }
|
1159
|
+
|
1160
|
+
it 'returns the string representation' do
|
1161
|
+
@result.must_equal '123'
|
1162
|
+
end
|
1163
|
+
end
|
1164
|
+
end
|
1165
|
+
|
1166
|
+
describe 'failure operations' do
|
1167
|
+
describe "when a an object that doesn't implement #nil?" do
|
1168
|
+
let(:input) { BasicObject.new }
|
1169
|
+
|
1170
|
+
it 'raises error' do
|
1171
|
+
-> { Lotus::Utils::Kernel.String(input) }.must_raise(NoMethodError)
|
1172
|
+
end
|
1173
|
+
end
|
1174
|
+
|
1175
|
+
describe "when a an object that doesn't implement a string interface" do
|
1176
|
+
let(:input) { BaseObject.new }
|
1177
|
+
|
1178
|
+
it 'raises error' do
|
1179
|
+
-> { Lotus::Utils::Kernel.String(input) }.must_raise(TypeError)
|
1180
|
+
end
|
1181
|
+
end
|
1182
|
+
end
|
1183
|
+
end
|
1184
|
+
|
1185
|
+
describe '.Boolean' do
|
1186
|
+
before do
|
1187
|
+
Answer = Struct.new(:answer) do
|
1188
|
+
def to_bool
|
1189
|
+
case answer
|
1190
|
+
when 'yes' then true
|
1191
|
+
else false
|
1192
|
+
end
|
1193
|
+
end
|
1194
|
+
end
|
1195
|
+
end
|
1196
|
+
|
1197
|
+
after do
|
1198
|
+
Object.send(:remove_const, :Answer)
|
1199
|
+
end
|
1200
|
+
|
1201
|
+
describe 'successful operations' do
|
1202
|
+
before do
|
1203
|
+
@result = Lotus::Utils::Kernel.Boolean(input)
|
1204
|
+
end
|
1205
|
+
|
1206
|
+
describe 'when nil is given' do
|
1207
|
+
let(:input) { nil }
|
1208
|
+
|
1209
|
+
it 'returns nil' do
|
1210
|
+
@result.must_be_nil
|
1211
|
+
end
|
1212
|
+
end
|
1213
|
+
|
1214
|
+
describe 'when true is given' do
|
1215
|
+
let(:input) { true }
|
1216
|
+
|
1217
|
+
it 'returns true' do
|
1218
|
+
@result.must_equal true
|
1219
|
+
end
|
1220
|
+
end
|
1221
|
+
|
1222
|
+
describe 'when false is given' do
|
1223
|
+
let(:input) { false }
|
1224
|
+
|
1225
|
+
it 'returns false' do
|
1226
|
+
@result.must_equal false
|
1227
|
+
end
|
1228
|
+
end
|
1229
|
+
|
1230
|
+
describe 'when 0 is given' do
|
1231
|
+
let(:input) { 0 }
|
1232
|
+
|
1233
|
+
it 'returns false' do
|
1234
|
+
@result.must_equal false
|
1235
|
+
end
|
1236
|
+
end
|
1237
|
+
|
1238
|
+
describe 'when 1 is given' do
|
1239
|
+
let(:input) { 1 }
|
1240
|
+
|
1241
|
+
it 'returns true' do
|
1242
|
+
@result.must_equal true
|
1243
|
+
end
|
1244
|
+
end
|
1245
|
+
|
1246
|
+
describe 'when 2 is given' do
|
1247
|
+
let(:input) { 2 }
|
1248
|
+
|
1249
|
+
it 'returns false' do
|
1250
|
+
@result.must_equal false
|
1251
|
+
end
|
1252
|
+
end
|
1253
|
+
|
1254
|
+
describe 'when -1 is given' do
|
1255
|
+
let(:input) { -1 }
|
1256
|
+
|
1257
|
+
it 'returns false' do
|
1258
|
+
@result.must_equal false
|
1259
|
+
end
|
1260
|
+
end
|
1261
|
+
|
1262
|
+
describe 'when "0" is given' do
|
1263
|
+
let(:input) { "0" }
|
1264
|
+
|
1265
|
+
it 'returns false' do
|
1266
|
+
@result.must_equal false
|
1267
|
+
end
|
1268
|
+
end
|
1269
|
+
|
1270
|
+
describe 'when "1" is given' do
|
1271
|
+
let(:input) { "1" }
|
1272
|
+
|
1273
|
+
it 'returns true' do
|
1274
|
+
@result.must_equal true
|
1275
|
+
end
|
1276
|
+
end
|
1277
|
+
|
1278
|
+
describe 'when an object is given' do
|
1279
|
+
let(:input) { Object.new }
|
1280
|
+
|
1281
|
+
it 'returns true' do
|
1282
|
+
@result.must_equal true
|
1283
|
+
end
|
1284
|
+
end
|
1285
|
+
|
1286
|
+
describe 'when the given object responds to #to_bool' do
|
1287
|
+
let(:input) { Answer.new('no') }
|
1288
|
+
|
1289
|
+
it 'returns the result of the serialization' do
|
1290
|
+
@result.must_equal false
|
1291
|
+
end
|
1292
|
+
end
|
1293
|
+
end
|
1294
|
+
|
1295
|
+
describe 'failure operations' do
|
1296
|
+
describe "when a an object that doesn't implement #respond_to?" do
|
1297
|
+
let(:input) { BasicObject.new }
|
1298
|
+
|
1299
|
+
it 'raises error' do
|
1300
|
+
-> { Lotus::Utils::Kernel.Boolean(input) }.must_raise(NoMethodError)
|
1301
|
+
end
|
1302
|
+
end
|
1303
|
+
end
|
1304
|
+
end
|
1305
|
+
|
1306
|
+
describe '.Date' do
|
1307
|
+
before do
|
1308
|
+
class Christmas
|
1309
|
+
def to_date
|
1310
|
+
Date.parse('Dec, 25')
|
1311
|
+
end
|
1312
|
+
end
|
1313
|
+
|
1314
|
+
BaseObject = Class.new(BasicObject) do
|
1315
|
+
def nil?
|
1316
|
+
false
|
1317
|
+
end
|
1318
|
+
end
|
1319
|
+
end
|
1320
|
+
|
1321
|
+
after do
|
1322
|
+
Object.send(:remove_const, :Christmas)
|
1323
|
+
Object.send(:remove_const, :BaseObject)
|
1324
|
+
end
|
1325
|
+
|
1326
|
+
describe 'successful operations' do
|
1327
|
+
before do
|
1328
|
+
@result = Lotus::Utils::Kernel.Date(input)
|
1329
|
+
end
|
1330
|
+
|
1331
|
+
describe 'when nil is given' do
|
1332
|
+
let(:input) { nil }
|
1333
|
+
|
1334
|
+
it 'returns nil' do
|
1335
|
+
@result.must_be_nil
|
1336
|
+
end
|
1337
|
+
end
|
1338
|
+
|
1339
|
+
describe 'when a date is given' do
|
1340
|
+
let(:input) { Date.today }
|
1341
|
+
|
1342
|
+
it 'returns the input' do
|
1343
|
+
@result.must_equal input
|
1344
|
+
end
|
1345
|
+
end
|
1346
|
+
|
1347
|
+
describe 'when a string that represents a date is given' do
|
1348
|
+
let(:input) { '2014-04-17' }
|
1349
|
+
|
1350
|
+
it 'returns a date' do
|
1351
|
+
@result.must_equal Date.parse(input)
|
1352
|
+
end
|
1353
|
+
end
|
1354
|
+
|
1355
|
+
describe 'when a string that represents a timestamp is given' do
|
1356
|
+
let(:input) { '2014-04-17 18:50:01' }
|
1357
|
+
|
1358
|
+
it 'returns a date' do
|
1359
|
+
@result.must_equal Date.parse('2014-04-17')
|
1360
|
+
end
|
1361
|
+
end
|
1362
|
+
|
1363
|
+
describe 'when a time is given' do
|
1364
|
+
let(:input) { Time.now }
|
1365
|
+
|
1366
|
+
it 'returns a date' do
|
1367
|
+
@result.must_equal Date.today
|
1368
|
+
end
|
1369
|
+
end
|
1370
|
+
|
1371
|
+
describe 'when a datetime is given' do
|
1372
|
+
let(:input) { DateTime.now }
|
1373
|
+
|
1374
|
+
it 'returns a date' do
|
1375
|
+
@result.must_equal Date.today
|
1376
|
+
end
|
1377
|
+
end
|
1378
|
+
|
1379
|
+
describe 'when an object that implements #to_date is given' do
|
1380
|
+
let(:input) { Christmas.new }
|
1381
|
+
|
1382
|
+
it 'returns a date' do
|
1383
|
+
@result.must_equal Date.parse('Dec, 25')
|
1384
|
+
end
|
1385
|
+
end
|
1386
|
+
end
|
1387
|
+
|
1388
|
+
describe 'failure operations' do
|
1389
|
+
describe 'when true is given' do
|
1390
|
+
let(:input) { true }
|
1391
|
+
|
1392
|
+
it 'raises error' do
|
1393
|
+
-> { Lotus::Utils::Kernel.Date(input) }.must_raise ArgumentError
|
1394
|
+
end
|
1395
|
+
end
|
1396
|
+
|
1397
|
+
describe 'when false is given' do
|
1398
|
+
let(:input) { false }
|
1399
|
+
|
1400
|
+
it 'raises error' do
|
1401
|
+
-> { Lotus::Utils::Kernel.Date(input) }.must_raise ArgumentError
|
1402
|
+
end
|
1403
|
+
end
|
1404
|
+
|
1405
|
+
describe 'when a fixnum is given' do
|
1406
|
+
let(:input) { 2 }
|
1407
|
+
|
1408
|
+
it 'raises error' do
|
1409
|
+
-> { Lotus::Utils::Kernel.Date(input) }.must_raise ArgumentError
|
1410
|
+
end
|
1411
|
+
end
|
1412
|
+
|
1413
|
+
describe 'when a float is given' do
|
1414
|
+
let(:input) { 2332.903007 }
|
1415
|
+
|
1416
|
+
it 'raises error' do
|
1417
|
+
-> { Lotus::Utils::Kernel.Date(input) }.must_raise ArgumentError
|
1418
|
+
end
|
1419
|
+
end
|
1420
|
+
|
1421
|
+
describe "when a string that doesn't represent a date is given" do
|
1422
|
+
let(:input) { 'lego' }
|
1423
|
+
|
1424
|
+
it 'raises error' do
|
1425
|
+
-> { Lotus::Utils::Kernel.Date(input) }.must_raise ArgumentError
|
1426
|
+
end
|
1427
|
+
end
|
1428
|
+
|
1429
|
+
describe "when a string that represent a hour is given" do
|
1430
|
+
let(:input) { '18:55' }
|
1431
|
+
|
1432
|
+
it 'raises error' do
|
1433
|
+
-> { Lotus::Utils::Kernel.Date(input) }.must_raise ArgumentError
|
1434
|
+
end
|
1435
|
+
end
|
1436
|
+
|
1437
|
+
describe "when an object that doesn't implement #respond_to?" do
|
1438
|
+
let(:input) { BasicObject.new }
|
1439
|
+
|
1440
|
+
it 'raises error' do
|
1441
|
+
-> { Lotus::Utils::Kernel.Date(input) }.must_raise NoMethodError
|
1442
|
+
end
|
1443
|
+
end
|
1444
|
+
|
1445
|
+
describe "when an object that doesn't implement #nil?" do
|
1446
|
+
let(:input) { BaseObject.new }
|
1447
|
+
|
1448
|
+
it 'raises error' do
|
1449
|
+
-> { Lotus::Utils::Kernel.Date(input) }.must_raise NoMethodError
|
1450
|
+
end
|
1451
|
+
end
|
1452
|
+
end
|
1453
|
+
end
|
1454
|
+
|
1455
|
+
describe '.DateTime' do
|
1456
|
+
before do
|
1457
|
+
class NewYearEve
|
1458
|
+
def to_datetime
|
1459
|
+
DateTime.parse('Jan, 1')
|
1460
|
+
end
|
1461
|
+
end
|
1462
|
+
|
1463
|
+
BaseObject = Class.new(BasicObject) do
|
1464
|
+
def nil?
|
1465
|
+
false
|
1466
|
+
end
|
1467
|
+
end
|
1468
|
+
end
|
1469
|
+
|
1470
|
+
after do
|
1471
|
+
Object.send(:remove_const, :NewYearEve)
|
1472
|
+
Object.send(:remove_const, :BaseObject)
|
1473
|
+
end
|
1474
|
+
|
1475
|
+
describe 'successful operations' do
|
1476
|
+
before do
|
1477
|
+
@result = Lotus::Utils::Kernel.DateTime(input)
|
1478
|
+
end
|
1479
|
+
|
1480
|
+
describe 'when nil is given' do
|
1481
|
+
let(:input) { nil }
|
1482
|
+
|
1483
|
+
it 'returns nil' do
|
1484
|
+
@result.must_be_nil
|
1485
|
+
end
|
1486
|
+
end
|
1487
|
+
|
1488
|
+
describe 'when a datetime is given' do
|
1489
|
+
let(:input) { DateTime.now }
|
1490
|
+
|
1491
|
+
it 'returns the input' do
|
1492
|
+
@result.must_equal input
|
1493
|
+
end
|
1494
|
+
end
|
1495
|
+
|
1496
|
+
describe 'when a date is given' do
|
1497
|
+
let(:input) { Date.today }
|
1498
|
+
|
1499
|
+
it 'returns a datetime' do
|
1500
|
+
@result.must_equal DateTime.parse(Date.today.to_s)
|
1501
|
+
end
|
1502
|
+
end
|
1503
|
+
|
1504
|
+
describe 'when a string that represents a date is given' do
|
1505
|
+
let(:input) { '2014-04-17' }
|
1506
|
+
|
1507
|
+
it 'returns a datetime' do
|
1508
|
+
@result.must_equal DateTime.parse(input)
|
1509
|
+
end
|
1510
|
+
end
|
1511
|
+
|
1512
|
+
describe 'when a string that represents a timestamp is given' do
|
1513
|
+
let(:input) { '2014-04-17 22:51:48' }
|
1514
|
+
|
1515
|
+
it 'returns a datetime' do
|
1516
|
+
@result.must_equal DateTime.parse(input)
|
1517
|
+
end
|
1518
|
+
end
|
1519
|
+
|
1520
|
+
describe 'when a time is given' do
|
1521
|
+
let(:input) { Time.now }
|
1522
|
+
|
1523
|
+
it 'returns a datetime' do
|
1524
|
+
@result.must_equal input.to_datetime
|
1525
|
+
end
|
1526
|
+
end
|
1527
|
+
|
1528
|
+
describe 'when an object that implements #to_datetime is given' do
|
1529
|
+
let(:input) { NewYearEve.new }
|
1530
|
+
|
1531
|
+
it 'returns a datetime' do
|
1532
|
+
@result.must_equal DateTime.parse('Jan 1')
|
1533
|
+
end
|
1534
|
+
end
|
1535
|
+
|
1536
|
+
describe "when a string that represent a hour is given" do
|
1537
|
+
let(:input) { '23:12' }
|
1538
|
+
|
1539
|
+
it 'returns a datetime' do
|
1540
|
+
@result.must_equal DateTime.parse(input)
|
1541
|
+
end
|
1542
|
+
end
|
1543
|
+
|
1544
|
+
describe 'when a float is given' do
|
1545
|
+
let(:input) { 2332.903007 }
|
1546
|
+
|
1547
|
+
it 'raises error' do
|
1548
|
+
@result.must_equal Time.at(input).to_datetime
|
1549
|
+
end
|
1550
|
+
end
|
1551
|
+
|
1552
|
+
describe 'when a fixnum is given' do
|
1553
|
+
let(:input) { 34322 }
|
1554
|
+
|
1555
|
+
it 'raises error' do
|
1556
|
+
@result.must_equal Time.at(input).to_datetime
|
1557
|
+
end
|
1558
|
+
end
|
1559
|
+
end
|
1560
|
+
|
1561
|
+
describe 'failure operations' do
|
1562
|
+
describe 'when true is given' do
|
1563
|
+
let(:input) { true }
|
1564
|
+
|
1565
|
+
it 'raises error' do
|
1566
|
+
-> { Lotus::Utils::Kernel.DateTime(input) }.must_raise ArgumentError
|
1567
|
+
end
|
1568
|
+
end
|
1569
|
+
|
1570
|
+
describe 'when false is given' do
|
1571
|
+
let(:input) { false }
|
1572
|
+
|
1573
|
+
it 'raises error' do
|
1574
|
+
-> { Lotus::Utils::Kernel.DateTime(input) }.must_raise ArgumentError
|
1575
|
+
end
|
1576
|
+
end
|
1577
|
+
|
1578
|
+
describe "when a string that doesn't represent a date is given" do
|
1579
|
+
let(:input) { 'crab' }
|
1580
|
+
|
1581
|
+
it 'raises error' do
|
1582
|
+
-> { Lotus::Utils::Kernel.DateTime(input) }.must_raise ArgumentError
|
1583
|
+
end
|
1584
|
+
end
|
1585
|
+
|
1586
|
+
describe "when an object that doesn't implement #respond_to?" do
|
1587
|
+
let(:input) { BasicObject.new }
|
1588
|
+
|
1589
|
+
it 'raises error' do
|
1590
|
+
-> { Lotus::Utils::Kernel.DateTime(input) }.must_raise NoMethodError
|
1591
|
+
end
|
1592
|
+
end
|
1593
|
+
|
1594
|
+
describe "when an object that doesn't implement #nil?" do
|
1595
|
+
let(:input) { BaseObject.new }
|
1596
|
+
|
1597
|
+
it 'raises error' do
|
1598
|
+
-> { Lotus::Utils::Kernel.DateTime(input) }.must_raise NoMethodError
|
1599
|
+
end
|
1600
|
+
end
|
1601
|
+
end
|
1602
|
+
end
|
1603
|
+
|
1604
|
+
describe '.Time' do
|
1605
|
+
before do
|
1606
|
+
class Epoch
|
1607
|
+
def to_time
|
1608
|
+
Time.at(0)
|
1609
|
+
end
|
1610
|
+
end
|
1611
|
+
|
1612
|
+
BaseObject = Class.new(BasicObject) do
|
1613
|
+
def nil?
|
1614
|
+
false
|
1615
|
+
end
|
1616
|
+
end
|
1617
|
+
end
|
1618
|
+
|
1619
|
+
after do
|
1620
|
+
Object.send(:remove_const, :Epoch)
|
1621
|
+
Object.send(:remove_const, :BaseObject)
|
1622
|
+
end
|
1623
|
+
|
1624
|
+
describe 'successful operations' do
|
1625
|
+
before do
|
1626
|
+
@result = Lotus::Utils::Kernel.Time(input)
|
1627
|
+
end
|
1628
|
+
|
1629
|
+
describe 'when nil is given' do
|
1630
|
+
let(:input) { nil }
|
1631
|
+
|
1632
|
+
it 'returns nil' do
|
1633
|
+
@result.must_be_nil
|
1634
|
+
end
|
1635
|
+
end
|
1636
|
+
|
1637
|
+
describe 'when a time is given' do
|
1638
|
+
let(:input) { Time.now }
|
1639
|
+
|
1640
|
+
it 'returns the input' do
|
1641
|
+
@result.must_equal input
|
1642
|
+
end
|
1643
|
+
end
|
1644
|
+
|
1645
|
+
describe 'when a datetime is given' do
|
1646
|
+
let(:input) { DateTime.now }
|
1647
|
+
|
1648
|
+
it 'returns time' do
|
1649
|
+
@result.must_equal input.to_time
|
1650
|
+
end
|
1651
|
+
end
|
1652
|
+
|
1653
|
+
describe 'when a date is given' do
|
1654
|
+
let(:input) { Date.today }
|
1655
|
+
|
1656
|
+
it 'returns time' do
|
1657
|
+
@result.must_equal input.to_time
|
1658
|
+
end
|
1659
|
+
end
|
1660
|
+
|
1661
|
+
describe 'when a string that represents a date is given' do
|
1662
|
+
let(:input) { '2014-04-18' }
|
1663
|
+
|
1664
|
+
it 'returns time' do
|
1665
|
+
@result.must_equal Time.parse(input)
|
1666
|
+
end
|
1667
|
+
end
|
1668
|
+
|
1669
|
+
describe 'when a string that represents a timestamp is given' do
|
1670
|
+
let(:input) { '2014-04-18 15:45:12' }
|
1671
|
+
|
1672
|
+
it 'returns time' do
|
1673
|
+
@result.must_equal Time.parse(input)
|
1674
|
+
end
|
1675
|
+
end
|
1676
|
+
|
1677
|
+
describe 'when an object that implements #to_time is given' do
|
1678
|
+
let(:input) { Epoch.new }
|
1679
|
+
|
1680
|
+
it 'returns time' do
|
1681
|
+
@result.must_equal Time.at(0)
|
1682
|
+
end
|
1683
|
+
end
|
1684
|
+
|
1685
|
+
describe 'when a string that represent a hour is given' do
|
1686
|
+
let(:input) { '15:47' }
|
1687
|
+
|
1688
|
+
it 'returns a time' do
|
1689
|
+
@result.must_equal Time.parse(input)
|
1690
|
+
end
|
1691
|
+
end
|
1692
|
+
|
1693
|
+
describe 'when a fixnum is given' do
|
1694
|
+
let(:input) { 38922 }
|
1695
|
+
|
1696
|
+
it 'returns a time' do
|
1697
|
+
@result.must_equal Time.at(input)
|
1698
|
+
end
|
1699
|
+
end
|
1700
|
+
|
1701
|
+
describe 'when a float is given' do
|
1702
|
+
let(:input) { 1332.9423843 }
|
1703
|
+
|
1704
|
+
it 'returns a time' do
|
1705
|
+
@result.must_equal Time.at(input)
|
1706
|
+
end
|
1707
|
+
end
|
1708
|
+
end
|
1709
|
+
|
1710
|
+
describe 'failure operations' do
|
1711
|
+
describe 'when true is given' do
|
1712
|
+
let(:input) { true }
|
1713
|
+
|
1714
|
+
it 'raises error' do
|
1715
|
+
-> { Lotus::Utils::Kernel.Time(input) }.must_raise ArgumentError
|
1716
|
+
end
|
1717
|
+
end
|
1718
|
+
|
1719
|
+
describe 'when false is given' do
|
1720
|
+
let(:input) { false }
|
1721
|
+
|
1722
|
+
it 'raises error' do
|
1723
|
+
-> { Lotus::Utils::Kernel.Time(input) }.must_raise ArgumentError
|
1724
|
+
end
|
1725
|
+
end
|
1726
|
+
|
1727
|
+
describe "when a string that doesn't represent a date is given" do
|
1728
|
+
let(:input) { 'boat' }
|
1729
|
+
|
1730
|
+
it 'raises error' do
|
1731
|
+
-> { Lotus::Utils::Kernel.Time(input) }.must_raise ArgumentError
|
1732
|
+
end
|
1733
|
+
end
|
1734
|
+
|
1735
|
+
describe "when an object that doesn't implement #respond_to?" do
|
1736
|
+
let(:input) { BasicObject.new }
|
1737
|
+
|
1738
|
+
it 'raises error' do
|
1739
|
+
-> { Lotus::Utils::Kernel.Time(input) }.must_raise NoMethodError
|
1740
|
+
end
|
1741
|
+
end
|
1742
|
+
|
1743
|
+
describe "when an object that doesn't implement #nil?" do
|
1744
|
+
let(:input) { BaseObject.new }
|
1745
|
+
|
1746
|
+
it 'raises error' do
|
1747
|
+
-> { Lotus::Utils::Kernel.Time(input) }.must_raise NoMethodError
|
1748
|
+
end
|
1749
|
+
end
|
1750
|
+
end
|
1751
|
+
end
|
1752
|
+
end
|