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}, {"sectionname {\n", []string{"sectionname"}, SECTION}, {"section-name {\n", []string{"section-name"}, SECTION}, {"section_name {\n", []string{"section_name"}, SECTION}, {"sectionname { \n", []string{"sectionname"}, SECTION}, {"sectionname {\t\n", []string{"sectionname"}, SECTION}, {"\tsectionname {\n", []string{"sectionname"}, SECTION}, {" \t sectionname {\n", []string{"sectionname"}, SECTION}, {"#some comment\n", []string{"some comment"}, COMMENT}, {"#some\tcomment\n", []string{"some\tcomment"}, COMMENT}, {"# some comment \n", []string{"some comment"}, COMMENT}, {"# some comment \t\n", []string{"some comment"}, COMMENT}, {"\t# some comment {\n", []string{"some comment {"}, COMMENT}, {";some comment\n", []string{"some comment"}, COLONCOMMENT}, {"; 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}, } for i, test := range tests { // 1. create reader parser := new(nginx) decoder := parser.NewDecoder(strings.NewReader(test.Raw)) // 2. Decode receiver := []*Line{} err := decoder.Decode(&receiver) if err != nil { t.Errorf("[%d] unexpected error <%s>", i, err) continue } if len(receiver) != 1 { t.Errorf("[%d] expected only 1 element, got %d", i, len(receiver)) continue } if receiver[0].Type != test.Type { t.Errorf("[%d] expected type %d, got %d", i, test.Type, receiver[0].Type) continue } if receiver[0].Components == nil && test.Components != nil { t.Errorf("[%d] expected components not to be null", i) continue } if len(receiver[0].Components) != len(test.Components) { t.Errorf("[%d] expected %d components, got %d", i, len(test.Components), len(receiver[0].Components)) continue } // check each component individually for c, comp := range receiver[0].Components { if comp != test.Components[c] { t.Errorf("[%d] expected component %d to be '%s', got '%s'", i, c, test.Components[c], comp) continue } } } }