Approval-based snapshot testing library for Go (mirror)

refactor: package layout changes and remove types from snapshots

+154 -95
+1 -1
__snapshots__/test_map.snap.new __snapshots__/test_map.snap
··· 3 3 test_name: TestMap 4 4 --- 5 5 map[string]interface{}{ 6 - string("foo"): string("bar"), 6 + "foo": "bar", 7 7 }
+2 -2
__snapshots__/test_snap_custom_type.snap.new __snapshots__/test_snap_custom_type.snap
··· 3 3 test_name: TestSnapCustomType 4 4 --- 5 5 freeze_test.CustomStruct{ 6 - Name: string("Alice"), 7 - Age: int(30), 6 + Name: "Alice", 7 + Age: 30, 8 8 }
+14
__snapshots__/test_snap_multiple.snap
··· 1 + --- 2 + version: 0.1.0 3 + test_name: TestSnapMultiple 4 + --- 5 + "value1" 6 + "value2" 7 + 42 8 + "foo" 9 + "bar" 10 + "baz" 11 + "wibble" 12 + "wobble" 13 + "tock" 14 + interface{}(nil)
-21
__snapshots__/test_snap_multiple.snap.new
··· 1 - --- 2 - version: 0.1.0 3 - test_name: TestSnapMultiple 4 - --- 5 - string("value1") 6 - 7 - string("value2") 8 - 9 - int(42) 10 - 11 - string("foo") 12 - 13 - string("bar") 14 - 15 - string("baz") 16 - 17 - string("wibble") 18 - 19 - string("wobble") 20 - 21 - string("tock")
__snapshots__/test_snap_string.snap.new __snapshots__/test_snap_string.snap
+22 -25
api.go
··· 1 1 package freeze 2 2 3 3 import ( 4 - "github.com/ptdewey/freeze/internal/diff" 5 - "github.com/ptdewey/freeze/internal/files" 6 - "github.com/ptdewey/freeze/internal/pretty" 4 + "github.com/ptdewey/freeze/internal/api" 7 5 ) 8 6 9 - type Snapshot = files.Snapshot 7 + type Snapshot = api.Snapshot 10 8 11 - type DiffLine = diff.DiffLine 9 + type DiffLine = api.DiffLine 12 10 13 11 const ( 14 - DiffShared = diff.DiffShared 15 - DiffOld = diff.DiffOld 16 - DiffNew = diff.DiffNew 12 + DiffShared = api.DiffShared 13 + DiffOld = api.DiffOld 14 + DiffNew = api.DiffNew 17 15 ) 18 16 19 17 func Deserialize(raw string) (*Snapshot, error) { 20 - return files.Deserialize(raw) 18 + return api.Deserialize(raw) 21 19 } 22 20 23 21 func SaveSnapshot(snap *Snapshot, state string) error { 24 - return files.SaveSnapshot(snap, state) 22 + return api.SaveSnapshot(snap, state) 25 23 } 26 24 27 25 func ReadSnapshot(testName string, state string) (*Snapshot, error) { 28 - return files.ReadSnapshot(testName, state) 26 + return api.ReadSnapshot(testName, state) 29 27 } 30 28 31 29 func SnapshotFileName(testName string) string { 32 - return files.SnapshotFileName(testName) 30 + return api.SnapshotFileName(testName) 33 31 } 34 32 35 33 func Histogram(old, new string) []DiffLine { 36 - return diff.Histogram(old, new) 34 + return api.Histogram(old, new) 37 35 } 38 36 39 37 func NewSnapshotBox(snap *Snapshot) string { 40 - return pretty.NewSnapshotBox(snap) 38 + return api.NewSnapshotBox(snap) 41 39 } 42 40 43 41 func DiffSnapshotBox(old, new *Snapshot) string { 44 - diffLines := convertDiffLines(diff.Histogram(old.Content, new.Content)) 45 - return pretty.DiffSnapshotBox(old, new, diffLines) 42 + return api.DiffSnapshotBox(old, new) 46 43 } 47 44 48 45 func Red(s string) string { 49 - return pretty.Red(s) 46 + return api.Red(s) 50 47 } 51 48 52 49 func Green(s string) string { 53 - return pretty.Green(s) 50 + return api.Green(s) 54 51 } 55 52 56 53 func Yellow(s string) string { 57 - return pretty.Yellow(s) 54 + return api.Yellow(s) 58 55 } 59 56 60 57 func Blue(s string) string { 61 - return pretty.Blue(s) 58 + return api.Blue(s) 62 59 } 63 60 64 61 func Gray(s string) string { 65 - return pretty.Gray(s) 62 + return api.Gray(s) 66 63 } 67 64 68 65 func Bold(s string) string { 69 - return pretty.Bold(s) 66 + return api.Bold(s) 70 67 } 71 68 72 69 func TerminalWidth() int { 73 - return pretty.TerminalWidth() 70 + return api.TerminalWidth() 74 71 } 75 72 76 73 func ClearScreen() { 77 - pretty.ClearScreen() 74 + api.ClearScreen() 78 75 } 79 76 80 77 func ClearLine() { 81 - pretty.ClearLine() 78 + api.ClearLine() 82 79 }
+20 -34
freeze.go
··· 12 12 13 13 const version = "0.1.0" 14 14 15 + // TODO: probably make this (and other things) configurable 16 + func init() { 17 + utter.Config.ElideType = true 18 + } 19 + 15 20 func SnapString(t testingT, content string) { 16 21 t.Helper() 17 22 snap(t, content) ··· 83 88 } 84 89 85 90 func formatValues(values ...any) string { 86 - if len(values) == 0 { 87 - return "" 88 - } 89 - 90 - if len(values) == 1 { 91 - return formatValue(values[0]) 92 - } 93 - 94 91 var result string 95 - for i, v := range values { 96 - if i > 0 { 97 - result += "\n" 98 - } 92 + for _, v := range values { 99 93 result += formatValue(v) 100 94 } 101 95 return result 102 96 } 103 97 104 - // TODO: improve this 105 98 func formatValue(v any) string { 106 - if v == nil { 107 - return "<nil>" 108 - } 109 - 110 - // if formattable, ok := v.(interface{ Format() string }); ok { 111 - // return formattable.Format() 99 + // if v == nil { 100 + // return "<nil>" 112 101 // } 113 - // 114 - // if stringer, ok := v.(interface{ String() string }); ok { 115 - // return stringer.String() 116 - // } 117 - // 118 - // val := reflect.ValueOf(v) 119 - // switch val.Kind() { 120 - // case reflect.String: 121 - // return v.(string) 122 - // case reflect.Struct, reflect.Slice, reflect.Array, reflect.Map: 123 - // // TODO: make this better probably (utter?) 124 - // return fmt.Sprintf("%#v", v) 125 - // default: 126 - // return fmt.Sprint(v) 127 - // } 102 + 128 103 return utter.Sdump(v) 129 104 } 130 105 ··· 139 114 func RejectAll() error { 140 115 return review.RejectAll() 141 116 } 117 + 118 + type testingT interface { 119 + Helper() 120 + Skip(...any) 121 + Skipf(string, ...any) 122 + SkipNow() 123 + Name() string 124 + Error(...any) 125 + Log(...any) 126 + Cleanup(func()) 127 + }
+1 -1
freeze_test.go
··· 14 14 } 15 15 16 16 func TestSnapMultiple(t *testing.T) { 17 - freeze.Snap(t, "value1", "value2", 42, "foo", "bar", "baz", "wibble", "wobble", "tock") 17 + freeze.Snap(t, "value1", "value2", 42, "foo", "bar", "baz", "wibble", "wobble", "tock", nil) 18 18 } 19 19 20 20 type CustomStruct struct {
+94
internal/api/api.go
··· 1 + package api 2 + 3 + import ( 4 + "github.com/ptdewey/freeze/internal/diff" 5 + "github.com/ptdewey/freeze/internal/files" 6 + "github.com/ptdewey/freeze/internal/pretty" 7 + ) 8 + 9 + type Snapshot = files.Snapshot 10 + 11 + type DiffLine = diff.DiffLine 12 + 13 + const ( 14 + DiffShared = diff.DiffShared 15 + DiffOld = diff.DiffOld 16 + DiffNew = diff.DiffNew 17 + ) 18 + 19 + func Deserialize(raw string) (*Snapshot, error) { 20 + return files.Deserialize(raw) 21 + } 22 + 23 + func SaveSnapshot(snap *Snapshot, state string) error { 24 + return files.SaveSnapshot(snap, state) 25 + } 26 + 27 + func ReadSnapshot(testName string, state string) (*Snapshot, error) { 28 + return files.ReadSnapshot(testName, state) 29 + } 30 + 31 + func SnapshotFileName(testName string) string { 32 + return files.SnapshotFileName(testName) 33 + } 34 + 35 + func Histogram(old, new string) []DiffLine { 36 + return diff.Histogram(old, new) 37 + } 38 + 39 + func NewSnapshotBox(snap *Snapshot) string { 40 + return pretty.NewSnapshotBox(snap) 41 + } 42 + 43 + func DiffSnapshotBox(old, new *Snapshot) string { 44 + diffLines := convertDiffLines(diff.Histogram(old.Content, new.Content)) 45 + return pretty.DiffSnapshotBox(old, new, diffLines) 46 + } 47 + 48 + func convertDiffLines(diffLines []diff.DiffLine) []pretty.DiffLine { 49 + result := make([]pretty.DiffLine, len(diffLines)) 50 + for i, dl := range diffLines { 51 + result[i] = pretty.DiffLine{ 52 + Number: dl.Number, 53 + Line: dl.Line, 54 + Kind: pretty.DiffKind(dl.Kind), 55 + } 56 + } 57 + return result 58 + } 59 + 60 + func Red(s string) string { 61 + return pretty.Red(s) 62 + } 63 + 64 + func Green(s string) string { 65 + return pretty.Green(s) 66 + } 67 + 68 + func Yellow(s string) string { 69 + return pretty.Yellow(s) 70 + } 71 + 72 + func Blue(s string) string { 73 + return pretty.Blue(s) 74 + } 75 + 76 + func Gray(s string) string { 77 + return pretty.Gray(s) 78 + } 79 + 80 + func Bold(s string) string { 81 + return pretty.Bold(s) 82 + } 83 + 84 + func TerminalWidth() int { 85 + return pretty.TerminalWidth() 86 + } 87 + 88 + func ClearScreen() { 89 + pretty.ClearScreen() 90 + } 91 + 92 + func ClearLine() { 93 + pretty.ClearLine() 94 + }
-11
utils.go
··· 1 1 package freeze 2 - 3 - type testingT interface { 4 - Helper() 5 - Skip(...any) 6 - Skipf(string, ...any) 7 - SkipNow() 8 - Name() string 9 - Error(...any) 10 - Log(...any) 11 - Cleanup(func()) 12 - }