github.com/leanovate/gopter@v0.2.9/shrink_test.go (about) 1 package gopter_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/leanovate/gopter" 8 ) 9 10 type counterShrink struct { 11 n int 12 } 13 14 func (c *counterShrink) Next() (interface{}, bool) { 15 if c.n > 0 { 16 v := c.n 17 c.n-- 18 return v, true 19 } 20 return 0, false 21 } 22 23 func TestShinkAll(t *testing.T) { 24 counter := &counterShrink{n: 10} 25 shrink := gopter.Shrink(counter.Next) 26 27 all := shrink.All() 28 if !reflect.DeepEqual(all, []interface{}{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}) { 29 t.Errorf("Invalid all: %#v", all) 30 } 31 } 32 33 func TestShrinkFilter(t *testing.T) { 34 counter := &counterShrink{n: 20} 35 shrink := gopter.Shrink(counter.Next) 36 37 all := shrink.Filter(func(v interface{}) bool { 38 return v.(int)%2 == 0 39 }).All() 40 if !reflect.DeepEqual(all, []interface{}{20, 18, 16, 14, 12, 10, 8, 6, 4, 2}) { 41 t.Errorf("Invalid all: %#v", all) 42 } 43 44 counter = &counterShrink{n: 5} 45 shrink = gopter.Shrink(counter.Next) 46 47 all = shrink.Filter(nil).All() 48 if !reflect.DeepEqual(all, []interface{}{5, 4, 3, 2, 1}) { 49 t.Errorf("Invalid all: %#v", all) 50 } 51 } 52 53 func TestShrinkConcat(t *testing.T) { 54 counterShrink1 := &counterShrink{n: 5} 55 counterShrink2 := &counterShrink{n: 4} 56 shrink1 := gopter.Shrink(counterShrink1.Next) 57 shrink2 := gopter.Shrink(counterShrink2.Next) 58 59 all := gopter.ConcatShrinks(shrink1, shrink2).All() 60 if !reflect.DeepEqual(all, []interface{}{5, 4, 3, 2, 1, 4, 3, 2, 1}) { 61 t.Errorf("Invalid all: %#v", all) 62 } 63 } 64 65 func TestShrinkInterleave(t *testing.T) { 66 counterShrink1 := &counterShrink{n: 5} 67 counterShrink2 := &counterShrink{n: 7} 68 69 shrink1 := gopter.Shrink(counterShrink1.Next) 70 shrink2 := gopter.Shrink(counterShrink2.Next) 71 72 all := shrink1.Interleave(shrink2).All() 73 if !reflect.DeepEqual(all, []interface{}{5, 7, 4, 6, 3, 5, 2, 4, 1, 3, 2, 1}) { 74 t.Errorf("Invalid all: %#v", all) 75 } 76 } 77 78 func TestCombineShrinker(t *testing.T) { 79 var shrinker1Arg, shrinker2Arg interface{} 80 shrinker1 := func(v interface{}) gopter.Shrink { 81 shrinker1Arg = v 82 shrink := &counterShrink{n: 5} 83 return shrink.Next 84 } 85 shrinker2 := func(v interface{}) gopter.Shrink { 86 shrinker2Arg = v 87 shrink := &counterShrink{n: 3} 88 return shrink.Next 89 } 90 shrinker := gopter.CombineShrinker(shrinker1, shrinker2) 91 all := shrinker([]interface{}{123, 456}).All() 92 if shrinker1Arg != 123 { 93 t.Errorf("Invalid shrinker1Arg: %#v", shrinker1Arg) 94 } 95 if shrinker2Arg != 456 { 96 t.Errorf("Invalid shrinker1Arg: %#v", shrinker1Arg) 97 } 98 if !reflect.DeepEqual(all, []interface{}{ 99 []interface{}{5, 456}, 100 []interface{}{4, 456}, 101 []interface{}{3, 456}, 102 []interface{}{2, 456}, 103 []interface{}{1, 456}, 104 []interface{}{123, 3}, 105 []interface{}{123, 2}, 106 []interface{}{123, 1}, 107 }) { 108 t.Errorf("Invalid all: %#v", all) 109 } 110 } 111 112 func TestShrinkMap(t *testing.T) { 113 counter := &counterShrink{n: 10} 114 shrink := gopter.Shrink(counter.Next).Map(func(v int) int { 115 return 10 - v 116 }) 117 118 all := shrink.All() 119 if !reflect.DeepEqual(all, []interface{}{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) { 120 t.Errorf("Invalid all: %#v", all) 121 } 122 } 123 124 func TestShrinkMapNoFunc(t *testing.T) { 125 defer expectPanic(t, "Param of Map has to be a func, but is string") 126 counter := &counterShrink{n: 10} 127 gopter.Shrink(counter.Next).Map("not a function") 128 } 129 130 func TestShrinkMapTooManyParams(t *testing.T) { 131 defer expectPanic(t, "Param of Map has to be a func with one param, but is 2") 132 counter := &counterShrink{n: 10} 133 gopter.Shrink(counter.Next).Map(func(a, b string) string { 134 return "" 135 }) 136 } 137 138 func TestShrinkMapToManyReturns(t *testing.T) { 139 defer expectPanic(t, "Param of Map has to be a func with one return value, but is 2") 140 counter := &counterShrink{n: 10} 141 gopter.Shrink(counter.Next).Map(func(a string) (string, bool) { 142 return "", false 143 }) 144 } 145 146 func TestNoShrinker(t *testing.T) { 147 shrink := gopter.NoShrinker(123) 148 if shrink == nil { 149 t.Error("Shrink has to be != nil") 150 } 151 value, ok := shrink() 152 if ok || value != nil { 153 t.Errorf("Invalid shrink: %#v", value) 154 } 155 }