github.com/leanovate/gopter@v0.2.9/prop_test.go (about) 1 package gopter 2 3 import ( 4 "strings" 5 "sync/atomic" 6 "testing" 7 ) 8 9 func TestSaveProp(t *testing.T) { 10 prop := SaveProp(func(*GenParameters) *PropResult { 11 panic("Ouchy") 12 }) 13 14 parameters := DefaultTestParameters() 15 result := prop.Check(parameters) 16 17 if result.Status != TestError || result.Error == nil || 18 !strings.HasPrefix(result.Error.Error(), "Check paniced: Ouchy") { 19 t.Errorf("Invalid result: %#v", result) 20 } 21 } 22 23 func TestPropUndecided(t *testing.T) { 24 var called int64 25 prop := Prop(func(genParams *GenParameters) *PropResult { 26 atomic.AddInt64(&called, 1) 27 28 return &PropResult{ 29 Status: PropUndecided, 30 } 31 }) 32 33 parameters := DefaultTestParameters() 34 result := prop.Check(parameters) 35 36 if result.Status != TestExhausted || result.Succeeded != 0 { 37 t.Errorf("Invalid result: %#v", result) 38 } 39 if called != int64(parameters.MinSuccessfulTests)+1 { 40 t.Errorf("Invalid number of calls: %d", called) 41 } 42 } 43 44 func TestPropMaxDiscardRatio(t *testing.T) { 45 var called int64 46 prop := Prop(func(genParams *GenParameters) *PropResult { 47 atomic.AddInt64(&called, 1) 48 49 if genParams.MaxSize > 21 { 50 return &PropResult{ 51 Status: PropTrue, 52 } 53 } 54 return &PropResult{ 55 Status: PropUndecided, 56 } 57 }) 58 59 parameters := DefaultTestParameters() 60 parameters.MaxDiscardRatio = 0.2 61 result := prop.Check(parameters) 62 63 if result.Status != TestExhausted || result.Succeeded != 100 { 64 t.Errorf("Invalid result: %#v", result) 65 } 66 if called != int64(parameters.MinSuccessfulTests)+22 { 67 t.Errorf("Invalid number of calls: %d", called) 68 } 69 } 70 71 func TestPropPassed(t *testing.T) { 72 var called int64 73 prop := Prop(func(genParams *GenParameters) *PropResult { 74 atomic.AddInt64(&called, 1) 75 76 return &PropResult{ 77 Status: PropTrue, 78 } 79 }) 80 81 parameters := DefaultTestParameters() 82 result := prop.Check(parameters) 83 84 if result.Status != TestPassed || result.Succeeded != parameters.MinSuccessfulTests { 85 t.Errorf("Invalid result: %#v", result) 86 } 87 if called != int64(parameters.MinSuccessfulTests) { 88 t.Errorf("Invalid number of calls: %d", called) 89 } 90 } 91 92 func TestPropProof(t *testing.T) { 93 var called int64 94 prop := Prop(func(genParams *GenParameters) *PropResult { 95 atomic.AddInt64(&called, 1) 96 97 return &PropResult{ 98 Status: PropProof, 99 } 100 }) 101 102 parameters := DefaultTestParameters() 103 result := prop.Check(parameters) 104 105 if result.Status != TestProved || result.Succeeded != 1 { 106 t.Errorf("Invalid result: %#v", result) 107 } 108 if called != 1 { 109 t.Errorf("Invalid number of calls: %d", called) 110 } 111 } 112 113 func TestPropFalse(t *testing.T) { 114 var called int64 115 prop := Prop(func(genParams *GenParameters) *PropResult { 116 atomic.AddInt64(&called, 1) 117 118 return &PropResult{ 119 Status: PropFalse, 120 } 121 }) 122 123 parameters := DefaultTestParameters() 124 result := prop.Check(parameters) 125 126 if result.Status != TestFailed || result.Succeeded != 0 { 127 t.Errorf("Invalid result: %#v", result) 128 } 129 if called != 1 { 130 t.Errorf("Invalid number of calls: %d", called) 131 } 132 } 133 134 func TestPropError(t *testing.T) { 135 var called int64 136 prop := Prop(func(genParams *GenParameters) *PropResult { 137 atomic.AddInt64(&called, 1) 138 139 return &PropResult{ 140 Status: PropError, 141 } 142 }) 143 144 parameters := DefaultTestParameters() 145 result := prop.Check(parameters) 146 147 if result.Status != TestError || result.Succeeded != 0 { 148 t.Errorf("Invalid result: %#v", result) 149 } 150 if called != 1 { 151 t.Errorf("Invalid number of calls: %d", called) 152 } 153 } 154 155 func TestPropPassedMulti(t *testing.T) { 156 var called int64 157 prop := Prop(func(genParams *GenParameters) *PropResult { 158 atomic.AddInt64(&called, 1) 159 160 return &PropResult{ 161 Status: PropTrue, 162 } 163 }) 164 165 parameters := DefaultTestParameters() 166 parameters.Workers = 10 167 result := prop.Check(parameters) 168 169 if result.Status != TestPassed || result.Succeeded != parameters.MinSuccessfulTests { 170 t.Errorf("Invalid result: %#v", result) 171 } 172 if called != int64(parameters.MinSuccessfulTests) { 173 t.Errorf("Invalid number of calls: %d", called) 174 } 175 }