Approval-based snapshot testing library for Go (mirror)

fix: fix freeze.go

+59 -22
+21 -3
README.md
··· 2 2 3 3 A [birdie](https://github.com/giacomocavalieri/birdie) and [insta](https://github.com/mitsuhiko/insta) inspired snapshot testing library for Go. 4 4 5 - TODO: screenshot 5 + ![New snapshot screen](./assets/screenshot-new.png) 6 6 7 7 ## Installation 8 8 9 - TODO: 9 + ```sh 10 + go get github.com/ptdewey/freeze 11 + ``` 10 12 11 13 ## Usage 12 14 13 - TODO: 15 + ```go 16 + package yourpackage_test 17 + 18 + func TestSomething(t *testing.T) { 19 + result := SomeFunction("foo") 20 + freeze.Snap(t, result) 21 + 22 + // To capture the calling function name use SnapFunc 23 + freeze.SnapFunc(t, SomeFunction("bar")) 24 + } 25 + ``` 26 + 27 + To review a set of snapshots, run: 28 + 29 + ```sh 30 + go run github.com/ptdewey/freeze/cmd/freeze review 31 + ``` 14 32 15 33 ## Disclaimer 16 34
+1 -1
__snapshots__/test_snap_func.snap
··· 1 1 --- 2 2 test_name: TestSnapFunc 3 3 file_path: /home/patrick/projects/freeze/freeze_test.go 4 - func_name: testHelperFunction 4 + func_name: 5 5 --- 6 6 "helper result"
+1 -1
__snapshots__/test_snap_func_another_helper.snap
··· 1 1 --- 2 2 test_name: TestSnapFuncAnotherHelper 3 3 file_path: /home/patrick/projects/freeze/freeze_test.go 4 - func_name: calculateSomething 4 + func_name: 5 5 --- 6 6 10
__snapshots__/test_snap_string.snap __snapshots__/test_snap_string.snap.new
assets/screenshot-new.png

This is a binary file and will not be displayed.

+34 -15
freeze.go
··· 35 35 36 36 func SnapFunc(t testingT, values ...any) { 37 37 t.Helper() 38 + pc, _, _, _ := runtime.Caller(1) 39 + fn := runtime.FuncForPC(pc) 40 + funcName := "unknown" 41 + if fn != nil { 42 + fullName := fn.Name() 43 + parts := len(fullName) - 1 44 + for i := len(fullName) - 1; i >= 0; i-- { 45 + if fullName[i] == '.' { 46 + parts = i + 1 47 + break 48 + } 49 + } 50 + funcName = fullName[parts:] 51 + } 38 52 content := formatValues(values...) 39 - snapWithTitle(t, t.Name(), content, t.Name()) 53 + snapWithTitle(t, funcName, content) 40 54 } 41 55 42 - func SnapFuncWithName(t testingT, funcName string, values ...any) { 43 - t.Helper() 44 - content := formatValues(values...) 45 - snapWithTitle(t, t.Name(), content, funcName) 56 + func getFunctionName() string { 57 + pc, _, _, _ := runtime.Caller(2) 58 + fn := runtime.FuncForPC(pc) 59 + if fn == nil { 60 + return "unknown" 61 + } 62 + 63 + fullName := fn.Name() 64 + parts := len(fullName) - 1 65 + for i := len(fullName) - 1; i >= 0; i-- { 66 + if fullName[i] == '.' { 67 + parts = i + 1 68 + break 69 + } 70 + } 71 + 72 + return fullName[parts:] 46 73 } 47 74 48 75 func snap(t testingT, content string) { ··· 51 78 snapWithTitle(t, testName, content) 52 79 } 53 80 54 - func snapWithTitle(t testingT, title string, content string, funcName ...string) { 81 + func snapWithTitle(t testingT, title string, content string) { 55 82 t.Helper() 56 83 57 84 _, filePath, _, _ := runtime.Caller(2) ··· 60 87 Name: title, 61 88 FilePath: filePath, 62 89 Content: content, 63 - } 64 - 65 - if len(funcName) > 0 && funcName[0] != "" { 66 - snapshot.FuncName = funcName[0] 67 90 } 68 91 69 92 accepted, err := files.ReadAccepted(title) ··· 88 111 return 89 112 } 90 113 91 - if len(funcName) > 0 && funcName[0] != "" { 92 - fmt.Println(pretty.NewSnapshotBoxFunc(snapshot)) 93 - } else { 94 - fmt.Println(pretty.NewSnapshotBox(snapshot)) 95 - } 114 + fmt.Println(pretty.NewSnapshotBox(snapshot)) 96 115 t.Error("new snapshot created - run 'freeze review' to accept") 97 116 } 98 117
+2 -2
freeze_test.go
··· 46 46 } 47 47 48 48 func TestSnapFunc(t *testing.T) { 49 - freeze.SnapFuncWithName(t, "testHelperFunction", testHelperFunction()) 49 + freeze.SnapFunc(t, testHelperFunction()) 50 50 } 51 51 52 52 func TestSnapFuncAnotherHelper(t *testing.T) { 53 - freeze.SnapFuncWithName(t, "calculateSomething", calculateSomething(5)) 53 + freeze.SnapFunc(t, calculateSomething(5)) 54 54 } 55 55 56 56 func calculateSomething(n int) int {