@@ -311,10 +311,11 @@ A benchmark function is one named BenchmarkXXX and should have the signature,
311
311
312
312
An example function is similar to a test function but, instead of using
313
313
*testing.T to report success or failure, prints output to os.Stdout.
314
- That output is compared against the function's "Output:" comment, which
315
- must be the last comment in the function body (see example below). An
316
- example with no such comment, or with no text after "Output:" is compiled
317
- but not executed.
314
+ If the last comment in the function starts with "Output:" then the output
315
+ is compared exactly against the comment (see examples below). If the last
316
+ comment begins with "Unordered output:" then the output is compared to the
317
+ comment, however the order of the lines is ignored. An example with no such
318
+ comment, or with no text after "Output:" is compiled but not executed.
318
319
319
320
Godoc displays the body of ExampleXXX to demonstrate the use
320
321
of the function, constant, or variable XXX. An example of a method M with
@@ -330,6 +331,20 @@ Here is an example of an example:
330
331
// this example.
331
332
}
332
333
334
+ Here is another example where the ordering of the output is ignored:
335
+
336
+ func ExamplePerm() {
337
+ for _, value := range Perm(4) {
338
+ fmt.Println(value)
339
+ }
340
+
341
+ // Unordered output: 4
342
+ // 2
343
+ // 1
344
+ // 3
345
+ // 0
346
+ }
347
+
333
348
The entire test file is presented as the example when it contains a single
334
349
example function, at least one other function, type, variable, or constant
335
350
declaration, and no test or benchmark functions.
@@ -1323,9 +1338,10 @@ func (t *testFuncs) Tested() string {
1323
1338
}
1324
1339
1325
1340
type testFunc struct {
1326
- Package string // imported package name (_test or _xtest)
1327
- Name string // function name
1328
- Output string // output, for examples
1341
+ Package string // imported package name (_test or _xtest)
1342
+ Name string // function name
1343
+ Output string // output, for examples
1344
+ Unordered bool // output is allowed to be unordered.
1329
1345
}
1330
1346
1331
1347
var testFileSet = token .NewFileSet ()
@@ -1349,21 +1365,21 @@ func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
1349
1365
if t .TestMain != nil {
1350
1366
return errors .New ("multiple definitions of TestMain" )
1351
1367
}
1352
- t .TestMain = & testFunc {pkg , name , "" }
1368
+ t .TestMain = & testFunc {pkg , name , "" , false }
1353
1369
* doImport , * seen = true , true
1354
1370
case isTest (name , "Test" ):
1355
1371
err := checkTestFunc (n , "T" )
1356
1372
if err != nil {
1357
1373
return err
1358
1374
}
1359
- t .Tests = append (t .Tests , testFunc {pkg , name , "" })
1375
+ t .Tests = append (t .Tests , testFunc {pkg , name , "" , false })
1360
1376
* doImport , * seen = true , true
1361
1377
case isTest (name , "Benchmark" ):
1362
1378
err := checkTestFunc (n , "B" )
1363
1379
if err != nil {
1364
1380
return err
1365
1381
}
1366
- t .Benchmarks = append (t .Benchmarks , testFunc {pkg , name , "" })
1382
+ t .Benchmarks = append (t .Benchmarks , testFunc {pkg , name , "" , false })
1367
1383
* doImport , * seen = true , true
1368
1384
}
1369
1385
}
@@ -1375,7 +1391,7 @@ func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
1375
1391
// Don't run examples with no output.
1376
1392
continue
1377
1393
}
1378
- t .Examples = append (t .Examples , testFunc {pkg , "Example" + e .Name , e .Output })
1394
+ t .Examples = append (t .Examples , testFunc {pkg , "Example" + e .Name , e .Output , e . Unordered })
1379
1395
* seen = true
1380
1396
}
1381
1397
return nil
@@ -1435,7 +1451,7 @@ var benchmarks = []testing.InternalBenchmark{
1435
1451
1436
1452
var examples = []testing.InternalExample{
1437
1453
{{range .Examples}}
1438
- {"{{.Name}}", {{.Package}}.{{.Name}}, {{.Output | printf "%q"}}},
1454
+ {"{{.Name}}", {{.Package}}.{{.Name}}, {{.Output | printf "%q"}}, {{.Unordered}} },
1439
1455
{{end}}
1440
1456
}
1441
1457
0 commit comments