2018-11-12 07:41:00 +00:00
|
|
|
package nginx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEachLineType(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Raw string
|
|
|
|
Components []string
|
|
|
|
Type LineType
|
|
|
|
}{
|
|
|
|
{"key value;\n", []string{"key", "value"}, ASSIGNMENT},
|
|
|
|
{"key value;\n", []string{"key", "value"}, ASSIGNMENT},
|
|
|
|
{"key \t value;\n", []string{"key", "value"}, ASSIGNMENT},
|
|
|
|
{"key\tvalue;\n", []string{"key", "value"}, ASSIGNMENT},
|
|
|
|
{"ke-y value;\n", []string{"ke-y", "value"}, ASSIGNMENT},
|
|
|
|
{"ke_y value;\n", []string{"ke_y", "value"}, ASSIGNMENT},
|
|
|
|
{"key value; \n", []string{"key", "value"}, ASSIGNMENT},
|
|
|
|
{"key value;\t\n", []string{"key", "value"}, ASSIGNMENT},
|
|
|
|
{"\tkey value;\n", []string{"key", "value"}, ASSIGNMENT},
|
|
|
|
{" \t key value;\n", []string{"key", "value"}, ASSIGNMENT},
|
|
|
|
|
|
|
|
{"include ./file/*.conf;\n", []string{"./file/*.conf"}, INCLUDE},
|
|
|
|
{"include ./file/*.conf; \n", []string{"./file/*.conf"}, INCLUDE},
|
|
|
|
{"include ./file/*.conf;\t\n", []string{"./file/*.conf"}, INCLUDE},
|
|
|
|
{"\tinclude ./file/*.conf;\n", []string{"./file/*.conf"}, INCLUDE},
|
|
|
|
{" \t include ./file/*.conf;\n", []string{"./file/*.conf"}, INCLUDE},
|
|
|
|
|
2018-11-12 11:00:41 +00:00
|
|
|
{"sectionname {\n}\n", []string{"sectionname"}, SECTION},
|
|
|
|
{"section-name {\n}\n", []string{"section-name"}, SECTION},
|
|
|
|
{"section_name {\n}\n", []string{"section_name"}, SECTION},
|
|
|
|
{"sectionname { \n}\n", []string{"sectionname"}, SECTION},
|
|
|
|
{"sectionname {\t\n}\n", []string{"sectionname"}, SECTION},
|
2018-11-12 16:07:30 +00:00
|
|
|
{"sectionname ~ with-args {\t\n}\n", []string{"sectionname", "~ with-args"}, SECTION},
|
2018-11-12 11:00:41 +00:00
|
|
|
{"\tsectionname {\n}\n", []string{"sectionname"}, SECTION},
|
|
|
|
{" \t sectionname {\n}\n", []string{"sectionname"}, SECTION},
|
2018-11-12 07:41:00 +00:00
|
|
|
|
|
|
|
{"#some comment\n", []string{"some comment"}, COMMENT},
|
|
|
|
{"#some\tcomment\n", []string{"some\tcomment"}, COMMENT},
|
2018-11-12 14:38:43 +00:00
|
|
|
{"# some comment \n", []string{" some comment"}, COMMENT},
|
|
|
|
{"# some comment \t\n", []string{" some comment"}, COMMENT},
|
|
|
|
{"\t# some comment {\n", []string{" some comment {"}, COMMENT},
|
2018-11-12 07:41:00 +00:00
|
|
|
|
|
|
|
{";some comment\n", []string{"some comment"}, COLONCOMMENT},
|
2018-11-12 14:38:43 +00:00
|
|
|
{"; some\tcomment\n", []string{" some\tcomment"}, COLONCOMMENT},
|
|
|
|
{"; some comment\n", []string{" some comment"}, COLONCOMMENT},
|
|
|
|
{"; some comment \n", []string{" some comment"}, COLONCOMMENT},
|
|
|
|
{"; some comment \t\n", []string{" some comment"}, COLONCOMMENT},
|
|
|
|
{"\t; some comment {\n", []string{" some comment {"}, COLONCOMMENT},
|
2018-11-12 07:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
// 1. create reader
|
2018-11-12 15:42:24 +00:00
|
|
|
decoder := NewDecoder(strings.NewReader(test.Raw))
|
2018-11-12 07:41:00 +00:00
|
|
|
|
|
|
|
// 2. Decode
|
2018-11-12 18:22:24 +00:00
|
|
|
receiver := new(Line)
|
2018-11-12 11:00:41 +00:00
|
|
|
err := decoder.Decode(receiver)
|
2018-11-12 07:41:00 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] unexpected error <%s>", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-12 11:00:41 +00:00
|
|
|
if len(receiver.Lines) != 1 {
|
|
|
|
t.Errorf("[%d] expected only 1 element, got %d", i, len(receiver.Lines))
|
2018-11-12 07:41:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-12 11:00:41 +00:00
|
|
|
if receiver.Lines[0].Type != test.Type {
|
|
|
|
t.Errorf("[%d] expected type %d, got %d", i, test.Type, receiver.Lines[0].Type)
|
2018-11-12 07:41:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-12 11:00:41 +00:00
|
|
|
if receiver.Lines[0].Components == nil && test.Components != nil {
|
2018-11-12 07:41:00 +00:00
|
|
|
t.Errorf("[%d] expected components not to be null", i)
|
|
|
|
continue
|
|
|
|
}
|
2018-11-12 11:00:41 +00:00
|
|
|
if len(receiver.Lines[0].Components) != len(test.Components) {
|
|
|
|
t.Errorf("[%d] expected %d components, got %d", i, len(test.Components), len(receiver.Lines[0].Components))
|
2018-11-12 07:41:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check each component individually
|
2018-11-12 11:00:41 +00:00
|
|
|
for c, comp := range receiver.Lines[0].Components {
|
2018-11-12 07:41:00 +00:00
|
|
|
if comp != test.Components[c] {
|
|
|
|
t.Errorf("[%d] expected component %d to be '%s', got '%s'", i, c, test.Components[c], comp)
|
2018-11-12 11:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNestedSections(t *testing.T) {
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Raw string
|
|
|
|
SectionChain []string
|
|
|
|
}{
|
|
|
|
|
|
|
|
{"a{\n}\n", []string{"a"}},
|
|
|
|
{"a{\n\n}\n", []string{"a"}},
|
|
|
|
{"a{\n; some comment\n}\n", []string{"a"}},
|
|
|
|
{"a{\n; some comment\n #another comment\n}\n", []string{"a"}},
|
|
|
|
{"a{\nb{\n}\n}\n", []string{"a", "b"}},
|
|
|
|
{"a{\n\tb{\n\t}\n}\n", []string{"a", "b"}},
|
|
|
|
{"a{\n\tb{\n\t\tc{\n\t\t}\n\t}\n}\n", []string{"a", "b", "c"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
// 1. create reader
|
2018-11-12 15:42:24 +00:00
|
|
|
decoder := NewDecoder(strings.NewReader(test.Raw))
|
2018-11-12 07:41:00 +00:00
|
|
|
|
2018-11-12 11:00:41 +00:00
|
|
|
// 2. Decode
|
2018-11-12 18:22:24 +00:00
|
|
|
receiver := new(Line)
|
2018-11-12 11:00:41 +00:00
|
|
|
err := decoder.Decode(receiver)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] unexpected error <%s>", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(receiver.Lines) != 1 {
|
|
|
|
t.Errorf("[%d] expected only 1 element, got %d", i, len(receiver.Lines))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if receiver.Lines[0].Type != SECTION {
|
|
|
|
t.Errorf("[%d] expected type %d, got %d", i, SECTION, receiver.Lines[0].Type)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check each component individually
|
2018-11-12 11:08:54 +00:00
|
|
|
current := receiver.Lines
|
2018-11-12 11:00:41 +00:00
|
|
|
for s, sec := range test.SectionChain {
|
|
|
|
|
|
|
|
// check that section exists in <current>
|
|
|
|
found := false
|
|
|
|
for _, line := range current {
|
|
|
|
|
|
|
|
// found
|
|
|
|
if line.Type == SECTION && line.Components[0] == sec {
|
|
|
|
found = true
|
|
|
|
current = line.Lines
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
t.Errorf("[%d] key '%s' not found\n", i, strings.Join(test.SectionChain[:s+1], "/"))
|
|
|
|
break
|
2018-11-12 07:41:00 +00:00
|
|
|
}
|
2018-11-12 11:00:41 +00:00
|
|
|
|
2018-11-12 07:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|