2018-04-09 20:26:44 +00:00
|
|
|
package errors_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/position"
|
|
|
|
|
|
|
|
"github.com/z7zmey/php-parser/errors"
|
2018-04-15 11:47:40 +00:00
|
|
|
"github.com/z7zmey/php-parser/scanner"
|
2018-04-09 20:26:44 +00:00
|
|
|
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
diff := pretty.Compare(expected, actual)
|
|
|
|
|
|
|
|
if diff != "" {
|
|
|
|
t.Errorf("diff: (-expected +actual)\n%s", diff)
|
|
|
|
} else {
|
|
|
|
t.Errorf("expected and actual are not equal\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConstructor(t *testing.T) {
|
2018-04-15 19:56:20 +00:00
|
|
|
pos := position.NewPosition(1, 2, 3, 4)
|
2018-06-10 23:41:12 +00:00
|
|
|
token := &scanner.Token{
|
2018-06-24 07:19:44 +00:00
|
|
|
Value: `test`,
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 2,
|
|
|
|
StartPos: 3,
|
|
|
|
EndPos: 4,
|
2018-06-10 23:41:12 +00:00
|
|
|
}
|
2018-04-09 20:26:44 +00:00
|
|
|
|
|
|
|
actual := errors.NewError("message", token)
|
|
|
|
|
|
|
|
expected := &errors.Error{
|
|
|
|
Msg: "message",
|
2018-04-15 19:56:20 +00:00
|
|
|
Pos: pos,
|
2018-04-09 20:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrint(t *testing.T) {
|
2018-06-10 23:41:12 +00:00
|
|
|
token := &scanner.Token{
|
2018-06-24 07:19:44 +00:00
|
|
|
Value: `test`,
|
|
|
|
StartLine: 1,
|
|
|
|
EndLine: 2,
|
|
|
|
StartPos: 3,
|
|
|
|
EndPos: 4,
|
2018-06-10 23:41:12 +00:00
|
|
|
}
|
2018-04-09 20:26:44 +00:00
|
|
|
|
|
|
|
Error := errors.NewError("message", token)
|
|
|
|
|
|
|
|
actual := Error.String()
|
|
|
|
|
|
|
|
expected := "message at line 1"
|
|
|
|
|
|
|
|
assertEqual(t, expected, actual)
|
|
|
|
}
|