/
true_table_spec.rb
799 lines (664 loc) · 19.6 KB
/
true_table_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
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
describe TrueTable do
subject { described_class.new data }
let(:data) do
[
{ year: 2020, population: 2_000_000 },
{ year: 2021, population: 20_000 },
{ year: 2022, population: 200 },
{ year: 2023, population: 2 },
]
end
let(:other) { described_class.new other_data }
let(:other_data) do
[
{ infected: 0 },
{ infected: 1_980_000 },
{ infected: 1_999_800 },
{ infected: 1_999_998 },
]
end
it 'is a subclass of Array' do
expect(subject).to be_an Array
end
describe 'class methods' do
subject { described_class }
describe '::from_csv' do
let(:csv) { File.read 'spec/fixtures/sample.csv' }
it 'returns a TrueTable' do
expect(subject.from_csv csv).to be_a described_class
end
it 'loads the CSV into the table' do
expect(subject.from_csv(csv)[1]).to eq({ population: 20_000, year: 2021 })
end
end
describe '::load_csv' do
let(:path) { 'spec/fixtures/sample.csv' }
it 'returns a TrueTable' do
expect(subject.load_csv path).to be_a described_class
end
it 'loads the CSV into the table' do
expect(subject.load_csv(path)[1]).to eq({ population: 20_000, year: 2021 })
end
end
end
describe '#+' do
it 'combines two tables into a new one' do
result = subject + other
expect(result.to_yaml).to match_approval 'plus'
end
it 'does not alter the original' do
expect(subject.to_yaml).to match_approval 'original'
end
end
describe '#-' do
it 'returns a new table without subtracted columns' do
result = subject - [:population]
expect(result.to_yaml).to match_approval 'minus'
end
it 'does not alter the original' do
expect(subject.to_yaml).to match_approval 'original'
end
end
describe '#[]' do
context 'with an integer key' do
it 'returns a row' do
expect(subject[2][:population]).to eq 200
end
end
context 'with a symbol key' do
it 'returns a column' do
expect(subject[:population][3]).to eq 2
end
end
context 'with a hash key' do
it 'returns a row' do
expect(subject[year: 2023][:population]).to eq 2
end
end
end
describe '#[]=' do
context 'with an integer key' do
it 'adds or updates the row' do
subject[4] = { year: 2024, population: 3 }
expect(subject[:population]).to eq [2_000_000, 20_000, 200, 2, 3]
end
end
context 'with a symbol key' do
it 'adds or updates the column' do
subject[:infected] = [0, 1_980_000, 1_999_800, 1_999_998]
expect(subject[1]).to eq({ population: 20_000, year: 2021, infected: 1_980_000 })
end
end
context 'with a hash key' do
it 'adds the row' do
subject[year: 2023] = { population: 23 }
expect(subject[:population]).to eq [2_000_000, 20_000, 200, 23]
end
end
end
describe '#clone' do
it 'returns a deep copy without altering the original' do
copy = subject.clone
copy[:cured] = ['10%', '5%', '3%', '1%']
expect(subject.headers).to eq %i[year population]
expect(copy.headers).to eq %i[year population cured]
end
end
describe '#col' do
it 'returns a column' do
expect(subject.col(:year)[1]).to eq 2021
end
end
describe '#cols' do
it 'returns a hash of columns' do
expect(subject.cols.to_yaml).to match_approval 'cols'
end
end
describe '#combination' do
it 'yields the row combinations' do
expect(subject.combination(2).to_a.size).to eq 6
end
end
describe '#compact' do
before { data[1][:population] = nil }
it 'returns a copy without rows that contain nil columns' do
expect(subject.compact.count).to eq data.compact.count - 1
end
end
describe '#compact!' do
before { subject[1][:population] = nil }
it 'removes rows with nil columns' do
subject.compact!
expect(subject.count).to eq 3
end
end
describe '#concat' do
it 'appends the other table to self' do
subject.concat other
expect(subject.count).to eq 8
end
end
describe '#cycle' do
it 'calls the block N times for each row' do
count = 0
subject.cycle(2) { |_row| count += 1 }
expect(count).to eq subject.count * 2
end
end
describe '#delete' do
it 'deletes rows for which the block returns true' do
subject.delete({ year: 2021, population: 20_000 })
expect(subject.count).to eq 3
end
end
describe '#delete_at' do
context 'with an integer argument' do
it 'deletes a row in place and returns the deleted row' do
expect(subject.delete_at 1).to eq({ population: 20_000, year: 2021 })
expect(subject.count).to eq 3
end
end
context 'with a symbol argument' do
it 'deletes a column in place and returns the deleted column' do
expect(subject.delete_at :year).to eq [2020, 2021, 2022, 2023]
expect(subject.headers).to eq [:population]
end
end
end
describe '#delete_if' do
it 'deletes rows for which the block returns true' do
subject.delete_if { |row| row[:year] == 2021 }
expect(subject.count).to eq 3
end
end
describe '#difference' do
let(:other) do
[
{ year: 2020, population: 2_000_000 },
{ year: 2023, population: 2 },
]
end
it 'returns a new Table consisting only difference rows' do
result = subject.difference(other)
expect(result.count).to eq 2
expect(result[:year]).to eq [2021, 2022]
end
end
describe '#dig' do
context 'with row, column' do
it 'extracts the nested value' do
expect(subject.dig 1, :year).to eq 2021
end
end
context 'with column, row' do
it 'extracts the nested value' do
expect(subject.dig :year, 1).to eq 2021
end
end
end
describe '#drop' do
it 'drops the first N rows' do
expect(subject.drop(2).count).to eq subject.count - 2
end
it 'returns a Table object' do
expect(subject.drop(2)).to be_a described_class
end
end
describe '#drop_while' do
it 'drops rows until the block returns false' do
result = subject.drop_while { |row| row[:year] < 2022 }
expect(result[0][:year]).to eq 2022
end
it 'returns a Table' do
result = subject.drop_while { |row| row[:year] < 2022 }
expect(result).to be_a described_class
end
end
describe '#each_col' do
it 'yields the columns and their names' do
subject.each_col do |col, header|
expect(col).to eq subject[header]
end
end
end
describe '#each_row' do
it 'yields the rows and their indexes' do
subject.each_row do |row, i|
expect(row).to eq subject[i]
end
end
end
describe '#eql?' do
it 'returns true for identical objects' do
expect(subject.eql? subject.dup).to be true
end
end
describe '#fetch' do
context 'with an integer argument' do
it 'returns a row' do
expect(subject.fetch 1).to eq({ year: 2021, population: 20_000 })
end
context 'when it is out of bounds' do
it 'raises IndexError' do
expect { subject.fetch 99 }.to raise_error(IndexError)
end
end
context 'when it is out of bounds but a default is given' do
it 'returns the default value' do
expect(subject.fetch 99, 'ok').to eq 'ok'
end
end
end
context 'with a symbol argument' do
it 'returns a column' do
expect(subject.fetch :year).to eq([2020, 2021, 2022, 2023])
end
context 'when it is out of bounds' do
it 'raises IndexError' do
expect { subject.fetch :no_such_column }.to raise_error(IndexError)
end
end
context 'when it is out of bounds but a default is given' do
it 'returns the default value' do
expect(subject.fetch :no_such_column, 'ok').to eq 'ok'
end
end
context 'when it is out of bounds but a block is given' do
it 'returns the default value' do
result = subject.fetch :no_such_column do |index|
"received :#{index}"
end
expect(result).to eq 'received :no_such_column'
end
end
end
end
describe '#fill' do
it 'replaces the rows' do
subject.fill { |index| { pos: index } }
expect(subject[:pos]).to eq [0, 1, 2, 3]
end
end
describe '#filter' do
it 'returns a new table with matching rows' do
result = subject.filter { |row| row[:population] < 20_000 }
expect(result).to be_a described_class
expect(result.count).to eq 2
expect(subject.count).to eq 4
end
end
describe '#filter!' do
it 'keeps only selected rows' do
subject.filter! { |row| row[:population] < 20_000 }
expect(subject[:population]).to eq [200, 2]
end
end
describe '#find_index' do
it 'returns the index of the first matching row' do
result = subject.find_index { |row| row[:year] == 2021 }
expect(result).to eq 1
end
end
# describe '#flatten' - not relevant
# describe '#flatten!' - not relevant
describe '#headers' do
it 'returns the hash keys from the first row' do
expect(subject.headers).to eq %i[year population]
end
end
describe '#include?' do
it 'returns true of the row exists' do
expect(subject.include? year: 2020, population: 2_000_000).to be true
end
end
describe '#index' do
it 'returns the index of the first matching row' do
result = subject.index { |row| row[:year] == 2021 }
expect(result).to eq 1
end
end
describe '#insert' do
it 'inserts the given rows before the given index' do
subject.insert(2, other[0], other[1])
expect(subject[2]).to eq other[0]
expect(subject[3]).to eq other[1]
expect(subject.count).to eq 6
end
end
describe '#intersection' do
let(:other) do
[
{ year: 2021, population: 20_000 },
{ year: 2023, population: 2 },
]
end
it 'returns a new table with intersecting rows' do
result = subject.intersection other
expect(result[:year]).to eq [2021, 2023]
end
end
describe '#join' do
context 'with no arguments' do
it 'returns a joined string' do
expect(subject.join).to match_approval 'join-no-args'
end
end
context 'with row separator argument' do
it 'returns a joined string using the separator as glue' do
expect(subject.join ' | ').to match_approval 'join-rows'
end
end
context 'with row and col separator arguments' do
it 'returns a joined string using the separators as glue' do
expect(subject.join "\n", ',').to match_approval 'join-rows-cols'
end
end
end
describe '#keep_if' do
it 'keeps only rows for which the block returns true' do
subject.keep_if { |row| row[:year] < 2022 }
expect(subject[:year]).to eq [2020, 2021]
end
end
describe '#length' do
it 'returns the number of rows' do
expect(subject.length).to eq 4
end
end
describe '#max' do
it 'returns the row with the maximum block value' do
result = subject.max_by { |a| a[:population] }
expect(result[:year]).to eq 2020
end
end
describe '#min' do
it 'returns the row with the minimum block value' do
result = subject.min_by { |a| a[:population] }
expect(result[:year]).to eq 2023
end
end
describe '#minmax' do
it 'returns a two element array with min and max rows' do
result = subject.minmax_by { |a| a[:population] }
expect(result[0][:year]).to eq 2023
expect(result[1][:year]).to eq 2020
end
end
describe '#permutation' do
it 'yields the row permutations' do
expect(subject.permutation.count).to eq 24
end
end
describe '#pop' do
it 'removes the last element and returns it' do
expect(subject.pop[:year]).to eq 2023
expect(subject.count).to eq 3
end
end
describe '#prepend' do
it 'inserts a row at the beginning' do
subject.prepend({ year: 2019, population: 3_000_000 })
expect(subject.count).to eq 5
expect(subject[0][:year]).to eq 2019
end
end
describe '#push' do
it 'appends a row to the table' do
subject.push year: 2024, population: 0
expect(subject.count).to eq 5
end
end
describe '#reject' do
it 'returns a new table without matching rows' do
result = subject.reject { |row| row[:population] < 20_000 }
expect(result).to be_a described_class
expect(result.count).to eq 2
expect(subject.count).to eq 4
end
end
describe '#reject!' do
it 'keeps only non-rejected rows' do
subject.reject! { |row| row[:population] < 20_000 }
expect(subject[:population]).to eq [2_000_000, 20_000]
end
end
describe '#replace' do
it 'replaces the table with a new table' do
subject.replace other
expect(subject[:infected][1]).to eq 1_980_000
expect(subject).to be_a described_class
end
end
describe '#reverse' do
it 'returns a new reversed table' do
result = subject.reverse
expect(result).to be_a described_class
expect(result.first[:year]).to eq subject.last[:year]
end
end
describe '#reverse!' do
it 'reverses the table in place' do
original_year = subject.last[:year]
subject.reverse!
expect(subject.first[:year]).to eq original_year
end
end
describe '#rindex' do
it 'returns the index of the last matching row' do
result = subject.rindex { |row| row[:year] <= 2021 }
expect(result).to eq 1
end
end
describe '#row' do
context 'with an integer key' do
it 'returns a row' do
expect(subject[2][:population]).to eq 200
end
end
context 'with a hash key' do
it 'returns a row' do
expect(subject[year: 2023][:population]).to eq 2
end
end
end
describe '#rotate' do
it 'offsets the rows to the top' do
expect(subject.rotate(2)[0][:year]).to eq 2022
expect(subject.rotate(2)).to be_a described_class
end
end
describe '#rotate!' do
it 'rotates the table in place' do
subject.rotate! 2
expect(subject[0][:year]).to eq 2022
expect(subject).to be_a described_class
end
end
describe '#row' do
it 'returns a row' do
expect(subject.row(1)[:year]).to eq 2021
end
end
describe '#sample' do
it 'returns one a random row' do
expect(subject.sample).to be_a Hash
end
context 'with an argument' do
it 'returns an array with multiple random rows' do
expect(subject.sample(2).count).to eq 2
end
end
end
describe '#save_csv' do
it 'saves the table as a CSV file' do
expect(File).to receive(:write).with('some-path.csv', subject.to_csv)
subject.save_csv 'some-path.csv'
end
end
describe '#save_tsv' do
it 'saves the table as a TSV file' do
expect(File).to receive(:write).with('some-path.tsv', subject.to_tsv)
subject.save_tsv 'some-path.tsv'
end
end
describe '#select' do
it 'returns a new table with matching rows' do
result = subject.select { |row| row[:population] < 20_000 }
expect(result).to be_a described_class
expect(result.count).to eq 2
expect(subject.count).to eq 4
end
end
describe '#select!' do
it 'keeps only selected rows' do
subject.select! { |row| row[:population] < 20_000 }
expect(subject[:population]).to eq [200, 2]
end
end
describe '#shift' do
it 'removes the first N rows' do
expect(subject.shift[:year]).to eq 2020
expect(subject.count).to eq 3
expect(subject).to be_a described_class
end
end
describe '#shuffle' do
it 'returns a new table with shuffled rows' do
expect(subject.shuffle).to be_a described_class
end
end
describe '#shuffle!' do
it 'shuffles the table in place' do
subject.shuffle!
expect(subject).to be_a described_class
end
end
describe '#slice' do
context 'with one argument' do
it 'returns the row at index' do
expect(subject.slice(1)[:year]).to eq 2021
end
end
context 'with two arguments' do
it 'returns a new table slice' do
result = subject.slice(1, 2)
expect(result).to be_a described_class
expect(result.count).to eq 2
expect(result.first[:year]).to eq 2021
end
end
end
describe '#slice!' do
it 'deletes and returns one or more rows' do
result = subject.slice!(0, 3)
expect(result).to be_a described_class
expect(result.count).to eq 3
expect(subject.count).to eq 1
end
end
describe '#sort' do
it 'returns a new sorted array' do
result = subject.sort_by { |a| a[:population] }
expect(result).to be_a described_class
expect(result[0][:population]).to eq 2
expect(result[3][:population]).to eq 2_000_000
expect(subject[0][:population]).to eq 2_000_000
end
end
describe '#sort!' do
it 'sorts the table in place' do
subject.sort_by! { |a| a[:population] }
expect(subject[0][:population]).to eq 2
expect(subject[3][:population]).to eq 2_000_000
end
end
describe '#sort_by' do
it 'returns a new sorted array' do
result = subject.sort_by { |row| row[:population] }
expect(result).to be_a described_class
expect(result[0][:population]).to eq 2
expect(result[3][:population]).to eq 2_000_000
expect(subject[0][:population]).to eq 2_000_000
end
end
describe '#sort_by!' do
it 'sorts the table in place' do
subject.sort_by! { |row| row[:population] }
expect(subject[0][:population]).to eq 2
expect(subject[3][:population]).to eq 2_000_000
end
end
describe '#take' do
it 'returns a new table with the first N rows' do
result = subject.take 2
expect(result).to be_a described_class
expect(result.count).to eq 2
expect(subject.count).to eq 4
end
end
describe '#take_while' do
it 'takes rows until the block returns false' do
result = subject.take_while { |row| row[:year] < 2022 }
expect(result.count).to eq 2
expect(result[1][:year]).to eq 2021
end
it 'returns a Table' do
result = subject.take_while { |row| row[:year] < 2022 }
expect(result).to be_a described_class
end
end
describe '#to_a' do
it 'returns an Array' do
expect(subject.to_a).to be_an Array
expect(subject.to_a).not_to be_a described_class
end
end
describe '#to_ary' do
it 'returns self' do
expect(subject.to_ary).to be_a described_class
end
end
describe '#to_csv' do
it 'returns a CSV string' do
expect(subject.to_csv).to match_approval 'csv'
end
end
describe '#to_tsv' do
it 'returns a TSV string' do
expect(subject.to_tsv).to match_approval 'tsv'
end
end
describe '#to_h' do
it 'returns a hash using the values of the first column as keys' do
result = subject.to_h
expect(result).to be_a Hash
expect(result[2022][:population]).to eq 200
end
context 'when a block is given' do
it 'uses the provided block to generate the hash' do
result = subject.to_h { |row| [row[:year], row[:population]] }
expect(result).to be_a Hash
expect(result[2022]).to eq 200
end
end
end
describe '#union' do
pending 'To be completed'
end
describe '#values' do
it 'returns an array of arrays' do
result = subject.values
expect(result).to be_an Array
expect(result.first).to eq [2020, 2_000_000]
end
end
describe '#values_at' do
pending 'To be completed'
end
describe '#zip' do
pending 'To be completed'
end
describe '#|' do
pending 'To be completed'
end
end