diff --git a/internal/cnf/parser/nginx/decoder.go b/internal/cnf/parser/nginx/decoder.go index 2cc8f79..8cb9a3e 100644 --- a/internal/cnf/parser/nginx/decoder.go +++ b/internal/cnf/parser/nginx/decoder.go @@ -155,12 +155,3 @@ func (d *Decoder) Decode(v interface{}) error { return nil } - -func inArray(haystack string, needle byte) bool { - for i, l := 0, len(haystack); i < l; i++ { - if haystack[i] == needle { - return true - } - } - return false -} diff --git a/internal/cnf/parser/nginx/decoder_test.go b/internal/cnf/parser/nginx/decoder_test.go index 6b8b35a..b4d40fa 100644 --- a/internal/cnf/parser/nginx/decoder_test.go +++ b/internal/cnf/parser/nginx/decoder_test.go @@ -1,6 +1,7 @@ package nginx import ( + "errors" "strings" "testing" ) @@ -160,3 +161,68 @@ func TestNestedSections(t *testing.T) { } } + +func TestReceiverAndSyntaxErrors(t *testing.T) { + tests := []struct { + Receiver interface{} + Input string + Err error + }{ + {new(Line), "", nil}, + {nil, "", ErrNullReceiver}, + {[]byte{}, "", ErrInvalidReceiver}, + + {new(Line), "}", &LineError{0, ErrUnexpectedSectionClose}}, + {new(Line), "key valuewithoutsemicolon", &LineError{0, ErrInvalidSyntax}}, + {new(Line), "section {\nkey value;", ErrUnclosedSection}, + } + + for i, test := range tests { + + // create reader + r := strings.NewReader(test.Input) + + // parse input + var receiver interface{} = test.Receiver + decoder := NewDecoder(r) + err := decoder.Decode(receiver) + if err == nil { + if test.Err != nil { + t.Errorf("[%d] expected error", i) + } + continue + } + + if err.Error() != test.Err.Error() { + t.Errorf("[%d] expected error <%s>, got <%s>", i, test.Err, err) + continue + } + + } +} + +var readerError = errors.New("error") + +type defectiveReader struct{} + +func (d defectiveReader) Read(buf []byte) (int, error) { + return 0, readerError +} +func TestReadErrors(t *testing.T) { + + // create reader + r := &defectiveReader{} + + // parse input + receiver := new(Line) + decoder := NewDecoder(r) + err := decoder.Decode(receiver) + if err == nil { + t.Fatalf("expected error") + } + + if err.Error() != (&LineError{0, readerError}).Error() { + t.Fatalf("expected error <%s>, got <%s>", &LineError{0, readerError}, err) + } + +}