notation/notation_test.go

154 lines
2.4 KiB
Go
Raw Permalink Normal View History

2020-11-23 00:46:14 +01:00
package notation
import (
"bytes"
"testing"
)
func TestPrint(t *testing.T) {
for _, test := range []struct {
p func(...interface{}) (int, error)
o interface{}
e string
}{{
p: Print,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: `{foo: 42}`,
}, {
p: Printw,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: `{
foo: 42,
}`,
}, {
p: Printt,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: `struct{foo int}{foo: 42}`,
}, {
p: Printwt,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: `struct{
foo int
}{
foo: 42,
}`,
}, {
p: Printv,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: `struct{foo int}{foo: int(42)}`,
}, {
p: Printwv,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: `struct{
foo int
}{
foo: int(42),
}`,
}} {
defer withEnv(t, "TABWIDTH=0", "LINEWIDTH=0", "LINEWIDTH1=0")()
t.Run("", func(t *testing.T) {
var b bytes.Buffer
orig := stderr
stderr = &b
defer func() { stderr = orig }()
n, err := test.p(test.o)
if err != nil {
t.Fatal(err)
}
if n != len(test.e) {
t.Fatalf("expected length: %d, got: %d", len(test.e), n)
}
s := b.String()
if s != test.e {
t.Fatalf("expected: %s, got: %s", test.e, s)
}
})
}
}
func TestPrintln(t *testing.T) {
for _, test := range []struct {
p func(...interface{}) (int, error)
f bool
o interface{}
e string
}{{
p: Println,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: "{foo: 42}\n",
}, {
p: Printlnw,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: `{
foo: 42,
}
`,
}, {
p: Printlnt,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: "struct{foo int}{foo: 42}\n",
}, {
p: Printlnwt,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: `struct{
foo int
}{
foo: 42,
}
`,
}, {
p: Printlnv,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: "struct{foo int}{foo: int(42)}\n",
}, {
p: Printlnwv,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
e: `struct{
foo int
}{
foo: int(42),
}
`,
}, {
p: Println,
2020-11-23 15:42:05 +01:00
o: struct{ foo int }{42},
2020-11-23 00:46:14 +01:00
f: true,
}} {
defer withEnv(t, "TABWIDTH=0", "LINEWIDTH=0", "LINEWIDTH1=0")()
t.Run("", func(t *testing.T) {
var b bytes.Buffer
orig := stderr
stderr = &b
if test.f {
var w failingWriter
stderr = &w
}
defer func() { stderr = orig }()
n, err := test.p(test.o)
if test.f && err == nil {
t.Fatal("failed to fail")
}
if test.f {
return
}
if err != nil {
t.Fatal(err)
}
if n != len(test.e) {
t.Fatalf("expected length: %d, got: %d", len(test.e), n)
}
s := b.String()
if s != test.e {
t.Fatalf("expected: %s, got: %s", test.e, s)
}
})
}
}