nix-amer/internal/cnf/parser/nginx/encoder_test.go

112 lines
3.2 KiB
Go

package nginx
import (
"bytes"
"strings"
"testing"
)
func TestDecodeEncode(t *testing.T) {
tests := []struct {
Input string
Output string
}{
{"key value;\n", "key value;\n"},
{"key value;\n", "key value;\n"},
{"key \t value;\n", "key value;\n"},
{"key\tvalue;\n", "key value;\n"},
{"ke-y value;\n", "ke-y value;\n"},
{"ke_y value;\n", "ke_y value;\n"},
{"key value; \n", "key value;\n"},
{"key value;\t\n", "key value;\n"},
{"\tkey value;\n", "key value;\n"},
{" \t key value;\n", "key value;\n"},
{"include ./file/*.conf;\n", "include ./file/*.conf;\n"},
{"include ./file/*.conf; \n", "include ./file/*.conf;\n"},
{"include ./file/*.conf;\t\n", "include ./file/*.conf;\n"},
{"\tinclude ./file/*.conf;\n", "include ./file/*.conf;\n"},
{" \t include ./file/*.conf;\n", "include ./file/*.conf;\n"},
{"sectionname {\n}\n", "sectionname {\n}\n\n"},
{"section-name {\n}\n", "section-name {\n}\n\n"},
{"section_name {\n}\n", "section_name {\n}\n\n"},
{"sectionname { \n}\n", "sectionname {\n}\n\n"},
{"sectionname {\t\n}\n", "sectionname {\n}\n\n"},
{"\tsectionname {\n}\n", "sectionname {\n}\n\n"},
{" \t sectionname {\n}\n", "sectionname {\n}\n\n"},
{"sectionname ~ with-args {\t\n}\n", "sectionname ~ with-args {\n}\n\n"},
{"#some comment\n", "#some comment\n"},
{"#some\tcomment\n", "#some\tcomment\n"},
{"# some comment \n", "# some comment\n"},
{"# some comment \t\n", "# some comment\n"},
{"\t# some comment {\n", "# some comment {\n"},
{";some comment\n", ";some comment\n"},
{"; some\tcomment\n", "; some\tcomment\n"},
{"; some comment\n", "; some comment\n"},
{"; some comment \n", "; some comment\n"},
{"; some comment \t\n", "; some comment\n"},
{"\t; some comment {\n", "; some comment {\n"},
{"a{\n}\n", "a {\n}\n\n"},
{"a{\n\n}\n", "a {\n}\n\n"},
{"a{\n; some comment\n}\n", "a {\n\t; some comment\n}\n\n"},
{"a{\n; some comment\n #another comment\n}\n", "a {\n\t; some comment\n\t#another comment\n}\n\n"},
{"a{\nb{\n}\n}\n", "a {\n\tb {\n\t}\n\n}\n\n"},
{"a{\n\tb{\n\t}\n}\n", "a {\n\tb {\n\t}\n\n}\n\n"},
{"a{\nb{\nc{\n}\n}\n}\n", "a {\n\tb {\n\t\tc {\n\t\t}\n\n\t}\n\n}\n\n"},
}
for i, test := range tests {
// create reader/writer
r, w := strings.NewReader(test.Input), &bytes.Buffer{}
// parse input
receiver := new(Line)
decoder := NewDecoder(r)
if err := decoder.Decode(receiver); err != nil {
t.Errorf("[%d] unexpected error <%s>", i, err)
continue
}
// encode back to writer
encoder := NewEncoder(w)
encoder.SetIndent("", "\t")
if err := encoder.Encode(receiver); err != nil {
t.Errorf("[%d] unexpected error <%s>", i, err)
continue
}
// check equality
if w.String() != test.Output {
t.Errorf("[%d] expected '%s', got '%s'", i, escape(test.Output), escape(w.String()))
}
}
}
func escape(raw string) string {
escaped := make([]rune, 0)
for _, char := range raw {
if char == '\n' {
escaped = append(escaped, []rune("\\n")...)
} else if char == '\t' {
escaped = append(escaped, []rune("\\t")...)
} else if char == '\r' {
escaped = append(escaped, []rune("\\r")...)
} else {
escaped = append(escaped, char)
}
}
return string(escaped)
}