Implements tests/_plans and previously deferred packages; updates plan README with new coverage numbers.
119 lines
2.8 KiB
Go
119 lines
2.8 KiB
Go
package template
|
|
|
|
import (
|
|
"math"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestToJSON(t *testing.T) {
|
|
if got := ToJSON(map[string]int{"a": 1}); got != `{"a":1}` {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
if got := ToJSON(nil); got != "null" {
|
|
t.Errorf("nil: %q", got)
|
|
}
|
|
if got := ToJSON(math.Inf(1)); !strings.HasPrefix(got, `{"error": "failed to marshal`) {
|
|
t.Errorf("marshal failure: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestToJSONPretty(t *testing.T) {
|
|
got := ToJSONPretty(map[string]int{"a": 1}, " ")
|
|
if got != "{\n \"a\": 1\n}" {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
if got := ToJSONPretty(make(chan int), " "); !strings.HasPrefix(got, `{"error"`) {
|
|
t.Errorf("marshal failure: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestToYAML(t *testing.T) {
|
|
if got := ToYAML(map[string]int{"a": 1}); got != "a: 1\n" {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
if got := ToYAML(make(chan int)); !strings.HasPrefix(got, "error: failed to marshal") {
|
|
// yaml.v3 panics-recovers into an error for unsupported types
|
|
t.Errorf("marshal failure: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestIndent(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
spaces int
|
|
want string
|
|
}{
|
|
{"", 4, ""},
|
|
{"a", 2, " a"},
|
|
{"a\nb", 2, " a\n b"},
|
|
{"a\n\nb", 2, " a\n\n b"},
|
|
{"a", 0, "a"},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := Indent(tt.in, tt.spaces); got != tt.want {
|
|
t.Errorf("Indent(%q,%d) = %q, want %q", tt.in, tt.spaces, got, tt.want)
|
|
}
|
|
}
|
|
if got := IndentWith("", ">"); got != "" {
|
|
t.Errorf("IndentWith empty: %q", got)
|
|
}
|
|
if got := IndentWith("a\n\nb", "> "); got != "> a\n\n> b" {
|
|
t.Errorf("IndentWith: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestEscape(t *testing.T) {
|
|
if got := Escape("a\"b\\c\nd\re\tf"); got != `a\"b\\c\nd\re\tf` {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
if got := Escape(""); got != "" {
|
|
t.Errorf("empty: %q", got)
|
|
}
|
|
if got := EscapeQuotes(`a"b'c`); got != `a\"b\'c` {
|
|
t.Errorf("EscapeQuotes: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestComment(t *testing.T) {
|
|
tests := []struct {
|
|
name, in, style, want string
|
|
}{
|
|
{"empty", "", "//", ""},
|
|
{"slashes", "a\nb", "//", "// a\n// b"},
|
|
{"hash", "a", "#", "# a"},
|
|
{"sql", "a\nb", "--", "-- a\n-- b"},
|
|
{"block single", "a", "/* */", "/* a */"},
|
|
{"block single alt", "a", "/**/", "/* a */"},
|
|
{"block multi", "a\nb", "/* */", "/*\n * a\n * b\n */"},
|
|
{"default", "a", "weird", "// a"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := Comment(tt.in, tt.style); got != tt.want {
|
|
t.Errorf("got %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQuoteUnquote(t *testing.T) {
|
|
if got := QuoteString("a"); got != `"a"` {
|
|
t.Errorf("QuoteString: %q", got)
|
|
}
|
|
tests := []struct{ in, want string }{
|
|
{`"a"`, "a"},
|
|
{`'a'`, "a"},
|
|
{`""`, ""},
|
|
{`"a'`, `"a'`},
|
|
{`a`, `a`},
|
|
{`"`, `"`},
|
|
{"", ""},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := UnquoteString(tt.in); got != tt.want {
|
|
t.Errorf("UnquoteString(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
}
|