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
|
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package generator
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"golang.org/x/tools/internal/testenv"
)
func mkGenState() *genstate {
return &genstate{
GenConfig: GenConfig{
Tag: "gen",
OutDir: "/tmp",
NumTestPackages: 1,
NumTestFunctions: 10,
},
ipref: "foo/",
derefFuncs: make(map[string]string),
assignFuncs: make(map[string]string),
allocFuncs: make(map[string]string),
globVars: make(map[string]string),
}
}
func TestBasic(t *testing.T) {
checkTunables(tunables)
s := mkGenState()
for i := 0; i < 1000; i++ {
s.wr = NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic)
fp := s.GenFunc(i, i)
var buf bytes.Buffer
var b *bytes.Buffer = &buf
wr := NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic)
s.wr = wr
s.emitCaller(fp, b, i)
s.wr = NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic)
s.emitChecker(fp, b, i, true)
wr.Check(s.wr)
}
if s.errs != 0 {
t.Errorf("%d errors during Generate", s.errs)
}
}
func TestMoreComplicated(t *testing.T) {
saveit := tunables
defer func() { tunables = saveit }()
checkTunables(tunables)
s := mkGenState()
for i := 0; i < 10000; i++ {
s.wr = NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic)
fp := s.GenFunc(i, i)
var buf bytes.Buffer
var b *bytes.Buffer = &buf
wr := NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic)
s.wr = wr
s.emitCaller(fp, b, i)
verb(1, "finished iter %d caller", i)
s.wr = NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic)
s.emitChecker(fp, b, i, true)
verb(1, "finished iter %d checker", i)
wr.Check(s.wr)
if s.errs != 0 {
t.Errorf("%d errors during Generate iter %d", s.errs, i)
}
}
}
func TestIsBuildable(t *testing.T) {
testenv.NeedsTool(t, "go")
if runtime.GOOS == "android" {
t.Skipf("the dependencies are not available on android")
}
td := t.TempDir()
verb(1, "generating into temp dir %s", td)
checkTunables(tunables)
pack := filepath.Base(td)
s := GenConfig{
Tag: "x",
OutDir: td,
PkgPath: pack,
NumTestFunctions: 10,
NumTestPackages: 10,
MaxFail: 10,
RandCtl: RandCtlChecks | RandCtlPanic,
}
errs := Generate(s)
if errs != 0 {
t.Errorf("%d errors during Generate", errs)
}
verb(1, "building %s\n", td)
cmd := exec.Command("go", "run", ".")
cmd.Dir = td
coutput, cerr := cmd.CombinedOutput()
if cerr != nil {
t.Errorf("go build command failed: %s\n", string(coutput))
}
verb(1, "output is: %s\n", string(coutput))
}
// TestExhaustive does a series of code genreation runs, starting with
// (relatively) simple code and then getting progressively more
// complex (more params, deeper structs, turning on additional
// features such as address-taken vars and reflect testing). The
// intent here is mainly to insure that the tester still works if you
// turn things on and off, e.g. that each feature is separately
// controllable and not linked to other things.
func TestExhaustive(t *testing.T) {
testenv.NeedsTool(t, "go")
if runtime.GOOS == "android" {
t.Skipf("the dependencies are not available on android")
}
if testing.Short() {
t.Skip("skipping test in short mode.")
}
td := t.TempDir()
verb(1, "generating into temp dir %s", td)
scenarios := []struct {
name string
adjuster func()
}{
{
"minimal",
func() {
tunables.nParmRange = 3
tunables.nReturnRange = 3
tunables.structDepth = 1
tunables.recurPerc = 0
tunables.methodPerc = 0
tunables.doReflectCall = false
tunables.doDefer = false
tunables.takeAddress = false
tunables.doFuncCallValues = false
tunables.doSkipCompare = false
checkTunables(tunables)
},
},
{
"moreparms",
func() {
tunables.nParmRange = 15
tunables.nReturnRange = 7
tunables.structDepth = 3
checkTunables(tunables)
},
},
{
"addrecur",
func() {
tunables.recurPerc = 20
checkTunables(tunables)
},
},
{
"addmethod",
func() {
tunables.methodPerc = 25
tunables.pointerMethodCallPerc = 30
checkTunables(tunables)
},
},
{
"addtakeaddr",
func() {
tunables.takeAddress = true
tunables.takenFraction = 20
checkTunables(tunables)
},
},
{
"addreflect",
func() {
tunables.doReflectCall = true
checkTunables(tunables)
},
},
{
"adddefer",
func() {
tunables.doDefer = true
checkTunables(tunables)
},
},
{
"addfuncval",
func() {
tunables.doFuncCallValues = true
checkTunables(tunables)
},
},
{
"addfuncval",
func() {
tunables.doSkipCompare = true
checkTunables(tunables)
},
},
}
// Loop over scenarios and make sure each one works properly.
for i, s := range scenarios {
t.Logf("running %s\n", s.name)
s.adjuster()
os.RemoveAll(td)
pack := filepath.Base(td)
c := GenConfig{
Tag: "x",
OutDir: td,
PkgPath: pack,
NumTestFunctions: 10,
NumTestPackages: 10,
Seed: int64(i + 9),
MaxFail: 10,
RandCtl: RandCtlChecks | RandCtlPanic,
}
errs := Generate(c)
if errs != 0 {
t.Errorf("%d errors during scenarios %q Generate", errs, s.name)
}
cmd := exec.Command("go", "run", ".")
cmd.Dir = td
coutput, cerr := cmd.CombinedOutput()
if cerr != nil {
t.Fatalf("run failed for scenario %q: %s\n", s.name, string(coutput))
}
verb(1, "output is: %s\n", string(coutput))
}
}
func TestEmitBadBuildFailure(t *testing.T) {
testenv.NeedsTool(t, "go")
if runtime.GOOS == "android" {
t.Skipf("the dependencies are not available on android")
}
td := t.TempDir()
verb(1, "generating into temp dir %s", td)
checkTunables(tunables)
pack := filepath.Base(td)
s := GenConfig{
Tag: "x",
OutDir: td,
PkgPath: pack,
NumTestFunctions: 10,
NumTestPackages: 10,
MaxFail: 10,
RandCtl: RandCtlChecks | RandCtlPanic,
EmitBad: 1,
}
errs := Generate(s)
if errs != 0 {
t.Errorf("%d errors during Generate", errs)
}
cmd := exec.Command("go", "build", ".")
cmd.Dir = td
coutput, cerr := cmd.CombinedOutput()
if cerr == nil {
t.Errorf("go build command passed, expected failure. output: %s\n", string(coutput))
}
}
func TestEmitBadRunFailure(t *testing.T) {
testenv.NeedsTool(t, "go")
if runtime.GOOS == "android" {
t.Skipf("the dependencies are not available on android")
}
td := t.TempDir()
verb(1, "generating into temp dir %s", td)
checkTunables(tunables)
pack := filepath.Base(td)
s := GenConfig{
Tag: "x",
OutDir: td,
PkgPath: pack,
NumTestFunctions: 10,
NumTestPackages: 10,
MaxFail: 10,
RandCtl: RandCtlChecks | RandCtlPanic,
EmitBad: 2,
}
errs := Generate(s)
if errs != 0 {
t.Errorf("%d errors during Generate", errs)
}
// build
cmd := exec.Command("go", "build", ".")
cmd.Dir = td
coutput, cerr := cmd.CombinedOutput()
if cerr != nil {
t.Fatalf("build failed: %s\n", string(coutput))
}
// run
cmd = exec.Command("./" + pack)
cmd.Dir = td
coutput, cerr = cmd.CombinedOutput()
if cerr == nil {
t.Fatalf("run passed, expected failure -- run output: %s", string(coutput))
}
}
|