| 1 | module main |
| 2 | |
| 3 | struct Pair[A, B] { |
| 4 | first A |
| 5 | second B |
| 6 | } |
| 7 | |
| 8 | fn (p Pair[A, B]) str() string { |
| 9 | return 'Pair(' + p.first.str() + ', ' + p.second.str() + ')' |
| 10 | } |
| 11 | |
| 12 | struct Gen[T] { |
| 13 | run fn () !T = unsafe { nil } |
| 14 | } |
| 15 | |
| 16 | type SmartValue = int | string |
| 17 | |
| 18 | fn (g Gen[T]) draw() !T { |
| 19 | v := g.run()! |
| 20 | return v |
| 21 | } |
| 22 | |
| 23 | fn int_gen() Gen[int] { |
| 24 | return Gen[int]{ |
| 25 | run: fn () !int { |
| 26 | return 1 |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn combine[A, B](a Gen[A], b Gen[B]) Gen[Pair[A, B]] { |
| 32 | return Gen[Pair[A, B]]{ |
| 33 | run: fn [a, b] [A, B]() !Pair[A, B] { |
| 34 | first := a.draw()! |
| 35 | second := b.draw()! |
| 36 | return Pair[A, B]{ |
| 37 | first: first |
| 38 | second: second |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn pair_gen() Gen[Pair[int, int]] { |
| 45 | return combine[int, int](int_gen(), int_gen()) |
| 46 | } |
| 47 | |
| 48 | fn smart_value_gen() Gen[SmartValue] { |
| 49 | return Gen[SmartValue]{ |
| 50 | run: fn () !SmartValue { |
| 51 | return SmartValue(7) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn delete_pass_simple[T](g Gen[T]) []string { |
| 57 | mut trail := []string{} |
| 58 | if v := g.draw() { |
| 59 | trail << '${v}' |
| 60 | } |
| 61 | return trail |
| 62 | } |
| 63 | |
| 64 | fn delete_pass_int_width[T](g Gen[T]) []string { |
| 65 | mut trail := []string{} |
| 66 | if v := g.draw() { |
| 67 | trail << '${v:4d}' |
| 68 | } |
| 69 | return trail |
| 70 | } |
| 71 | |
| 72 | fn delete_pass_string_width[T](g Gen[T]) []string { |
| 73 | mut trail := []string{} |
| 74 | if v := g.draw() { |
| 75 | trail << '${v:12s}' |
| 76 | } |
| 77 | return trail |
| 78 | } |
| 79 | |
| 80 | fn delete_pass_smartcast_width[T](g Gen[T]) []string { |
| 81 | mut trail := []string{} |
| 82 | if v := g.draw() { |
| 83 | if v is int { |
| 84 | trail << '${v:4d}' |
| 85 | } |
| 86 | } |
| 87 | return trail |
| 88 | } |
| 89 | |
| 90 | fn test_generic_if_guard_simple_interpolation_int_then_pair() { |
| 91 | assert delete_pass_simple[int](int_gen()) == ['1'] |
| 92 | assert delete_pass_simple[Pair[int, int]](pair_gen()) == ['Pair(1, 1)'] |
| 93 | } |
| 94 | |
| 95 | fn test_generic_if_guard_simple_interpolation_pair_then_int() { |
| 96 | assert delete_pass_simple[Pair[int, int]](pair_gen()) == ['Pair(1, 1)'] |
| 97 | assert delete_pass_simple[int](int_gen()) == ['1'] |
| 98 | } |
| 99 | |
| 100 | fn test_generic_if_guard_full_interpolation_int_then_pair() { |
| 101 | assert delete_pass_int_width[int](int_gen()) == [' 1'] |
| 102 | assert delete_pass_string_width[Pair[int, int]](pair_gen()) == [' Pair(1, 1)'] |
| 103 | } |
| 104 | |
| 105 | fn test_generic_if_guard_full_interpolation_pair_then_int() { |
| 106 | assert delete_pass_string_width[Pair[int, int]](pair_gen()) == [' Pair(1, 1)'] |
| 107 | assert delete_pass_int_width[int](int_gen()) == [' 1'] |
| 108 | } |
| 109 | |
| 110 | fn test_generic_if_guard_smartcast_interpolation_keeps_smartcast_type() { |
| 111 | assert delete_pass_smartcast_width[SmartValue](smart_value_gen()) == [' 7'] |
| 112 | } |
| 113 | |