Runtime assertions for Ruby
literal.fun
ruby
1# frozen_string_literal: true
2
3include Literal::Types
4
5test "coerce" do
6 original = ["Joel", "Stephen"]
7 coerced = Literal::Array(String).coerce(original)
8
9 assert_equal coerced, Literal::Array(String).new(
10 "Joel", "Stephen"
11 )
12end
13
14test "coerce with invalid values" do
15 original = ["Joel", "Stephen"]
16
17 assert_raises(Literal::TypeError) do
18 Literal::Array(Integer).coerce(original)
19 end
20end
21
22test "to_proc" do
23 mapped = [
24 ["Joel", "Stephen"],
25 ].map(&Literal::Array(String))
26
27 assert_equal mapped, [
28 Literal::Array(String).new(
29 "Joel", "Stephen"
30 ),
31 ]
32end
33
34test "===" do
35 assert Literal::Array(_Boolean) === Literal::Array(true).new(true)
36 assert Literal::Array(Object) === Literal::Array(Integer).new(1)
37 assert Literal::Array(Numeric) === Literal::Array(Integer).new(1)
38 assert Literal::Array(Numeric) === Literal::Array(Float).new(1.0)
39
40 assert Literal::Array(
41 Literal::Array(Numeric)
42 ) === Literal::Array(
43 Literal::Array(Integer)
44 ).new(
45 Literal::Array(Integer).new(1)
46 )
47
48 refute Literal::Array(true) === Literal::Array(_Boolean).new(true, false)
49 refute Literal::Array(Integer) === Literal::Array(Numeric).new(1, 1.234)
50end
51
52test "#initialize" do
53 assert_raises(Literal::TypeError) do
54 Literal::Array(String).new(1, 2, 3)
55 end
56end
57
58test "#to_a" do
59 array = Literal::Array(Integer).new(1, 2, 3)
60 assert_equal array.to_a, [1, 2, 3]
61end
62
63test "#to_a doesn't return the internal array" do
64 array = Literal::Array(Integer).new(1, 2, 3)
65 refute_same array.__value__, array.to_a
66end
67
68test "#map maps each item correctly" do
69 array = Literal::Array(Integer).new(1, 2, 3)
70
71 mapped = array.map(String, &:to_s)
72 assert_equal mapped.to_a, ["1", "2", "3"]
73end
74
75test "#map raises if the type is wrong" do
76 array = Literal::Array(Integer).new(1, 2, 3)
77
78 assert_raises(Literal::TypeError) do
79 array.map(Integer, &:to_s)
80 end
81end
82
83test "#map! maps each item correctly" do
84 array = Literal::Array(Integer).new(1, 2, 3)
85
86 array.map!(&:succ)
87
88 assert_equal array.to_a, [2, 3, 4]
89end
90
91test "map! raises if the type is wrong" do
92 array = Literal::Array(Integer).new(1, 2, 3)
93
94 assert_raises(Literal::TypeError) do
95 array.map!(&:to_s)
96 end
97end
98
99test "#[]" do
100 array = Literal::Array(Integer).new(1, 2, 3)
101
102 assert_equal array[0], 1
103 assert_equal array[1], 2
104 assert_equal array[2], 3
105 assert_equal array[3], nil
106end
107
108test "#[]= raises if the type is wrong" do
109 array = Literal::Array(Integer).new(1, 2, 3)
110
111 assert_raises Literal::TypeError do
112 array[0] = "1"
113 end
114end
115
116test "#[]= works as expected" do
117 array = Literal::Array(Integer).new(1, 2, 3)
118
119 array[0] = 5
120 array[3] = 6
121
122 assert_equal array[0], 5
123 assert_equal array[3], 6
124end
125
126test "#== compares the Literal::Arrays" do
127 array = Literal::Array(Integer).new(1, 2, 3)
128 other = Literal::Array(Integer).new(1, 2, 3)
129
130 assert_equal (array == other), true
131end
132
133test "#<< inserts a new item" do
134 array = Literal::Array(Integer).new(1, 2, 3)
135
136 array << 4
137
138 assert_equal array.to_a, [1, 2, 3, 4]
139end
140
141test "#<< raises if the type is wrong" do
142 array = Literal::Array(Integer).new(1, 2, 3)
143
144 assert_raises(Literal::TypeError) do
145 array << "4"
146 end
147end
148
149test "#& performs bitwise AND with another Literal::Array" do
150 array = Literal::Array(Integer).new(1, 2, 3)
151 other = Literal::Array(Integer).new(2, 3, 4)
152
153 assert_equal (array & other).to_a, [2, 3]
154end
155
156test "#& performs bitwise AND with a regular Array" do
157 array = Literal::Array(Integer).new(1, 2, 3)
158 other = [2, 3, 4]
159
160 assert_equal (array & other).to_a, [2, 3]
161end
162
163test "#* with an integer multiplies the array" do
164 array = Literal::Array(Integer).new(1, 2, 3)
165
166 result = array * 2
167
168 assert Literal::Array(Integer) === result
169 assert_equal result.to_a, [1, 2, 3, 1, 2, 3]
170end
171
172test "#* raises with a negative integer" do
173 array = Literal::Array(Integer).new(1, 2, 3)
174
175 assert_raises(ArgumentError) do
176 array * -1
177 end
178end
179
180test "#* with a string joins the elements" do
181 array = Literal::Array(Integer).new(1, 2, 3)
182 result = array * ","
183
184 assert_equal result, "1,2,3"
185end
186
187test "#+ adds another Literal::Array" do
188 array = Literal::Array(Integer).new(1, 2, 3)
189 other = Literal::Array(Integer).new(4, 5)
190
191 result = array + other
192 assert Literal::Array(Integer) === result
193 assert_equal result.to_a, [1, 2, 3, 4, 5]
194end
195
196test "#+ adds an array" do
197 array = Literal::Array(Integer).new(1, 2, 3)
198 other = [4, 5]
199
200 result = array + other
201 assert Literal::Array(Integer) === result
202 assert_equal result.to_a, [1, 2, 3, 4, 5]
203end
204
205test "#+ raises if the type is wrong" do
206 array = Literal::Array(Integer).new(1, 2, 3)
207 other = Literal::Array(String).new("a", "b")
208 other_primitive = ["a", "b"]
209
210 assert_raises(Literal::TypeError) do
211 array + other
212 end
213
214 assert_raises(Literal::TypeError) do
215 array + other_primitive
216 end
217end
218
219test "#- removes elements from another Literal::Array" do
220 array = Literal::Array(Integer).new(1, 2, 3)
221 other = Literal::Array(Integer).new(1)
222
223 result = array - other
224
225 assert_equal result.to_a, [2, 3]
226end
227
228test "#- removes elements from an array" do
229 array = Literal::Array(Integer).new(1, 2, 3)
230 other = [1]
231
232 result = array - other
233 assert_equal result.to_a, [2, 3]
234end
235
236test "#<=> works as expected" do
237 array = Literal::Array(Integer).new(1, 2, 3)
238
239 assert_equal (array <=> [1, 2, 4]), -1
240 assert_equal (array <=> [1, 2, 2]), 1
241 assert_equal (array <=> [1, 2, 3, 4]), -1
242 assert_equal (array <=> [1, 2]), 1
243 assert_equal (array <=> [1, 2, 3]), 0
244end
245
246test "#<=> works with another Literal::Array" do
247 array = Literal::Array(Integer).new(1, 2, 3)
248 other = Literal::Array(Integer).new(1, 2, 4)
249
250 assert_equal (array <=> other), -1
251end
252
253test "#sort returns a new sorted array" do
254 array = Literal::Array(Integer).new(3, 2, 1)
255
256 result = array.sort
257
258 assert Literal::Array(Integer) === result
259 assert_equal result.to_a, [1, 2, 3]
260end
261
262test "#sort! sorts the array in place" do
263 array = Literal::Array(Integer).new(3, 2, 1)
264 array.sort!
265
266 assert_equal array.to_a, [1, 2, 3]
267end
268
269test "#sort_by! sorts the array given a block" do
270 array = Literal::Array(String).new("Lucy", "Stephen", "Bob")
271 array.sort_by!(&:size)
272
273 assert_equal array.to_a, ["Bob", "Lucy", "Stephen"]
274end
275
276test "#push appends single value" do
277 array = Literal::Array(Integer).new(1, 2, 3)
278
279 return_value = array.push(4)
280
281 assert_same return_value, array
282 assert_equal array.to_a, [1, 2, 3, 4]
283end
284
285test "#push appends multiple values" do
286 array = Literal::Array(Integer).new(1, 2, 3)
287
288 return_value = array.push(4, 5)
289
290 assert_same return_value, array
291 assert_equal array.to_a, [1, 2, 3, 4, 5]
292end
293
294test "#push raises if any type is wrong" do
295 array = Literal::Array(Integer).new(1, 2, 3)
296
297 assert_raises(Literal::TypeError) do
298 array.push("4")
299 end
300
301 assert_raises(Literal::TypeError) do
302 array.push(4, "5")
303 end
304end
305
306test "#assoc returns the correct element" do
307 array = Literal::Array(Array).new([1, 2], [3, 4])
308
309 assert_equal array.assoc(1), [1, 2]
310 assert_equal array.assoc(3), [3, 4]
311end
312
313test "#combination yields to the block" do
314 array = Literal::Array(Integer).new(1, 2, 3)
315 results = []
316
317 array.combination(2) { |x| results << x }
318
319 assert_equal results, [[1, 2], [1, 3], [2, 3]]
320end
321
322test "#combination returns self" do
323 array = Literal::Array(Integer).new(1, 2, 3)
324 return_value = array.combination(0) { nil }
325
326 assert_same return_value, array
327end
328
329test "#compact returns a new Literal::Array" do
330 array = Literal::Array(Integer).new(1, 2, 3)
331
332 result = array.compact
333
334 refute_same array, result
335 assert Literal::Array(Integer) === result
336 assert_equal result.to_a, [1, 2, 3]
337end
338
339test "#compact! returns nil" do
340 array = Literal::Array(Integer).new(1, 2, 3)
341 return_value = array.compact!
342
343 assert_equal return_value, nil
344end
345
346test "#delete deletes the element" do
347 array = Literal::Array(Integer).new(1, 2, 3)
348
349 return_value = array.delete(2)
350
351 assert_equal return_value, 2
352 assert_equal array, Literal::Array(Integer).new(1, 3)
353end
354
355test "#delete_at deletes the element at the index" do
356 array = Literal::Array(Integer).new(1, 2, 3)
357
358 return_value = array.delete_at(1)
359
360 assert_equal return_value, 2
361 assert_equal array, Literal::Array(Integer).new(1, 3)
362end
363
364test "#delete_if deletes elements that match the block" do
365 array = Literal::Array(Integer).new(1, 2, 3)
366
367 return_value = array.delete_if { |i| i < 2 }
368
369 assert_same return_value, array
370 assert_equal array, Literal::Array(Integer).new(2, 3)
371end
372
373test "#drop returns a new array with the first n elements removed" do
374 array = Literal::Array(Integer).new(1, 2, 3)
375 dropped = array.drop(1)
376
377 refute_same dropped, array
378 assert Literal::Array(Integer) === dropped
379 assert_equal dropped, Literal::Array(Integer).new(2, 3)
380end
381
382test "#drop_while returns a new array with the first n elements removed" do
383 array = Literal::Array(Integer).new(1, 2, 3)
384 dropped = array.drop_while { |i| i < 2 }
385
386 refute_same dropped, array
387 assert Literal::Array(Integer) === dropped
388 assert_equal dropped, Literal::Array(Integer).new(2, 3)
389end
390
391test "#each_index iterates through the indexes" do
392 array = Literal::Array(Integer).new(1, 2, 3)
393 indexes = []
394
395 array.each_index { |i| indexes << i }
396
397 assert_equal indexes, [0, 1, 2]
398end
399
400test "#insert inserts single element at index offset" do
401 array = Literal::Array(Integer).new(1, 2, 3)
402
403 return_value = array.insert(1, 4)
404
405 assert_same return_value, array
406 assert_equal array, Literal::Array(Integer).new(1, 4, 2, 3)
407end
408
409test "#insert inserts multiple elements at index offset" do
410 array = Literal::Array(Integer).new(1, 2, 3)
411
412 return_value = array.insert(1, 4, 5, 6)
413
414 assert_same return_value, array
415 assert_equal array, Literal::Array(Integer).new(1, 4, 5, 6, 2, 3)
416end
417
418test "#insert raises if any type is wrong" do
419 array = Literal::Array(Integer).new(1, 2, 3)
420
421 assert_raises(Literal::TypeError) do
422 array.insert(1, "4")
423 end
424
425 assert_raises(Literal::TypeError) do
426 array.insert(1, 4, "5", 6)
427 end
428end
429
430test "#intersect? returns true if the arrays intersect" do
431 array = Literal::Array(Integer).new(1, 2, 3)
432 other_true = Literal::Array(Integer).new(2, 3, 4)
433 other_false = Literal::Array(Integer).new(4, 5, 6)
434
435 assert array.intersect?(other_true)
436 refute array.intersect?(other_false)
437end
438
439test "#intersection returns an array of the intersection of two arrays" do
440 array = Literal::Array(Integer).new(1, 2, 3)
441 other = Literal::Array(Integer).new(2, 3, 4)
442
443 intersection = array.intersection(other, [2])
444
445 assert_equal intersection, Literal::Array(Integer).new(2)
446end
447
448test "#join joins the elements into a string" do
449 array = Literal::Array(Integer).new(1, 2, 3)
450
451 assert_equal array.join, "123"
452 assert_equal array.join(", "), "1, 2, 3"
453end
454
455test "#keep_if keeps elements that match the block" do
456 array = Literal::Array(Integer).new(1, 2, 3)
457
458 return_value = array.keep_if { |i| i > 1 }
459
460 assert_same return_value, array
461 assert_equal array, Literal::Array(Integer).new(2, 3)
462end
463
464test "#keep_if returns an enumerator if no block is given" do
465 array = Literal::Array(Integer).new(1, 2, 3)
466
467 return_value = array.keep_if
468
469 assert_equal return_value.class, Enumerator
470end
471
472test "#replace replaces with regular Array" do
473 array = Literal::Array(Integer).new(1, 2, 3)
474
475 return_value = array.replace([4, 5, 6])
476
477 assert_same return_value, array
478 assert_equal array, Literal::Array(Integer).new(4, 5, 6)
479end
480
481test "#replace replaces with Literal::Array" do
482 array = Literal::Array(Integer).new(1, 2, 3)
483 other = Literal::Array(Integer).new(4, 5, 6)
484
485 return_value = array.replace(other)
486
487 assert_same return_value, array
488 refute_same array, other
489 assert_equal array, other
490end
491
492test "#replace raises if type of any element in array is wrong" do
493 array = Literal::Array(Integer).new(1, 2, 3)
494
495 assert_raises(Literal::TypeError) do
496 array.replace([1, "4"])
497 end
498
499 assert_raises(Literal::TypeError) do
500 array.replace(
501 Literal::Array(String).new
502 )
503 end
504end
505
506test "#replace raises with non-array argument" do
507 array = Literal::Array(Integer).new(1, 2, 3)
508
509 assert_raises(ArgumentError) do
510 array.replace("not an array")
511 end
512end
513
514test "#values_at returns the values at the given indexes" do
515 array = Literal::Array(Integer).new(1, 2, 3)
516
517 assert_equal array.values_at(0), Literal::Array(Integer).new(1)
518 assert_equal array.values_at(1), Literal::Array(Integer).new(2)
519 assert_equal array.values_at(2), Literal::Array(Integer).new(3)
520 assert_equal array.values_at(1..2), Literal::Array(Integer).new(2, 3)
521
522 assert_raises IndexError do
523 array.values_at(3)
524 end
525
526 assert_raises IndexError do
527 array.values_at(-4)
528 end
529
530 assert_raises IndexError do
531 array.values_at(-4..2)
532 end
533
534 assert_raises IndexError do
535 array.values_at(1..3)
536 end
537end
538
539test "#values_at on a nilable array returns the values at the given indexes" do
540 array = Literal::Array(_Nilable(Integer)).new(1, 2, 3)
541
542 # TODO: We could do some type narrowing here.
543 assert_equal array.values_at(-4), Literal::Array(_Nilable(Integer)).new(nil)
544 assert_equal array.values_at(3), Literal::Array(_Nilable(Integer)).new(nil)
545end
546
547test "#uniq! removes duplicates" do
548 array = Literal::Array(Integer).new(1, 2, 3, 2, 1)
549
550 return_value = array.uniq!
551
552 assert_same return_value, array
553 assert_equal array, Literal::Array(Integer).new(1, 2, 3)
554end
555
556test "#uniq! returns nil if no duplicates" do
557 array = Literal::Array(Integer).new(1, 2, 3)
558
559 return_value = array.uniq!
560
561 assert_equal return_value, nil
562end
563
564test "#uniq returns a new array with duplicates removed" do
565 array = Literal::Array(Integer).new(1, 2, 2, 3, 3, 3)
566
567 return_value = array.uniq
568
569 refute_same return_value, array
570 assert_equal return_value, Literal::Array(Integer).new(1, 2, 3)
571end
572
573test "#| returns a union of two Literal::Arrays" do
574 array = Literal::Array(Integer).new(1, 2, 3)
575 other = Literal::Array(Integer).new(2, 3, 4)
576
577 union = array | other
578
579 refute_same union, array
580 refute_same union, other
581
582 assert_equal union, Literal::Array(Integer).new(1, 2, 3, 4)
583end
584
585test "#| returns a union of a Literal::Array and an Array" do
586 array = Literal::Array(Integer).new(1, 2, 3)
587 other = [2, 3, 4]
588
589 union = array | other
590
591 assert_equal union, Literal::Array(Integer).new(1, 2, 3, 4)
592end
593
594test "#| raises if the type is wrong" do
595 array = Literal::Array(Integer).new(1, 2, 3)
596 other = Literal::Array(String).new("2", "3")
597 other_primitive = ["2", "3"]
598
599 assert_raises(Literal::TypeError) do
600 array | other
601 end
602
603 assert_raises(Literal::TypeError) do
604 array | other_primitive
605 end
606end
607
608test "#sum" do
609 assert_equal Literal::Array(Integer).new(1, 2, 3).sum, 6
610 assert_equal Literal::Array(String).new("1", "2", "3").sum(&:to_i), 6
611end
612
613test "#select returns a new array with elements that match the block" do
614 array = Literal::Array(Integer).new(1, 2, 3)
615
616 return_value = array.select { |i| i > 1 }
617
618 refute_same return_value, array
619 assert_equal return_value, Literal::Array(Integer).new(2, 3)
620end
621
622test "#select! removes elements that do not match the block" do
623 array = Literal::Array(Integer).new(1, 2, 3)
624
625 return_value = array.select! { |i| i > 1 }
626
627 assert_same return_value, array
628 assert_equal array, Literal::Array(Integer).new(2, 3)
629end
630
631test "#select! empties the array if no elements match the block" do
632 array = Literal::Array(Integer).new(1, 2, 3)
633
634 return_value = array.select! { |i| i > 4 }
635
636 assert_same return_value, array
637 assert_equal return_value, Literal::Array(Integer).new
638end
639
640test "#narrow" do
641 array = Literal::Array(Numeric).new(1, 2, 3)
642
643 return_value = array.narrow(Integer)
644 assert Literal::Array(Integer) === return_value
645end
646
647test "#narrow with same type" do
648 array = Literal::Array(Numeric).new(1, 2, 3)
649
650 assert Literal::Array(Numeric) === array
651end
652
653test "#narrow with wrong value" do
654 array = Literal::Array(Numeric).new(1, 2, 3.456)
655
656 assert_raises(Literal::TypeError) do
657 array.narrow(Integer)
658 end
659end
660
661test "#narrow with wrong type" do
662 array = Literal::Array(Integer).new(1, 2, 3)
663
664 assert_raises(ArgumentError) do
665 array.narrow(Numeric)
666 end
667end
668
669test "#flatten! flattens the array" do
670 array = Literal::Array(Array).new([1, 2], [3, 4])
671
672 return_value = array.flatten!
673
674 assert_same return_value, array
675 assert_equal return_value, Literal::Array(Integer).new(1, 2, 3, 4)
676end
677
678test "#flatten! returns nil if no nested arrays" do
679 array = Literal::Array(Integer).new(1, 2, 3)
680
681 return_value = array.flatten!
682
683 assert_equal return_value, nil
684end
685
686test "#flatten flattens the array" do
687 array = Literal::Array(Array).new([1, 2], [3, 4])
688
689 return_value = array.flatten
690
691 refute_same return_value, array
692 assert_equal return_value, Literal::Array(Integer).new(1, 2, 3, 4)
693end
694
695test "#flatten with level flattens the array" do
696 array = Literal::Array(Array).new([1, 2], [3, 4])
697
698 return_value = array.flatten(1)
699
700 refute_same return_value, array
701 assert_equal return_value, Literal::Array(Integer).new(1, 2, 3, 4)
702end
703
704test "#fetch" do
705 array = Literal::Array(Integer).new(1, 2, 3)
706
707 assert_equal array.fetch(0), 1
708 assert_equal array.fetch(1), 2
709 assert_equal array.fetch(2), 3
710
711 assert_raises(IndexError) { array.fetch(3) }
712end
713
714test "#inspect returns a string representation of the array" do
715 array = Literal::Array(Integer).new(1, 2, 3)
716
717 assert_equal array.inspect, "Literal::Array(Integer)[1, 2, 3]"
718end
719
720test "#to_s returns a string representation of the array" do
721 array = Literal::Array(Integer).new(1, 2, 3)
722
723 assert_equal array.to_s, "[1, 2, 3]"
724end
725
726test "#fetch returns default value if element is missing at index" do
727 array = Literal::Array(Integer).new(1, 2, 3)
728
729 assert_equal array.fetch(4, :missing), :missing
730end
731
732test "#fetch returns value of block if element is missing at index" do
733 array = Literal::Array(Integer).new(1, 2, 3)
734
735 assert_equal array.fetch(4) { |index| index * 2 }, 8
736end
737
738test "#include? returns true if array contains value" do
739 array = Literal::Array(Integer).new(1, 2, 3)
740
741 assert array.include?(2)
742 refute array.include?(4)
743end
744
745test "#reverse returns a new reversed array" do
746 array = Literal::Array(Integer).new(1, 2, 3)
747
748 return_value = array.reverse
749
750 assert_equal return_value, Literal::Array(Integer).new(3, 2, 1)
751end
752
753test "#rotate! rotates the array" do
754 array = Literal::Array(Integer).new(1, 2, 3)
755
756 return_value = array.rotate!
757
758 assert_same return_value, array
759 assert_equal array, Literal::Array(Integer).new(2, 3, 1)
760end
761
762test "#rotate rotates the array" do
763 array = Literal::Array(Integer).new(1, 2, 3)
764
765 return_value = array.rotate
766
767 refute_same return_value, array
768 assert_equal return_value, Literal::Array(Integer).new(2, 3, 1)
769end
770
771test "#reverse! reverses the array in place" do
772 array = Literal::Array(Integer).new(1, 2, 3)
773
774 return_value = array.reverse!
775
776 assert_same return_value, array
777end
778
779test "#take takes the first n elements" do
780 array = Literal::Array(Integer).new(1, 2, 3, 4, 5)
781
782 return_value = array.take(2)
783
784 refute_same return_value, array
785 assert_equal return_value, Literal::Array(Integer).new(1, 2)
786end
787
788test "#take_while takes elements where the block returns true" do
789 array = Literal::Array(Integer).new(1, 2, 3, 4, 5)
790
791 return_value = array.take_while { |i| i < 3 }
792
793 refute_same return_value, array
794 assert_equal return_value, Literal::Array(Integer).new(1, 2)
795end
796
797test "#shuffle returns a new shuffled array" do
798 array = Literal::Array(Integer).new(1, 2, 3, 4, 5)
799 random = Random.new(42)
800
801 return_value = array.shuffle(random:)
802
803 assert_equal return_value, Literal::Array(Integer).new(2, 5, 3, 1, 4)
804end
805
806test "#shuffle! shuffles the array" do
807 array = Literal::Array(Integer).new(1, 2, 3, 4, 5)
808 random = Random.new(42)
809
810 result = array.shuffle!(random:)
811
812 assert_same result, array
813 assert_equal result, Literal::Array(Integer).new(2, 5, 3, 1, 4)
814end
815
816test "#product with block" do
817 a = Literal::Array(Integer).new(1, 2)
818 b = Literal::Array(String).new("a", "b")
819
820 yielded = []
821
822 result = a.product(b) { |x, y| yielded << [x, y] }
823
824 assert_same result, a
825 assert_equal yielded, [[1, "a"], [1, "b"], [2, "a"], [2, "b"]]
826end
827
828test "#product with another Literal::Array" do
829 a = Literal::Array(Integer).new(1, 2)
830 b = Literal::Array(String).new("a", "b")
831
832 result = a.product(b)
833
834 assert Literal::Array(Literal::Tuple(Integer, String)) === result
835
836 assert_equal result.size, 4
837 assert_equal result.first, Literal::Tuple(Integer, String).new(1, "a")
838end
839
840test "#transpose with a nested literal tuple" do
841 array = Literal::Array(
842 Literal::Tuple(Integer, String)
843 ).new(
844 Literal::Tuple(Integer, String).new(1, "a"),
845 Literal::Tuple(Integer, String).new(2, "b"),
846 )
847
848 assert_equal array.transpose, Literal::Tuple(
849 Literal::Array(Integer),
850 Literal::Array(String),
851 ).new(
852 Literal::Array(Integer).new(1, 2),
853 Literal::Array(String).new("a", "b"),
854 )
855end
856
857test "#transpose with a nested literal array" do
858 array = Literal::Array(Literal::Array(Integer)).new(
859 Literal::Array(Integer).new(1, 2),
860 Literal::Array(Integer).new(3, 4),
861 )
862
863 assert_equal array.transpose, Literal::Array(
864 Literal::Array(Integer)
865 ).new(
866 Literal::Array(Integer).new(1, 3),
867 Literal::Array(Integer).new(2, 4),
868 )
869end
870
871test "#transpose with a nested literal array with different lengths raises an IndexError" do
872 array = Literal::Array(Literal::Array(Integer)).new(
873 Literal::Array(Integer).new(1, 2),
874 Literal::Array(Integer).new(3, 4, 5),
875 )
876
877 assert_raises(IndexError) do
878 array.transpose
879 end
880end
881
882test "#transpose with a nested regular array" do
883 array = Literal::Array(_Array(Integer)).new(
884 [1, 2],
885 [3, 4],
886 )
887
888 assert_equal array.transpose, [
889 [1, 3],
890 [2, 4],
891 ]
892end
893
894test "#zip with other literal arrays when all the lengths match" do
895 a = Literal::Array(String).new("a", "b")
896 b = Literal::Array(Integer).new(1, 2)
897 c = Literal::Array(Symbol).new(:a, :b)
898
899 assert_equal a.zip(b, c), Literal::Array(
900 Literal::Tuple(String, Integer, Symbol)
901 ).new(
902 Literal::Tuple(String, Integer, Symbol).new("a", 1, :a),
903 Literal::Tuple(String, Integer, Symbol).new("b", 2, :b),
904 )
905end
906
907test "#zip with other regular arrays" do
908 a = Literal::Array(String).new("a", "b")
909 b = [1, 2]
910 c = [:a, :b]
911
912 assert_equal a.zip(b, c), Literal::Array(
913 Literal::Tuple(String, _Any?, _Any?)
914 ).new(
915 Literal::Tuple(String, _Any?, _Any?).new("a", 1, :a),
916 Literal::Tuple(String, _Any?, _Any?).new("b", 2, :b),
917 )
918end
919
920test "#zip with other literal arrays where one of the others length is not the max length and the type is not nilable" do
921 a = Literal::Array(String).new("a", "b")
922 b = Literal::Array(Integer).new(1)
923 c = Literal::Array(Symbol).new(:a, :b)
924
925 assert_raises ArgumentError do
926 a.zip(b, c)
927 end
928end
929
930test "#zip with literal arrays where our length is not the max length and the type is not nilable" do
931 a = Literal::Array(String).new("a")
932 b = Literal::Array(Integer).new(1, 2)
933 c = Literal::Array(Symbol).new(:a, :b)
934
935 assert_raises ArgumentError do
936 a.zip(b, c)
937 end
938end
939
940test "#zip when our length is not the max length but the type is nilable" do
941 a = Literal::Array(_Nilable(String)).new("a")
942 b = [1, 2]
943 c = [:a, :b]
944
945 assert_equal a.zip(b, c), Literal::Array(
946 Literal::Tuple(_Nilable(String), _Any, _Any)
947 ).new(
948 Literal::Tuple(_Nilable(String), _Any, _Any).new("a", 1, :a),
949 Literal::Tuple(_Nilable(String), _Any, _Any).new(nil, 2, :b),
950 )
951end
952
953test "#zip with others length is not the max length but the types are nilable" do
954 a = Literal::Array(String).new("a", "b")
955 b = Literal::Array(_Nilable(Integer)).new(1)
956 c = [:a]
957
958 assert_equal a.zip(b, c), Literal::Array(
959 Literal::Tuple(String, _Nilable(Integer), _Any)
960 ).new(
961 Literal::Tuple(String, _Nilable(Integer), _Any).new("a", 1, :a),
962 Literal::Tuple(String, _Nilable(Integer), _Any).new("b", nil, nil),
963 )
964end
965
966test "#zip with a block" do
967 a = Literal::Array(String).new("a", "b")
968 b = Literal::Array(Integer).new(1, 2)
969 c = [:a, :b]
970
971 results = []
972
973 return_value = a.zip(b, c) { |it| results << it }
974
975 assert_equal results, [
976 Literal::Tuple(String, Integer, _Any).new("a", 1, :a),
977 Literal::Tuple(String, Integer, _Any).new("b", 2, :b),
978 ]
979
980 assert_equal return_value, nil
981end