fix decoder + tests

This commit is contained in:
xdrm-brackets 2018-11-12 16:50:50 +01:00
parent 0d6067dda5
commit 02bf3d9103
2 changed files with 10 additions and 3 deletions

View File

@ -68,7 +68,6 @@ func (e *encoder) Encode(v interface{}) error {
if line == nil { if line == nil {
repr = append(repr, []byte("}\n\n")...) repr = append(repr, []byte("}\n\n")...)
// Note: new line after section for readability // Note: new line after section for readability
indent--
// 3. comments // 3. comments
} else if line.Type == COMMENT { } else if line.Type == COMMENT {
@ -92,7 +91,7 @@ func (e *encoder) Encode(v interface{}) error {
// push children lines in reverse order // push children lines in reverse order
if len(line.Lines) > 0 { if len(line.Lines) > 0 {
for l := len(line.Lines); l > 0; l-- { for l := len(line.Lines) - 1; l >= 0; l-- {
stack = append(stack, line.Lines[l]) stack = append(stack, line.Lines[l])
} }
} }

View File

@ -48,6 +48,14 @@ func TestDecodeEncode(t *testing.T) {
{"; some comment \n", "; some comment\n"}, {"; some comment \n", "; some comment\n"},
{"; some comment \t\n", "; some comment\n"}, {"; some comment \t\n", "; some comment\n"},
{"\t; some comment {\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 { for i, test := range tests {
@ -73,7 +81,7 @@ func TestDecodeEncode(t *testing.T) {
// check equality // check equality
if w.String() != test.Output { if w.String() != test.Output {
t.Errorf("[%d] expected '%s', got '%s'", i, escape(test.Output), escape(w.String())) t.Errorf("[%d] expected '%s', got '%s'", i, (test.Output), (w.String()))
} }
} }