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

Configure Feed

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

fix: improved styling

+57 -23
+5
__snapshots__/test_map.new
··· 1 + --- 2 + version: 0.1.0 3 + test_name: TestMap 4 + --- 5 + map[string]interface {}{"foo":"bar"}
+7 -1
__snapshots__/test_snap_multiple.new
··· 4 4 --- 5 5 value1 6 6 value2 7 - 42 7 + 42 8 + foo 9 + bar 10 + baz 11 + wibble 12 + wobble 13 + tick
+27 -18
format.go
··· 82 82 83 83 func NewSnapshotBox(snap *Snapshot) string { 84 84 width := TerminalWidth() 85 - separator := strings.Repeat("─", width) 86 85 87 86 var sb strings.Builder 88 - sb.WriteString("╭" + strings.Repeat("─", width) + "╮\n") 89 - // FIX: this line is missing the '│' symbol at the end 90 - sb.WriteString(fmt.Sprintf("│ %s\n", Blue("New Snapshot"))) 91 - sb.WriteString("├" + separator + "┤\n") 87 + sb.WriteString(strings.Repeat("─", width+2) + "\n") 88 + // TODO: add file path to a new line below this 89 + sb.WriteString(fmt.Sprintf(" %s \n", Blue("New Snapshot -- \""+snap.TestName+"\""))) 92 90 93 91 lines := strings.Split(snap.Content, "\n") 94 - for _, line := range lines { 95 - if len(line) > width-4 { 96 - line = line[:width-7] + "..." 92 + numLines := len(lines) 93 + lineNumWidth := len(strconv.Itoa(numLines)) 94 + 95 + topBar := strings.Repeat("─", lineNumWidth+3) + "┬" + strings.Repeat("─", width-lineNumWidth-2) + "\n" 96 + sb.WriteString(topBar) 97 + 98 + for i, line := range lines { 99 + lineNum := fmt.Sprintf("%*d", lineNumWidth, i+1) 100 + prefix := fmt.Sprintf("%s %s", Green(lineNum), Green("+")) 101 + 102 + if len(line) > width-len(prefix)-4 { 103 + line = line[:width-len(prefix)-7] + "..." 97 104 } 98 - // TODO: added code lines in snapshots should be in green with "<line number> +" next to them 99 - // - line numbers should be left aligned with space padding 100 - // FIX: each of these lines is missing the '│' symbol at the end 101 - sb.WriteString(fmt.Sprintf("│ %s\n", line)) 105 + 106 + display := fmt.Sprintf("%s %s", prefix, Green(line)) 107 + sb.WriteString(fmt.Sprintf(" %s\n", display)) 102 108 } 103 109 104 - sb.WriteString("╰" + strings.Repeat("─", width) + "╯\n") 110 + bottomBar := strings.Repeat("─", lineNumWidth+3) + "┴" + strings.Repeat("─", width-lineNumWidth-2) + "\n" 111 + sb.WriteString(bottomBar) 112 + 105 113 return sb.String() 106 114 } 107 115 116 + // TODO: this probably needs the styling overhaul from above 108 117 func DiffSnapshotBox(old, new *Snapshot) string { 109 118 width := TerminalWidth() 110 119 111 120 diffLines := Histogram(old.Content, new.Content) 112 121 113 122 var sb strings.Builder 114 - sb.WriteString("╭" + strings.Repeat("─", width-2) + "╮\n") 115 - sb.WriteString(fmt.Sprintf("│ %s\n", Blue("Snapshot Diff"))) 116 - sb.WriteString("├" + strings.Repeat("─", width-2) + "┤\n") 123 + sb.WriteString(strings.Repeat("─", width) + "\n") 124 + sb.WriteString(fmt.Sprintf(" %s\n", Blue("Snapshot Diff"))) 125 + sb.WriteString(strings.Repeat("─", width) + "\n") 117 126 118 127 for _, dl := range diffLines { 119 128 var prefix string ··· 135 144 if len(display) > width-4 { 136 145 display = display[:width-7] + "..." 137 146 } 138 - sb.WriteString(fmt.Sprintf("│ %s\n", display)) 147 + sb.WriteString(fmt.Sprintf(" %s\n", display)) 139 148 } 140 149 141 - sb.WriteString("╰" + strings.Repeat("─", width-2) + "╯\n") 150 + sb.WriteString(strings.Repeat("─", width) + "\n") 142 151 return sb.String() 143 152 } 144 153
+1
freeze.go
··· 83 83 return result 84 84 } 85 85 86 + // TODO: improve this 86 87 func formatValue(v any) string { 87 88 if v == nil { 88 89 return "<nil>"
+9 -3
freeze_test.go
··· 14 14 } 15 15 16 16 func TestSnapMultiple(t *testing.T) { 17 - freeze.Snap(t, "value1", "value2", 42) 17 + freeze.Snap(t, "value1", "value2", 42, "foo", "bar", "baz", "wibble", "wobble", "tick") 18 18 } 19 19 20 20 type CustomStruct struct { ··· 32 32 Age: 30, 33 33 } 34 34 freeze.Snap(t, cs) 35 + } 36 + 37 + func TestMap(t *testing.T) { 38 + freeze.Snap(t, map[string]any{ 39 + "foo": "bar", 40 + }) 35 41 } 36 42 37 43 func TestSerializeDeserialize(t *testing.T) { ··· 83 89 t.Errorf("content mismatch: %s != %s", read.Content, snap.Content) 84 90 } 85 91 86 - cleanupTestSnapshots(t) 92 + // cleanupTestSnapshots(t) 87 93 } 88 94 89 95 func TestSnapshotFileName(t *testing.T) { ··· 216 222 return 217 223 } 218 224 219 - snapshotDir := filepath.Join(cwd, "freeze_snapshots") 225 + snapshotDir := filepath.Join(cwd, "__snapshots__") 220 226 _ = os.RemoveAll(snapshotDir) 221 227 }
+8
justfile
··· 1 + clean-test: 2 + @rm -rf ./__snapshots__ 3 + @go test ./... -cover -coverprofile=cover.out 4 + 1 5 test: 2 6 @go test ./... -cover -coverprofile=cover.out 7 + 8 + 9 + clean: 10 + @rm -rf ./__snapshots__
-1
utils.go
··· 1 1 package freeze 2 2 3 - // DOCS: 4 3 type testingT interface { 5 4 Helper() 6 5 Skip(...any)