// 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 testtrace

import (
	
	
	
	
	
	
)

// Expectation represents the expected result of some operation.
type Expectation struct {
	failure      bool
	errorMatcher *regexp.Regexp
}

// ExpectSuccess returns an Expectation that trivially expects success.
func () *Expectation {
	return new(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 {
		return fmt.Errorf("unexpected error while reading the trace: %v", )
	}
	if .failure &&  == nil {
		return fmt.Errorf("expected error while reading the trace: want something matching %q, got none", .errorMatcher)
	}
	if .failure &&  != nil && !.errorMatcher.MatchString(.Error()) {
		return fmt.Errorf("unexpected error while reading the trace: want something matching %q, got %s", .errorMatcher, .Error())
	}
	return nil
}

// 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 = true
			if len() != 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 {
		return nil, fmt.Errorf("malformed pattern: not correctly quoted: %s: %v", , )
	}
	,  := regexp.Compile()
	if  != nil {
		return nil, fmt.Errorf("malformed pattern: not a valid regexp: %s: %v", , )
	}
	return , nil
}