Approval-based snapshot testing library for Go (mirror)
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

docs: update readme

+29 -65
+23 -7
README.md
··· 4 4 5 5 ![New snapshot screen](./assets/screenshot-new.png "New snapshot view") 6 6 7 - ![Snapshot review CLI](./assets/screenshots-diff-cli.png "Snapshot diff view (CLI)") 7 + ![Snapshot review CLI](./assets/screenshot-diff-cli.png "Snapshot diff view (CLI)") 8 8 9 9 ## Installation 10 10 ··· 15 15 ## Usage 16 16 17 17 ```go 18 - package yourpackage_test 18 + package package_test 19 19 20 20 func TestSomething(t *testing.T) { 21 21 result := SomeFunction("foo") 22 22 freeze.Snap(t, result) 23 - 24 - // To capture the calling function name use SnapFunc 25 - freeze.SnapFunc(t, SomeFunction("bar")) 26 23 } 27 24 ``` 28 25 ··· 32 29 go run github.com/ptdewey/freeze/cmd/freeze review 33 30 ``` 34 31 35 - <!-- TODO: add example of `freeze.Review()` in go code --> 32 + Freeze can also be used programmatically: 33 + 34 + ```go 35 + // Example: tools/freeze/main.go 36 + package main 36 37 37 - Freeze also includes (in a separate Go module) a [Bubbletea](https://github.com/charmbracelet/bubbletea) TUI in [cmd/tui/main.go](./cmd/tui/main.go). (The TUI is shipped in a separate module to make the added dependencies optional) 38 + import "github.com/ptdewey/freeze" 39 + 40 + func main() { 41 + // This will start the CLI review tool 42 + freeze.Review() 43 + } 44 + ``` 45 + 46 + Which can then be run with: 47 + 48 + ```sh 49 + go run tools/freeze/main.go 50 + ``` 51 + 52 + Freeze also includes (in a separate Go module) a [Bubbletea](https://github.com/charmbracelet/bubbletea) TUI in [cmd/tui/main.go](./cmd/tui/main.go). 53 + (The TUI is shipped in a separate module to make the added dependencies optional) 38 54 39 55 ### TUI Usage 40 56
-8
__snapshots__/test_snap_func.snap
··· 1 - --- 2 - title: TestSnapFunc 3 - test_name: TestSnapFunc 4 - file_path: 5 - func_name: 6 - version: 0.1.0 7 - --- 8 - "helper result"
-8
__snapshots__/test_snap_func_another_helper.snap
··· 1 - --- 2 - title: TestSnapFuncAnotherHelper 3 - test_name: TestSnapFuncAnotherHelper 4 - file_path: 5 - func_name: 6 - version: 0.1.0 7 - --- 8 - 10
-22
freeze.go
··· 2 2 3 3 import ( 4 4 "fmt" 5 - "runtime" 6 5 7 6 "github.com/kortschak/utter" 8 7 "github.com/ptdewey/freeze/internal/diff" ··· 27 26 t.Helper() 28 27 content := formatValues(values...) 29 28 snap(t, title, content) 30 - } 31 - 32 - func SnapFunc(t testingT, values ...any) { 33 - t.Helper() 34 - pc, _, _, _ := runtime.Caller(1) 35 - fn := runtime.FuncForPC(pc) 36 - funcName := "unknown" 37 - if fn != nil { 38 - fullName := fn.Name() 39 - parts := len(fullName) - 1 40 - for i := len(fullName) - 1; i >= 0; i-- { 41 - if fullName[i] == '.' { 42 - parts = i + 1 43 - break 44 - } 45 - } 46 - funcName = fullName[parts:] 47 - } 48 - testName := t.Name() 49 - content := formatValues(values...) 50 - snapWithTitle(t, funcName, testName, content) 51 29 } 52 30 53 31 func snap(t testingT, title string, content string) {
-16
freeze_test.go
··· 42 42 }) 43 43 } 44 44 45 - func testHelperFunction() string { 46 - return "helper result" 47 - } 48 - 49 - func TestSnapFunc(t *testing.T) { 50 - freeze.SnapFunc(t, testHelperFunction()) 51 - } 52 - 53 - func TestSnapFuncAnotherHelper(t *testing.T) { 54 - freeze.SnapFunc(t, calculateSomething(5)) 55 - } 56 - 57 - func calculateSomething(n int) int { 58 - return n * 2 59 - } 60 - 61 45 func TestSerializeDeserialize(t *testing.T) { 62 46 snap := &freeze.Snapshot{ 63 47 Title: "My Test Title",
+6 -4
internal/pretty/boxes.go
··· 28 28 snapshotFileName := files.SnapshotFileName(snap.Name) + ".snap.new" 29 29 30 30 var sb strings.Builder 31 - sb.WriteString("─── " + "New Snapshot " + strings.Repeat("─", width-15) + "\n") 32 - sb.WriteString(fmt.Sprintf(" file: %s\n\n", Gray(snapshotFileName))) 31 + sb.WriteString("─── " + "New Snapshot " + strings.Repeat("─", width-15) + "\n\n") 33 32 34 33 if snap.Title != "" { 35 - sb.WriteString(fmt.Sprintf(" title: %s\n", Blue("\""+snap.Title+"\""))) 34 + sb.WriteString(fmt.Sprintf(" title: %s\n", Blue(snap.Title))) 36 35 } 37 36 if snap.Name != "" { 38 - sb.WriteString(fmt.Sprintf(" test: %s\n", Blue("\""+snap.Name+"\""))) 37 + sb.WriteString(fmt.Sprintf(" test: %s\n", Blue(snap.Name))) 38 + } 39 + if snapshotFileName != "" { 40 + sb.WriteString(fmt.Sprintf(" file: %s\n", Gray(snapshotFileName))) 39 41 } 40 42 sb.WriteString("\n") 41 43