add cnf/parser/nginx/decoder receiver/syntax/writer error

This commit is contained in:
Adrien Marquès 2018-11-13 21:33:16 +01:00
parent a77923a686
commit a9a69a5728
2 changed files with 66 additions and 9 deletions

View File

@ -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
}

View File

@ -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)
}
}