From a9a69a572808f33d4adfe213458ac96bd120cf43 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 13 Nov 2018 21:33:16 +0100 Subject: [PATCH] add cnf/parser/nginx/decoder receiver/syntax/writer error --- internal/cnf/parser/nginx/decoder.go | 9 ---- internal/cnf/parser/nginx/decoder_test.go | 66 +++++++++++++++++++++++ 2 files changed, 66 insertions(+), 9 deletions(-) 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) + } + +}