package testing
import (
"fmt"
"runtime"
"slices"
"strings"
"time"
)
type InternalExample struct {
Name string
F func ()
Output string
Unordered bool
}
func RunExamples (matchString func (pat , str string ) (bool , error ), examples []InternalExample ) (ok bool ) {
_, ok = runExamples (matchString , examples )
return ok
}
func runExamples(matchString func (pat , str string ) (bool , error ), examples []InternalExample ) (ran , ok bool ) {
ok = true
m := newMatcher (matchString , *match , "-test.run" , *skip )
var eg InternalExample
for _, eg = range examples {
_ , matched , _ := m .fullName (nil , eg .Name )
if !matched {
continue
}
ran = true
if !runExample (eg ) {
ok = false
}
}
return ran , ok
}
func (eg *InternalExample ) processRunResult (stdout string , timeSpent time .Duration , finished bool , recovered any ) (passed bool ) {
passed = true
dstr := fmtDuration (timeSpent )
var fail string
got := strings .TrimSpace (stdout )
want := strings .TrimSpace (eg .Output )
if runtime .GOOS == "windows" {
got = strings .ReplaceAll (got , "\r\n" , "\n" )
want = strings .ReplaceAll (want , "\r\n" , "\n" )
}
if eg .Unordered {
gotLines := slices .Sorted (strings .SplitSeq (got , "\n" ))
wantLines := slices .Sorted (strings .SplitSeq (want , "\n" ))
if !slices .Equal (gotLines , wantLines ) && recovered == nil {
fail = fmt .Sprintf ("got:\n%s\nwant (unordered):\n%s\n" , stdout , eg .Output )
}
} else {
if got != want && recovered == nil {
fail = fmt .Sprintf ("got:\n%s\nwant:\n%s\n" , got , want )
}
}
if fail != "" || !finished || recovered != nil {
fmt .Printf ("%s--- FAIL: %s (%s)\n%s" , chatty .prefix (), eg .Name , dstr , fail )
passed = false
} else if chatty .on {
fmt .Printf ("%s--- PASS: %s (%s)\n" , chatty .prefix (), eg .Name , dstr )
}
if chatty .on && chatty .json {
fmt .Printf ("%s=== NAME %s\n" , chatty .prefix (), "" )
}
if recovered != nil {
panic (recovered )
} else if !finished {
panic (errNilPanicOrGoexit )
}
return
}
The pages are generated with Golds v0.7.7-preview . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .