testutil_test.go 953 B

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