nix-amer/internal/buildfile/reader_test.go

90 lines
2.4 KiB
Go
Raw Normal View History

package buildfile
import (
"bytes"
"git.xdrm.io/go/nix-amer/internal/instruction"
"testing"
)
func TestNullContext(t *testing.T) {
buffer := bytes.NewBufferString("")
_, err := NewReader(nil, buffer)
if err != ErrNullContext {
t.Fatalf("expected <%s>, got <%v>", ErrNullContext, err)
}
}
func TestIgnoreCommentsAndEmptyLines(t *testing.T) {
ctx, _ := instruction.CreateContext("apt-get", "")
buffer := bytes.NewBufferString("[ some comment ]\n\n \t \n\t \t\n[ other comment after empty lines ]")
r, err := NewReader(ctx, buffer)
if err != nil {
t.Fatalf("unexpected error <%v>", err)
}
if len(r.Content) > 0 {
t.Fatalf("expected no content, got %d instructions", len(r.Content))
}
}
func TestInstructionSyntax(t *testing.T) {
tests := []struct {
File string
Line int
Err error
}{
{"install args\ndelete args\n", 0, nil},
{" install args\ndelete args\n", 0, nil},
{"\tinstall args\ndelete args\n", 0, nil},
{" \t install args\ndelete args\n", 0, nil},
{" \t install args\ndelete args\n", 0, nil},
2018-11-09 21:37:05 +00:00
{"cmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
{"install args\ncmd args\n", 2, instruction.ErrUnknownInstruction},
2018-11-09 21:37:05 +00:00
{" cmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
{"\tcmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
{" \t cmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
{" \t cmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
{"cmd args\ncmd\n", 1, instruction.ErrUnknownInstruction},
{"install\ncmd args\n", 1, instruction.ErrInvalidSyntax},
{"install args\n cmd args\n", 2, instruction.ErrUnknownInstruction},
{"install args\ncmd\n", 2, instruction.ErrInvalidSyntax},
}
ctx, _ := instruction.CreateContext("apt-get", "")
for i, test := range tests {
// create reader
buffer := bytes.NewBufferString(test.File)
_, err := NewReader(ctx, buffer)
// no error expected
if test.Err == nil {
if err != nil {
lineerr := err.(LineError)
t.Errorf("[%d] expect no error, got <%s>", i, lineerr.Err)
}
continue
} else if err == nil {
t.Errorf("[%d] expect error <%s>, got none", i, test.Err)
continue
}
lineerr := err.(LineError)
// error expected
if lineerr.Err != test.Err {
t.Errorf("[%d] expect error <%s>, got <%s>", i, test.Err, lineerr.Err)
continue
}
if lineerr.Line != test.Line {
t.Errorf("[%d] expect error at line %d, got %d", i, test.Line, lineerr.Line)
}
}
}