2018-11-07 21:13:47 +00:00
|
|
|
package buildfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-11-09 21:37:05 +00:00
|
|
|
"git.xdrm.io/xdrm-brackets/nix-amer/internal/instruction"
|
2018-11-07 21:13:47 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInstructionSyntax(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
File string
|
|
|
|
Line int
|
|
|
|
Err error
|
|
|
|
}{
|
2018-11-09 21:37:05 +00:00
|
|
|
{"ins args\ndel args\n", 0, nil},
|
|
|
|
{" ins args\ndel args\n", 0, nil},
|
|
|
|
{"\tins args\ndel args\n", 0, nil},
|
|
|
|
{" \t ins args\ndel args\n", 0, nil},
|
|
|
|
{" \t ins args\ndel args\n", 0, nil},
|
2018-11-07 21:13:47 +00:00
|
|
|
|
2018-11-09 21:37:05 +00:00
|
|
|
{"cmd args\ncmd args\n", 1, instruction.ErrUnknownInstruction},
|
|
|
|
{"ins args\ncmd args\n", 2, instruction.ErrUnknownInstruction},
|
|
|
|
{" 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},
|
2018-11-07 21:13:47 +00:00
|
|
|
|
2018-11-09 21:37:05 +00:00
|
|
|
{" md args\ncmd args\n", 1, instruction.ErrInvalidSyntax},
|
|
|
|
{"c d args\ncmd args\n", 1, instruction.ErrInvalidSyntax},
|
|
|
|
{"cm args\ncmd args\n", 1, instruction.ErrInvalidSyntax},
|
2018-11-07 21:13:47 +00:00
|
|
|
|
2018-11-09 21:37:05 +00:00
|
|
|
{"ins args\n md args\n", 2, instruction.ErrInvalidSyntax},
|
|
|
|
{"ins args\nc d args\n", 2, instruction.ErrInvalidSyntax},
|
|
|
|
{"ins args\ncm args\n", 2, instruction.ErrInvalidSyntax},
|
2018-11-07 21:13:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 11:37:31 +00:00
|
|
|
ctx, _ := instruction.CreateContext("apt-get", "")
|
|
|
|
|
2018-11-07 21:13:47 +00:00
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
// create reader
|
|
|
|
buffer := bytes.NewBufferString(test.File)
|
2018-11-10 11:37:31 +00:00
|
|
|
_, err := NewReader(ctx, buffer)
|
2018-11-07 21:13:47 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|