github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/parser/testutil_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  )
     9  
    10  // Quick and dirty replacement for terst
    11  
    12  func tt(t *testing.T, f func()) {
    13  	defer func() {
    14  		if x := recover(); x != nil {
    15  			pcs := make([]uintptr, 16)
    16  			pcs = pcs[:runtime.Callers(1, pcs)]
    17  			frames := runtime.CallersFrames(pcs)
    18  			var file string
    19  			var line int
    20  			for {
    21  				frame, more := frames.Next()
    22  				// The line number here must match the line where f() is called (see below)
    23  				if frame.Line == 40 && filepath.Base(frame.File) == "testutil_test.go" {
    24  					break
    25  				}
    26  
    27  				if !more {
    28  					break
    29  				}
    30  				file, line = frame.File, frame.Line
    31  			}
    32  			if line > 0 {
    33  				t.Errorf("Error at %s:%d: %v", filepath.Base(file), line, x)
    34  			} else {
    35  				t.Errorf("Error at <unknown>: %v", x)
    36  			}
    37  		}
    38  	}()
    39  
    40  	f()
    41  }
    42  
    43  func is(a, b interface{}) {
    44  	as := fmt.Sprintf("%v", a)
    45  	bs := fmt.Sprintf("%v", b)
    46  	if as != bs {
    47  		panic(fmt.Errorf("%+v(%T) != %+v(%T)", a, a, b, b))
    48  	}
    49  }