refactor php7

This commit is contained in:
Vadym Slizov
2020-05-13 00:16:36 +03:00
parent aab9da03f0
commit 6a84d58ee6
54 changed files with 29034 additions and 28821 deletions

30
pkg/errors/error.go Normal file
View File

@@ -0,0 +1,30 @@
package errors
import (
"fmt"
"github.com/z7zmey/php-parser/pkg/position"
)
// Error parsing error
type Error struct {
Msg string
Pos *position.Position
}
// NewError creates and returns new Error
func NewError(msg string, p *position.Position) *Error {
return &Error{
Msg: msg,
Pos: p,
}
}
func (e *Error) String() string {
atLine := ""
if e.Pos != nil {
atLine = fmt.Sprintf(" at line %d", e.Pos.StartLine)
}
return fmt.Sprintf("%s%s", e.Msg, atLine)
}

45
pkg/errors/error_test.go Normal file
View File

@@ -0,0 +1,45 @@
package errors_test
import (
"testing"
"gotest.tools/assert"
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/position"
)
func TestConstructor(t *testing.T) {
pos := position.NewPosition(1, 2, 3, 4)
actual := errors.NewError("message", pos)
expected := &errors.Error{
Msg: "message",
Pos: pos,
}
assert.DeepEqual(t, expected, actual)
}
func TestPrint(t *testing.T) {
pos := position.NewPosition(1, 2, 3, 4)
Error := errors.NewError("message", pos)
actual := Error.String()
expected := "message at line 1"
assert.DeepEqual(t, expected, actual)
}
func TestPrintWithotPos(t *testing.T) {
Error := errors.NewError("message", nil)
actual := Error.String()
expected := "message"
assert.DeepEqual(t, expected, actual)
}