golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/trace/internal/testtrace/expectation.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Code generated by "gen.bash" from internal/trace/v2; DO NOT EDIT.
     6  
     7  //go:build go1.21
     8  
     9  package testtrace
    10  
    11  import (
    12  	"bufio"
    13  	"bytes"
    14  	"fmt"
    15  	"regexp"
    16  	"strconv"
    17  	"strings"
    18  )
    19  
    20  // Expectation represents the expected result of some operation.
    21  type Expectation struct {
    22  	failure      bool
    23  	errorMatcher *regexp.Regexp
    24  }
    25  
    26  // ExpectSuccess returns an Expectation that trivially expects success.
    27  func ExpectSuccess() *Expectation {
    28  	return new(Expectation)
    29  }
    30  
    31  // Check validates whether err conforms to the expectation. Returns
    32  // an error if it does not conform.
    33  //
    34  // Conformance means that if failure is true, then err must be non-nil.
    35  // If err is non-nil, then it must match errorMatcher.
    36  func (e *Expectation) Check(err error) error {
    37  	if !e.failure && err != nil {
    38  		return fmt.Errorf("unexpected error while reading the trace: %v", err)
    39  	}
    40  	if e.failure && err == nil {
    41  		return fmt.Errorf("expected error while reading the trace: want something matching %q, got none", e.errorMatcher)
    42  	}
    43  	if e.failure && err != nil && !e.errorMatcher.MatchString(err.Error()) {
    44  		return fmt.Errorf("unexpected error while reading the trace: want something matching %q, got %s", e.errorMatcher, err.Error())
    45  	}
    46  	return nil
    47  }
    48  
    49  // ParseExpectation parses the serialized form of an Expectation.
    50  func ParseExpectation(data []byte) (*Expectation, error) {
    51  	exp := new(Expectation)
    52  	s := bufio.NewScanner(bytes.NewReader(data))
    53  	if s.Scan() {
    54  		c := strings.SplitN(s.Text(), " ", 2)
    55  		switch c[0] {
    56  		case "SUCCESS":
    57  		case "FAILURE":
    58  			exp.failure = true
    59  			if len(c) != 2 {
    60  				return exp, fmt.Errorf("bad header line for FAILURE: %q", s.Text())
    61  			}
    62  			matcher, err := parseMatcher(c[1])
    63  			if err != nil {
    64  				return exp, err
    65  			}
    66  			exp.errorMatcher = matcher
    67  		default:
    68  			return exp, fmt.Errorf("bad header line: %q", s.Text())
    69  		}
    70  		return exp, nil
    71  	}
    72  	return exp, s.Err()
    73  }
    74  
    75  func parseMatcher(quoted string) (*regexp.Regexp, error) {
    76  	pattern, err := strconv.Unquote(quoted)
    77  	if err != nil {
    78  		return nil, fmt.Errorf("malformed pattern: not correctly quoted: %s: %v", quoted, err)
    79  	}
    80  	matcher, err := regexp.Compile(pattern)
    81  	if err != nil {
    82  		return nil, fmt.Errorf("malformed pattern: not a valid regexp: %s: %v", pattern, err)
    83  	}
    84  	return matcher, nil
    85  }