Skip to content

Commit

Permalink
test utility
Browse files Browse the repository at this point in the history
  • Loading branch information
PereRohit authored Sep 18, 2022
1 parent bcee919 commit 48d5493
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ module github.com/PereRohit/util

go 1.18

require github.com/go-playground/validator/v10 v10.11.0
require (
github.com/go-playground/validator/v10 v10.11.0
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
)

require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.11.0 h1:0W+xRM511GY47Yy3bZUbJVitCNg2BOGlCyvTqsp/xIw=
github.com/go-playground/validator/v10 v10.11.0/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down
49 changes: 49 additions & 0 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package testutil

import (
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

// https://www.youtube.com/watch?v=Wncq9qtzgh0

func Diff[T any](got, want T) string {
opts := cmp.Options{
cmp.Exporter(func(p reflect.Type) bool { return true }),
cmpopts.EquateEmpty(),
}
diff := cmp.Diff(got, want, opts...)
if diff != "" {
return "\n-got +want\n" + diff
}
return ""
}

// Callers prints the stack trace of everything up till the line where Callers()
func Callers() string {
var pc [50]uintptr
n := runtime.Callers(2, pc[:]) // skip runtime.Callers + Callers
callsites := make([]string, 0, n)
frames := runtime.CallersFrames(pc[:n])
for frame, more := frames.Next(); more; frame, more = frames.Next() {
callsites = append(callsites, frame.File+":"+strconv.Itoa(frame.Line))
}
callsites = callsites[:len(callsites)-1] // skip testing.tRunner
if len(callsites) == 1 {
return ""
}
var b strings.Builder
for i := len(callsites) - 1; i >= 0; i-- {
if b.Len() > 0 {
b.WriteString(" -> ")
}
b.WriteString(filepath.Base(callsites[i]))
}
return "\n" + b.String() + ":"
}

0 comments on commit 48d5493

Please sign in to comment.