Welcome to mirror list, hosted at ThFree Co, Russian Federation.

object_spec.lua « spec - github.com/Yonaba/Moses.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18588babdc729537a510ce4213441f3eee523ea4 (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
require 'luacov'
local M = require 'moses'

describe('Object functions specs', function()

  describe('keys', function()
  
    it('collects a given object attributes',function()
      assert.is_true(M.isEqual(M.keys({1,2,3}),{1,2,3}))
      assert.is_true(M.isEqual(M.keys({4,5,6}),{1,2,3}))
      assert.is_true(M.same(M.keys({x = 1, y = 2, 3}),{'x','y',1}))
    end)
    
  end)
  
  describe('values', function()
  
    it('collects an given object values',function()
      assert.is_true(M.isEqual(M.values({1,2,3}),{1,2,3}))
      assert.is_true(M.isEqual(M.values({4,5,6}),{4,5,6}))
      assert.is_true(M.same(M.values({x = 1, y = 2, 3}),{1,2,3}))
    end)
   
  end)
  
  describe('path', function()
  
    it('return the value at a given path in object',function()
      local entity = {
        pos = {x = 1},
        engine = {
          left = {status = 'active'},
          right = {damage = 10}
        },
        boost = false
      }
      assert.equal(M.path(entity, 'pos','x'), 1)
      assert.equal(M.path(entity, 'engine','left','status'), 'active')
      assert.equal(M.path(entity, 'engine','right','damage'), 10)
      assert.equal(M.path(entity, 'boost'), false)
      assert.is_nil(M.path(entity, 'x'))
    end)
   
  end)   
	
  describe('kvpairs', function()
  
    it('converts key-values pairs in object to array-list of k,v pairs',function()
			local obj = M.kvpairs({x = 1, y = 2, z = 3})
			table.sort(obj, function(a,b) return a[1] < b[1] end)
			assert.is_true(M.isEqual(obj[1],{'x',1}))
			assert.is_true(M.isEqual(obj[2],{'y',2}))
			assert.is_true(M.isEqual(obj[3],{'z',3}))
    end)
   
  end)  	
	
  describe('toObj', function()
  
    it('converts an array-list of {k,v} pairs to an object',function()
			local obj = M.toObj({{'x',1},{'y',2},{'z',3}})
			assert.equal(obj.x,1)
			assert.equal(obj.y,2)
			assert.equal(obj.z,3)
    end)
   
  end)  	
  
  describe('property', function()
  
    it('Returns a function that will return the key property of any passed-in object.',function()
			assert.equal(M.property('sin')(math), math.sin)
			assert.equal(M.property('find')(string), string.find)
			assert.equal(M.property('insert')(table), table.insert)
			assert.equal(M.property('yield')(coroutine), coroutine.yield)
    end)
   
  end)  	
  
  describe('propertyOf', function()
  
    it('Returns a function which will return the value of an object property.',function()
			assert.equal(M.propertyOf(math)('cos'), math.cos)
			assert.equal(M.propertyOf(string)('char'), string.char)
			assert.equal(M.propertyOf(table)('remove'), table.remove)	
			assert.equal(M.propertyOf(M)('propertyOf'), M.propertyOf)	
    end)
   
  end)  	
  
	
  describe('toBoolean', function()
  
    it('converts a value to a boolean',function()
      assert.is_true(type(M.toBoolean(true)) == 'boolean')
      assert.is_true(type(M.toBoolean(1)) == 'boolean')
      assert.is_true(type(M.toBoolean(false)) == 'boolean')
      assert.is_true(type(M.toBoolean(nil)) == 'boolean')
      assert.is_true(type(M.toBoolean({})) == 'boolean')
      assert.is_true(type(M.toBoolean(1/0)) == 'boolean')
      
      assert.is_true(M.toBoolean(true))
      assert.is_true(M.toBoolean(1))
      assert.is_false(M.toBoolean(false))
      assert.is_false(M.toBoolean(nil))
      assert.is_true(M.toBoolean({}))
      assert.is_true(M.toBoolean(1/0))      
    end)
  
  end)  
  
  describe('extend', function()
  
    it('extends a destination objects with key-values a source object',function()
      assert.is_true(M.isEqual(M.extend({},{a = 'b'}),{a = 'b'}))
    end)
    
    it('source properties overrides destination properties',function()
      assert.is_true(M.isEqual(M.extend({a = 'a'},{a = 'b'}),{a = 'b'}))
    end)   

    it('leaves source object untouched',function()
      local source = {i = 'i'}
      assert.is_true(M.isEqual(M.extend({a = 'a'},source),{a = 'a',i = 'i'}))
      assert.is_true(M.isEqual(source,{i = 'i'}))
    end)

    it('can extend destination from multiple sources',function()
      local sourceA = {a = 'a'}; local sourceBC = {b = 'b', c = 'c'} 
      assert.is_true(M.isEqual(M.extend({},sourceA, sourceBC),{a = 'a', b = 'b', c = 'c'}))
    end) 

    it('extending from multiple source, latter properties overrides',function()
      local sourceA = {a = 'a'}; local sourceBC = {b = 'b', a = 'c'} 
      assert.is_true(M.isEqual(M.extend({},sourceA, sourceBC),{a = 'c', b = 'b'}))
    end)     
    
    it('will not copy nil values',function()
      local sourceA = {a = nil}; local sourceBC = {b = 'b', c = nil} 
      assert.is_true(M.isEqual(M.extend({},sourceA, sourceBC),{b = 'b'}))
    end)    
  end)
  
  describe('functions', function()

    it('collects function names within an object',function()
      local x = {}
      function x.a() return end; function x.b() return end    
      assert.is_true(M.isEqual(M.functions(x),{'a','b'}))
    end)
    
    it('collects metatable functions if "recurseMt" arg is supplied',function()
      local x = {} ; x.__index = x
      function x.a() return end; function x.b() return end
      local xx = setmetatable({},x)
      function xx.c() return end
      assert.is_true(M.same(M.functions(xx),{'c'}))
      assert.is_true(M.same(M.functions(xx,true),{'a','b','c'}))
    end)

    it('when given no obj as argument, returns all library functions',function()
      local functions = M.functions()
      M.each(functions, function(v)
        assert.is_true(M.isFunction(M[v]))
      end)
    end)
    
  end)  
  
  describe('clone', function()
  
    it('clones the attributes of an object',function()
      local vector = {x = 0, y = 0}
      assert.is_true(M.isEqual(M.clone(vector),vector))
    end)
    
    it('By default, cloning is deep (clones nested tables)',function()
      local particle = {position = {x = 0,y=0},mass = 1}
      local particle_clone = M.clone (particle)
      assert.is_true(M.isEqual(particle_clone,particle))
      particle_clone.position.x = 3
      assert.is_false(M.isEqual(particle_clone,particle))
    end)
 
    it('Unless "shallow" arg is provided',function()
      local particle = {position = {x = 0,y=0},mass = 1}
      local particle_clone = M.clone (particle,true)
      assert.is_true(M.isEqual(particle_clone,particle))
      particle_clone.position.x = 3
      assert.is_true(M.isEqual(particle_clone,particle))
    end) 
    
    it('Non objects are simply returned',function()
      assert.equal(M.clone(1),1)
      assert.equal(M.clone(false),false)
      assert.equal(M.clone(true),true)
      assert.equal(M.clone(nil),nil)
      assert.equal(M.clone('hello'),'hello')
      assert.equal(M.clone(print),print)
    end)     
    
  end)
  
  describe('tap', function()
    
    it('tap-into a method chain', function()
      local t = {}
      local catchMax = function(k) t[#t+1] = M.max(k) end
      local catchMin = function(k) t[#t+1] = M.min(k) end
      
      M.chain({1,2,3})
        :map(function(j) return j*2 end)
        :tap(catchMax)
        :map(function(k) return k^2 end)
        :tap(catchMin)
        :value()
        
      assert.equal(t[1],6)
      assert.equal(t[2],4)
    end)
  
  end)

  describe('has', function()
  
    it('checks if an object has an attribute',function()
      assert.is_true(M.has(M,'has'))
      assert.is_true(M.has(table,'concat'))
      assert.is_true(M.has(string,'format'))
      assert.is_true(M.has(os,'time'))
      assert.is_true(M.has(math,'random'))
    end)
    
  end) 
  
  describe('pick', function()
  
    it('collect specified properties',function()
      local object = {a = 1, b = 2, c = 3}
      assert.is_true(M.isEqual(M.pick(object,'a','c'),{a = 1, c = 3}))
    end)
    
    it('given args can be nested as well',function()
      local object = {a = 1, b = 2, c = 3}
      assert.is_true(M.isEqual(M.pick(object,{{'b','a'}},'c'),{a = 1,b = 2, c = 3}))
    end)

    it('will ignore properties the object do not have',function()
      local object = {a = 1, b = 2, c = 3}
      assert.is_true(M.isEqual(M.pick(object,{{'k'}},'c'),{c = 3}))
    end) 

    it('returns an empty table when given no properties to pick',function()
      local object = {a = 1, b = 2, c = 3}
      assert.is_true(M.isEqual(M.pick(object),{}))
    end)  

    it('should also pick attributes having falsy values',function()
      local object = {a = false, b = false, c = true}
      assert.is_true(M.isEqual(M.pick(object,'a','b'),{a = false,b = false}))
    end)  
    
  end)  
  
  describe('omit', function()
  
    it('collect all properties leaving those given as args',function()
      local object = {a = 1, b = 2, c = 3}
      assert.is_true(M.isEqual(M.omit(object,'a','c'),{b=2}))
    end)
    
    it('given args can be nested as well',function()
      local object = {a = 1, b = 2, c = 3}
      assert.is_true(M.isEqual(M.omit(object,{{'b'}},'c'),{a = 1}))
    end)

    it('will ignore properties the object do not have',function()
      local object = {a = 1, b = 2, c = 3}
      assert.is_true(M.isEqual(M.omit(object,{{'k'}},'c'),{a = 1, b=2}))
    end) 

    it('returns the original object clone when given no properties to omit',function()
      local object = {a = 1, b = 2, c = 3}
      assert.is_true(M.isEqual(M.omit(object),{a = 1, b = 2, c = 3}))
    end)
    
  end)
  
  describe('template', function()
  
    it('applies a template on an object',function()
      assert.is_true(M.isEqual(M.template({},{a = 1, b = 2, c = 3}),{a = 1, b = 2, c = 3}))
    end)
    
    it('does not override existing properies',function()
      assert.is_true(M.isEqual(M.template({a = 10, b = 10},{a = 1, b = 2, c = 3}),{a = 10, b = 10, c = 3}))
    end)

    it('returns the object when given no template arg',function()
      assert.is_true(M.isEqual(M.template({a = 10, b = 10}),{a = 10, b = 10}))
    end)
    
  end) 

  describe('isEqual', function()
  
    it('compares values',function()
      assert.is_true(M.isEqual(1,1))
      assert.is_true(M.isEqual(1.0,1))
      assert.is_false(M.isEqual(1,2))
      assert.is_false(M.isEqual(2,2.0001))
    end)
    
    it('compares objects by reference and components',function()
      local oldprint = print
      assert.is_true(M.isEqual(print,oldprint))
      
      local t = {}
      local v = t
      assert.is_true(M.isEqual(t,v))
      assert.is_true(M.isEqual({},{}))
      
      assert.is_false(M.isEqual('a','b'))
      
      assert.is_false(M.isEqual(true, false))
      assert.is_false(M.isEqual(nil, false))
      assert.is_false(M.isEqual(true, nil))       
    
    end)

    it('compares nested properties',function()
      assert.is_true(M.isEqual({x = 0,{x1 = 0,{x2 =0}}}, {x = 0,{x1 = 0,{x2 =0}}}))
      assert.is_false(M.isEqual({x = 0,{x1 = 0,{x2 =0}}}, {x = 0,{x1 = 0,{x2 =1}}}))
    end)

    it('can compare tables on the basis of their metatable',function()
      local a, b = {x = 1, y = 2}, {x = 2, y = 1}
      setmetatable(a,{__eq = function(a,b) return (a.x and b.x and a.y and b.y)~=nil end})
      assert.is_false(M.isEqual(a, b))
      assert.is_true(M.isEqual(a, b, true))
    end)
  
    
  end)

  describe('result', function()
  
    it('calls an object method, passing it as a first arg the object itself',function()
     assert.equal(M.result('a','len'),1)
     assert.equal(M.result('hello','reverse'),'olleh')
     assert.equal(M.result({'a','b','c'},table.concat),'abc')
    end)
    
    it('handles extra-args to be passed to the so-called method',function()
     assert.equal(M.result('Hello','match','%u'),'H')
     assert.equal(M.result({'a','b','c'},table.concat,' '),'a b c')
    end)    
    
    it('returns the property itself if not callable',function()
     assert.equal(M.result({size = 0},'size'),0)
    end)
     
  end)

  describe('isTable', function()
  
    it('returns "true" if arg is table or array',function()
      assert.is_true(M.isTable({}))
      assert.is_true(M.isTable({1,2}))
      assert.is_true(M.isTable({x = 1, 2}))
      assert.is_true(M.isTable(string))
      assert.is_true(M.isTable(table))
      assert.is_true(M.isTable(math))
      
    end)
    
    it('returns "false" otherwise',function()
      assert.is_false(M.isTable(1))
      assert.is_false(M.isTable(''))
      assert.is_false(M.isTable(function() end))
      assert.is_false(M.isTable(print))
      assert.is_false(M.isTable(false))
      assert.is_false(M.isTable(nil))
      assert.is_false(M.isTable(true))      
    end)
    
  end) 

  describe('isCallable', function()
  
    it('returns "true" if arg is callable',function()
      assert.is_true(M.isCallable(print))
      assert.is_true(M.isCallable(function() end))
      assert.is_true(M.isCallable(string.gmatch))
      assert.is_true(M.isCallable(setmetatable({},{__index = string}).upper))      
      assert.is_true(M.isCallable(setmetatable({},{__call = function() return end})))      
    end)
    
    it('returns "false" otherwise',function()
      assert.is_false(M.isCallable(1))
      assert.is_false(M.isCallable(''))
      assert.is_false(M.isCallable({}))
      assert.is_false(M.isCallable(false))
      assert.is_false(M.isCallable(nil))
      assert.is_false(M.isCallable(true))      
    end)
    
  end) 

  describe('isArray', function()
  
    it('returns "true" if arg is an array',function()
      assert.is_true(M.isArray({}))
      assert.is_true(M.isArray({1,2,3}))
      assert.is_true(M.isArray({'a','b','c',{}}))
      assert.is_true(M.isArray({false,true}))
      assert.is_true(M.isArray({1,nil}))
    end)
   
    it('returns "false" otherwise',function()
      assert.is_false(M.isArray(1))
      assert.is_false(M.isArray(''))
      assert.is_false(M.isArray(false))
      assert.is_false(M.isArray(nil))
      assert.is_false(M.isArray(true))      
      assert.is_false(M.isArray(print))
      assert.is_false(M.isArray({a = 1, x = 1}))
      assert.is_false(M.isArray({a = 1, 1, 2,3}))
      assert.is_false(M.isArray({1,nil,2}))
      assert.is_false(M.isArray({1,nil,3,k=4}))
      assert.is_false(M.isArray({a=1}))
    end)   
   
    it('returns false on "sparse arrays"',function()
      assert.is_false(M.isArray({[1] = true, [10] = false}))
   end)   
  
  end)
  
  describe('isIterable', function()
  
    it('checks if the given object is iterable with pairs',function()
      assert.is_true(M.isIterable({}))
      assert.is_false(M.isIterable(function() end))
      assert.is_false(M.isIterable(false))
      assert.is_false(M.isIterable(1))
    end)
    
  end)  
     
  describe('isEmpty', function()
  
    it('returns "true" if arg is an empty array',function()
      assert.is_true(M.isEmpty({}))      
    end)
    
    it('returns "false" otherwise',function()
      assert.is_false(M.isEmpty({1,2,3}))
      assert.is_false(M.isEmpty({'a','b','c',{}}))
      assert.is_false(M.isEmpty({nil,false,true}))
    end)

    it('booleans, nil and functions are considered empty',function()
      assert.is_true(M.isEmpty(print))
      assert.is_true(M.isEmpty(nil))
      assert.is_true(M.isEmpty(false))
      assert.is_true(M.isEmpty(true))
    end)
    
    it('handles strings',function()
      assert.is_true(M.isEmpty(''))
      assert.is_false(M.isEmpty('a'))
      assert.is_false(M.isEmpty('bcd'))
      assert.is_false(M.isEmpty(' '))
    end)  
    
  end) 

  describe('isString', function()
  
    it('returns "true" if arg is a string',function()
      assert.is_true(M.isString(''))
      assert.is_true(M.isString('a'))
      assert.is_true(M.isString(' '))
      assert.is_true(M.isString(type(nil)))
    end)
    
    it('returns "false" otherwise',function()
      assert.is_false(M.isString(false))
      assert.is_false(M.isString(print))
      assert.is_false(M.isString(nil))
      assert.is_false(M.isString(true))
      assert.is_false(M.isString({}))
    end)
    
  end)

  describe('isFunction', function()
  
    it('returns "true" if arg is a function',function()
      assert.is_true(M.isFunction(print))
      assert.is_true(M.isFunction(string.match))
      assert.is_true(M.isFunction(function() end))
    end)

    it('returns "false" otherwise',function()
      assert.is_false(M.isFunction({}))
      assert.is_false(M.isFunction(nil))
      assert.is_false(M.isFunction(false))
      assert.is_false(M.isFunction(true))
      assert.is_false(M.isFunction('a'))
    end)    
    
  end)
  
  describe('isNil', function()
  
    it('returns "true" if arg is nil',function()
      assert.is_true(M.isNil(nil))
      assert.is_true(M.isNil())
      assert.is_true(M.isNil(a))
    end)
    
    it('returns "false" otherwise',function()
      assert.is_false(M.isNil(false))
      assert.is_false(M.isNil(true))
      assert.is_false(M.isNil(table))
      assert.is_false(M.isNil(function() end))
      assert.is_false(M.isNil('a'))
    end)     
    
  end) 

  describe('isNumber', function()
  
    it('returns "true" if arg is a number',function()
      assert.is_true(M.isNumber(1))
      assert.is_true(M.isNumber(0.5))
      assert.is_true(M.isNumber(math.pi))
      assert.is_true(M.isNumber(1/0))
      assert.is_true(M.isNumber(math.huge))
      assert.is_true(M.isNumber(0/0))
    end)
    
    it('returns "false" otherwise',function()
      assert.is_false(M.isNumber(print))
      assert.is_false(M.isNumber(nil))
      assert.is_false(M.isNumber(true))
      assert.is_false(M.isNumber(false))
      assert.is_false(M.isNumber({1}))
      assert.is_false(M.isNumber('1'))
    end)    
    
  end)

  describe('isNaN', function()
  
    it('returns "true" if arg is NaN',function()
      assert.is_true(M.isNaN(0/0))  
    end)
    
    it('returns "false" for not NaN values',function()
      assert.is_false(M.isNaN(1/0))  
      assert.is_false(M.isNaN(math.huge))
      assert.is_false(M.isNaN(math.pi))
      assert.is_false(M.isNaN(1))
      assert.is_false(M.isNaN(''))
      assert.is_false(M.isNaN('0'))
      assert.is_false(M.isNaN({}))
      assert.is_false(M.isNaN(nil))
      assert.is_false(M.isNaN(false))
      assert.is_false(M.isNaN(true))
    end)    
    
  end) 
  
  describe('isFinite', function()
  
    it('returns "true" if arg is a finite number',function()
      assert.is_true(M.isFinite(1))
      assert.is_true(M.isFinite(0))
      assert.is_true(M.isFinite(math.pi))
      assert.is_true(M.isFinite(99e99))
    end)
    
    it('returns "false" otherwise',function()
      assert.is_false(M.isFinite(math.huge))
      assert.is_false(M.isFinite(1/0))
      assert.is_false(M.isFinite(0/0))
    end) 

    it('returns "false" for non-numbers',function()
      assert.is_false(M.isFinite(''))
      assert.is_false(M.isFinite(function() end))
      assert.is_false(M.isFinite({}))
    end)    
    
  end)

  describe('isBoolean', function()
 
    it('returns "true" if arg is a boolean or a thruthy statement',function()
      assert.is_true(M.isBoolean(true))
      assert.is_true(M.isBoolean(false))
      assert.is_true(M.isBoolean(1==1))
      assert.is_true(M.isBoolean(print==tostring))     
    end)
    
    it('returns "false" otherwise',function()
      assert.is_false(M.isBoolean(''))
      assert.is_false(M.isBoolean(nil))
      assert.is_false(M.isBoolean({}))
      assert.is_false(M.isBoolean(function() end))

      assert.is_false(M.isBoolean(0))
      assert.is_false(M.isBoolean('1'))
    end)    
    
  end)
  
  describe('isInteger', function()
  
    it('returns "true" if arg is a integer, "false" otherwise',function()
      assert.is_true(M.isInteger(1))
      assert.is_true(M.isInteger(0))
      assert.is_false(M.isInteger(math.pi))
      assert.is_true(M.isInteger(1/0))
      assert.is_true(M.isInteger(math.huge))
      assert.is_false(M.isInteger(0/0))
    end)  
    
  end)
  
end)