Commit 68d2bbec authored by Kyle Lemons's avatar Kyle Lemons

Improve diffable output for lists

parent fffda665
...@@ -116,21 +116,40 @@ func (l list) WriteTo(w *bytes.Buffer, indent string, cfg *Config) { ...@@ -116,21 +116,40 @@ func (l list) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
} }
} }
inner := indent + " "
w.WriteByte('[') w.WriteByte('[')
for i, v := range l {
if !cfg.Compact && (i > 0 || cfg.Diffable) { switch {
w.WriteByte('\n') case cfg.Compact:
// All on one line:
for i, v := range l {
if i > 0 {
w.WriteByte(',')
}
v.WriteTo(w, indent, cfg)
}
case cfg.Diffable:
w.WriteByte('\n')
inner := indent + " "
// Each value gets its own line:
for _, v := range l {
w.WriteString(inner) w.WriteString(inner)
v.WriteTo(w, inner, cfg)
w.WriteString(",\n")
} }
v.WriteTo(w, inner, cfg) if len(l) > 0 {
if i+1 < len(l) || cfg.Diffable { w.WriteString(indent)
w.WriteByte(',') }
default:
inner := indent + " "
// First and last line shared with bracket:
for i, v := range l {
if i > 0 {
w.WriteString(",\n")
w.WriteString(inner)
}
v.WriteTo(w, inner, cfg)
} }
} }
if !cfg.Compact && cfg.Diffable && len(l) > 0 {
w.WriteByte('\n')
w.WriteString(indent)
}
w.WriteByte(']') w.WriteByte(']')
} }
...@@ -16,27 +16,30 @@ package pretty ...@@ -16,27 +16,30 @@ package pretty
import ( import (
"bytes" "bytes"
"strings"
"testing" "testing"
) )
func TestWriteTo(t *testing.T) { func TestWriteTo(t *testing.T) {
tests := []struct { tests := []struct {
desc string desc string
node node node node
// All strings have a leading newline trimmed before comparison:
normal string normal string
extended string diffable string
}{ }{
{ {
desc: "string", desc: "string",
node: stringVal("zaphod"), node: stringVal("zaphod"),
normal: `"zaphod"`, normal: `"zaphod"`,
extended: `"zaphod"`, diffable: `"zaphod"`,
}, },
{ {
desc: "raw", desc: "raw",
node: rawVal("42"), node: rawVal("42"),
normal: `42`, normal: `42`,
extended: `42`, diffable: `42`,
}, },
{ {
desc: "keyvals", desc: "keyvals",
...@@ -44,12 +47,23 @@ func TestWriteTo(t *testing.T) { ...@@ -44,12 +47,23 @@ func TestWriteTo(t *testing.T) {
{"name", stringVal("zaphod")}, {"name", stringVal("zaphod")},
{"age", rawVal("42")}, {"age", rawVal("42")},
}, },
normal: `{name: "zaphod", normal: `
{name: "zaphod",
age: 42}`, age: 42}`,
extended: `{ diffable: `
{
name: "zaphod", name: "zaphod",
age: 42, age: 42,
}`, }`,
},
{
desc: "empty list",
node: list{},
normal: `
[]`,
diffable: `
[
]`,
}, },
{ {
desc: "list", desc: "list",
...@@ -57,12 +71,23 @@ func TestWriteTo(t *testing.T) { ...@@ -57,12 +71,23 @@ func TestWriteTo(t *testing.T) {
stringVal("zaphod"), stringVal("zaphod"),
rawVal("42"), rawVal("42"),
}, },
normal: `["zaphod", normal: `
["zaphod",
42]`, 42]`,
extended: `[ diffable: `
[
"zaphod", "zaphod",
42, 42,
]`, ]`,
},
{
desc: "empty keyvals",
node: keyvals{},
normal: `
{}`,
diffable: `
{
}`,
}, },
{ {
desc: "nested", desc: "nested",
...@@ -81,7 +106,8 @@ func TestWriteTo(t *testing.T) { ...@@ -81,7 +106,8 @@ func TestWriteTo(t *testing.T) {
}, },
keyvals{}, keyvals{},
}, },
normal: `["first", normal: `
["first",
[1, [1,
2, 2,
3], 3],
...@@ -90,7 +116,8 @@ func TestWriteTo(t *testing.T) { ...@@ -90,7 +116,8 @@ func TestWriteTo(t *testing.T) {
zaphod: {occupation: "president of the galaxy", zaphod: {occupation: "president of the galaxy",
features: "two heads"}}, features: "two heads"}},
{}]`, {}]`,
extended: `[ diffable: `
[
"first", "first",
[ [
1, 1,
...@@ -114,6 +141,10 @@ func TestWriteTo(t *testing.T) { ...@@ -114,6 +141,10 @@ func TestWriteTo(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
// For readability, we have a newline that won't be there in the output
test.normal = strings.TrimPrefix(test.normal, "\n")
test.diffable = strings.TrimPrefix(test.diffable, "\n")
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
test.node.WriteTo(buf, "", &Config{}) test.node.WriteTo(buf, "", &Config{})
if got, want := buf.String(), test.normal; got != want { if got, want := buf.String(), test.normal; got != want {
...@@ -121,8 +152,8 @@ func TestWriteTo(t *testing.T) { ...@@ -121,8 +152,8 @@ func TestWriteTo(t *testing.T) {
} }
buf.Reset() buf.Reset()
test.node.WriteTo(buf, "", &Config{Diffable: true}) test.node.WriteTo(buf, "", &Config{Diffable: true})
if got, want := buf.String(), test.extended; got != want { if got, want := buf.String(), test.diffable; got != want {
t.Errorf("%s: extended rendendered incorrectly\ngot:\n%s\nwant:\n%s", test.desc, got, want) t.Errorf("%s: diffable rendendered incorrectly\ngot:\n%s\nwant:\n%s", test.desc, got, want)
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment