From aaae97004fdb9bae18ce87cfce660a9f4507c929 Mon Sep 17 00:00:00 2001 From: Patrick Dewey <57921252+ptdewey@users.noreply.github.com> Date: Mon, 17 Nov 2025 17:18:39 -0500 Subject: [PATCH] fix: improved styling --- __snapshots__/test_map.new | 5 ++++ __snapshots__/test_snap_multiple.new | 8 ++++- format.go | 45 +++++++++++++++++----------- freeze.go | 1 + freeze_test.go | 12 ++++++-- justfile | 8 +++++ utils.go | 1 - 7 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 __snapshots__/test_map.new diff --git a/__snapshots__/test_map.new b/__snapshots__/test_map.new new file mode 100644 index 0000000..462fd96 --- /dev/null +++ b/__snapshots__/test_map.new @@ -0,0 +1,5 @@ +--- +version: 0.1.0 +test_name: TestMap +--- +map[string]interface {}{"foo":"bar"} \ No newline at end of file diff --git a/__snapshots__/test_snap_multiple.new b/__snapshots__/test_snap_multiple.new index 459de63..bae6dcb 100644 --- a/__snapshots__/test_snap_multiple.new +++ b/__snapshots__/test_snap_multiple.new @@ -4,4 +4,10 @@ test_name: TestSnapMultiple --- value1 value2 -42 \ No newline at end of file +42 +foo +bar +baz +wibble +wobble +tick \ No newline at end of file diff --git a/format.go b/format.go index b10636a..a46ea8f 100644 --- a/format.go +++ b/format.go @@ -82,38 +82,47 @@ func hasColor() bool { func NewSnapshotBox(snap *Snapshot) string { width := TerminalWidth() - separator := strings.Repeat("─", width) var sb strings.Builder - sb.WriteString("╭" + strings.Repeat("─", width) + "╮\n") - // FIX: this line is missing the '│' symbol at the end - sb.WriteString(fmt.Sprintf("│ %s\n", Blue("New Snapshot"))) - sb.WriteString("├" + separator + "┤\n") + sb.WriteString(strings.Repeat("─", width+2) + "\n") + // TODO: add file path to a new line below this + sb.WriteString(fmt.Sprintf(" %s \n", Blue("New Snapshot -- \""+snap.TestName+"\""))) lines := strings.Split(snap.Content, "\n") - for _, line := range lines { - if len(line) > width-4 { - line = line[:width-7] + "..." + numLines := len(lines) + lineNumWidth := len(strconv.Itoa(numLines)) + + topBar := strings.Repeat("─", lineNumWidth+3) + "┬" + strings.Repeat("─", width-lineNumWidth-2) + "\n" + sb.WriteString(topBar) + + for i, line := range lines { + lineNum := fmt.Sprintf("%*d", lineNumWidth, i+1) + prefix := fmt.Sprintf("%s %s", Green(lineNum), Green("+")) + + if len(line) > width-len(prefix)-4 { + line = line[:width-len(prefix)-7] + "..." } - // TODO: added code lines in snapshots should be in green with " +" next to them - // - line numbers should be left aligned with space padding - // FIX: each of these lines is missing the '│' symbol at the end - sb.WriteString(fmt.Sprintf("│ %s\n", line)) + + display := fmt.Sprintf("%s %s", prefix, Green(line)) + sb.WriteString(fmt.Sprintf(" %s\n", display)) } - sb.WriteString("╰" + strings.Repeat("─", width) + "╯\n") + bottomBar := strings.Repeat("─", lineNumWidth+3) + "┴" + strings.Repeat("─", width-lineNumWidth-2) + "\n" + sb.WriteString(bottomBar) + return sb.String() } +// TODO: this probably needs the styling overhaul from above func DiffSnapshotBox(old, new *Snapshot) string { width := TerminalWidth() diffLines := Histogram(old.Content, new.Content) var sb strings.Builder - sb.WriteString("╭" + strings.Repeat("─", width-2) + "╮\n") - sb.WriteString(fmt.Sprintf("│ %s\n", Blue("Snapshot Diff"))) - sb.WriteString("├" + strings.Repeat("─", width-2) + "┤\n") + sb.WriteString(strings.Repeat("─", width) + "\n") + sb.WriteString(fmt.Sprintf(" %s\n", Blue("Snapshot Diff"))) + sb.WriteString(strings.Repeat("─", width) + "\n") for _, dl := range diffLines { var prefix string @@ -135,10 +144,10 @@ func DiffSnapshotBox(old, new *Snapshot) string { if len(display) > width-4 { display = display[:width-7] + "..." } - sb.WriteString(fmt.Sprintf("│ %s\n", display)) + sb.WriteString(fmt.Sprintf(" %s\n", display)) } - sb.WriteString("╰" + strings.Repeat("─", width-2) + "╯\n") + sb.WriteString(strings.Repeat("─", width) + "\n") return sb.String() } diff --git a/freeze.go b/freeze.go index 3682634..648f1e6 100644 --- a/freeze.go +++ b/freeze.go @@ -83,6 +83,7 @@ func formatValues(values ...any) string { return result } +// TODO: improve this func formatValue(v any) string { if v == nil { return "" diff --git a/freeze_test.go b/freeze_test.go index 9f65109..1e0e543 100644 --- a/freeze_test.go +++ b/freeze_test.go @@ -14,7 +14,7 @@ func TestSnapString(t *testing.T) { } func TestSnapMultiple(t *testing.T) { - freeze.Snap(t, "value1", "value2", 42) + freeze.Snap(t, "value1", "value2", 42, "foo", "bar", "baz", "wibble", "wobble", "tick") } type CustomStruct struct { @@ -34,6 +34,12 @@ func TestSnapCustomType(t *testing.T) { freeze.Snap(t, cs) } +func TestMap(t *testing.T) { + freeze.Snap(t, map[string]any{ + "foo": "bar", + }) +} + func TestSerializeDeserialize(t *testing.T) { snap := &freeze.Snapshot{ Version: "1.0.0", @@ -83,7 +89,7 @@ func TestFileOperations(t *testing.T) { t.Errorf("content mismatch: %s != %s", read.Content, snap.Content) } - cleanupTestSnapshots(t) + // cleanupTestSnapshots(t) } func TestSnapshotFileName(t *testing.T) { @@ -216,6 +222,6 @@ func cleanupTestSnapshots(t *testing.T) { return } - snapshotDir := filepath.Join(cwd, "freeze_snapshots") + snapshotDir := filepath.Join(cwd, "__snapshots__") _ = os.RemoveAll(snapshotDir) } diff --git a/justfile b/justfile index d68126e..f8a1416 100644 --- a/justfile +++ b/justfile @@ -1,2 +1,10 @@ +clean-test: + @rm -rf ./__snapshots__ + @go test ./... -cover -coverprofile=cover.out + test: @go test ./... -cover -coverprofile=cover.out + + +clean: + @rm -rf ./__snapshots__ diff --git a/utils.go b/utils.go index 5010db5..2db248a 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,5 @@ package freeze -// DOCS: type testingT interface { Helper() Skip(...any) -- 2.51.2