68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package buildfile
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestInstructionSyntax(t *testing.T) {
|
|
tests := []struct {
|
|
File string
|
|
Line int
|
|
Err error
|
|
}{
|
|
{"ins args\ncnf args\n", 0, nil},
|
|
{" ins args\ncnf args\n", 0, nil},
|
|
{"\tins args\ncnf args\n", 0, nil},
|
|
{" \t ins args\ncnf args\n", 0, nil},
|
|
{" \t ins args\ncnf args\n", 0, nil},
|
|
|
|
{"cmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
|
{"ins args\ncmd args\n", 2, ErrUnknownInstruction},
|
|
{" cmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
|
{"\tcmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
|
{" \t cmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
|
{" \t cmd args\ncmd args\n", 1, ErrUnknownInstruction},
|
|
|
|
{" md args\ncmd args\n", 1, ErrInvalidSyntax},
|
|
{"c d args\ncmd args\n", 1, ErrInvalidSyntax},
|
|
{"cm args\ncmd args\n", 1, ErrInvalidSyntax},
|
|
|
|
{"ins args\n md args\n", 2, ErrInvalidSyntax},
|
|
{"ins args\nc d args\n", 2, ErrInvalidSyntax},
|
|
{"ins args\ncm args\n", 2, ErrInvalidSyntax},
|
|
}
|
|
|
|
for i, test := range tests {
|
|
|
|
// create reader
|
|
buffer := bytes.NewBufferString(test.File)
|
|
_, err := NewReader("ubuntu", 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)
|
|
}
|
|
|
|
}
|
|
}
|