// Copyright 2023 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 testtraceimport ()// Expectation represents the expected result of some operation.typeExpectationstruct { failure bool errorMatcher *regexp.Regexp}// ExpectSuccess returns an Expectation that trivially expects success.func () *Expectation {returnnew(Expectation)}// Check validates whether err conforms to the expectation. Returns// an error if it does not conform.//// Conformance means that if failure is true, then err must be non-nil.// If err is non-nil, then it must match errorMatcher.func ( *Expectation) ( error) error {if !.failure && != nil {returnfmt.Errorf("unexpected error while reading the trace: %v", ) }if .failure && == nil {returnfmt.Errorf("expected error while reading the trace: want something matching %q, got none", .errorMatcher) }if .failure && != nil && !.errorMatcher.MatchString(.Error()) {returnfmt.Errorf("unexpected error while reading the trace: want something matching %q, got %s", .errorMatcher, .Error()) }returnnil}// ParseExpectation parses the serialized form of an Expectation.func ( []byte) (*Expectation, error) { := new(Expectation) := bufio.NewScanner(bytes.NewReader())if .Scan() { := strings.SplitN(.Text(), " ", 2)switch [0] {case"SUCCESS":case"FAILURE": .failure = trueiflen() != 2 {return , fmt.Errorf("bad header line for FAILURE: %q", .Text()) } , := parseMatcher([1])if != nil {return , } .errorMatcher = default:return , fmt.Errorf("bad header line: %q", .Text()) }return , nil }return , .Err()}func parseMatcher( string) (*regexp.Regexp, error) { , := strconv.Unquote()if != nil {returnnil, fmt.Errorf("malformed pattern: not correctly quoted: %s: %v", , ) } , := regexp.Compile()if != nil {returnnil, fmt.Errorf("malformed pattern: not a valid regexp: %s: %v", , ) }return , nil}
The pages are generated with Goldsv0.7.0-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.