php-parser/internal/position/position_test.go

465 lines
8.8 KiB
Go
Raw Normal View History

package position_test
2020-05-12 21:16:36 +00:00
import (
"testing"
"gotest.tools/assert"
2023-12-09 21:36:19 +00:00
builder "git.maride.cc/maride/php-parser/internal/position"
"git.maride.cc/maride/php-parser/pkg/ast"
"git.maride.cc/maride/php-parser/pkg/position"
"git.maride.cc/maride/php-parser/pkg/token"
2020-05-12 21:16:36 +00:00
)
func TestNewTokenPosition(t *testing.T) {
2020-08-17 17:31:04 +00:00
tkn := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 3,
StartCol: 1,
EndCol: 2,
},
2020-05-12 21:16:36 +00:00
}
pos := builder.NewBuilder().NewTokenPosition(tkn)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(
t,
&position.Position{StartLine: 1, EndLine: 1, EndPos: 3, StartCol: 1, EndCol: 2},
pos,
)
2020-05-12 21:16:36 +00:00
}
func TestNewTokensPosition(t *testing.T) {
2020-08-17 17:31:04 +00:00
token1 := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 3,
},
2020-05-12 21:16:36 +00:00
}
2020-08-17 17:31:04 +00:00
token2 := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 4,
EndPos: 6,
},
2020-05-12 21:16:36 +00:00
}
pos := builder.NewBuilder().NewTokensPosition(token1, token2)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 6}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewNodePosition(t *testing.T) {
n := &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 3,
2020-05-12 21:16:36 +00:00
},
}
pos := builder.NewBuilder().NewNodePosition(n)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 1, EndPos: 3}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewTokenNodePosition(t *testing.T) {
2020-08-17 17:31:04 +00:00
tkn := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 3,
},
2020-05-12 21:16:36 +00:00
}
n := &ast.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 4,
EndPos: 12,
2020-05-12 21:16:36 +00:00
},
}
pos := builder.NewBuilder().NewTokenNodePosition(tkn, n)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 12}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewNodeTokenPosition(t *testing.T) {
n := &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 9,
2020-05-12 21:16:36 +00:00
},
}
2020-08-17 17:31:04 +00:00
tkn := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 10,
EndPos: 12,
},
2020-05-12 21:16:36 +00:00
}
pos := builder.NewBuilder().NewNodeTokenPosition(n, tkn)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 12}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewNodeListPosition(t *testing.T) {
n1 := &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 9,
2020-05-12 21:16:36 +00:00
},
}
n2 := &ast.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 10,
EndPos: 19,
2020-05-12 21:16:36 +00:00
},
}
pos := builder.NewBuilder().NewNodeListPosition([]ast.Vertex{n1, n2})
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 19}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewNodesPosition(t *testing.T) {
n1 := &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 9,
2020-05-12 21:16:36 +00:00
},
}
n2 := &ast.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 10,
EndPos: 19,
2020-05-12 21:16:36 +00:00
},
}
pos := builder.NewBuilder().NewNodesPosition(n1, n2)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 19}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewNodeListTokenPosition(t *testing.T) {
n1 := &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 9,
2020-05-12 21:16:36 +00:00
},
}
n2 := &ast.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 10,
EndPos: 19,
2020-05-12 21:16:36 +00:00
},
}
2020-08-17 17:31:04 +00:00
tkn := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 20,
EndPos: 22,
},
2020-05-12 21:16:36 +00:00
}
pos := builder.NewBuilder().NewNodeListTokenPosition([]ast.Vertex{n1, n2}, tkn)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 22}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewTokenNodeListPosition(t *testing.T) {
2020-08-17 17:31:04 +00:00
tkn := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 2,
},
2020-05-12 21:16:36 +00:00
}
n1 := &ast.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 3,
EndPos: 10,
2020-05-12 21:16:36 +00:00
},
}
n2 := &ast.Identifier{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 11,
EndPos: 20,
2020-05-12 21:16:36 +00:00
},
}
pos := builder.NewBuilder().NewTokenNodeListPosition(tkn, []ast.Vertex{n1, n2})
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 20}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewNodeNodeListPosition(t *testing.T) {
n1 := &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 8,
2020-05-12 21:16:36 +00:00
},
}
n2 := &ast.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 9,
EndPos: 17,
2020-05-12 21:16:36 +00:00
},
}
n3 := &ast.Identifier{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 18,
EndPos: 26,
2020-05-12 21:16:36 +00:00
},
}
pos := builder.NewBuilder().NewNodeNodeListPosition(n1, []ast.Vertex{n2, n3})
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 26}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewNodeListNodePosition(t *testing.T) {
n1 := &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 8,
2020-05-12 21:16:36 +00:00
},
}
n2 := &ast.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 9,
EndPos: 17,
2020-05-12 21:16:36 +00:00
},
}
n3 := &ast.Identifier{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 18,
EndPos: 26,
2020-05-12 21:16:36 +00:00
},
}
pos := builder.NewBuilder().NewNodeListNodePosition([]ast.Vertex{n1, n2}, n3)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 26}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewOptionalListTokensPosition(t *testing.T) {
2020-08-17 17:31:04 +00:00
token1 := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 3,
},
2020-05-12 21:16:36 +00:00
}
2020-08-17 17:31:04 +00:00
token2 := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 4,
EndPos: 6,
},
2020-05-12 21:16:36 +00:00
}
pos := builder.NewBuilder().NewOptionalListTokensPosition(nil, token1, token2)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 6}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNewOptionalListTokensPosition2(t *testing.T) {
n2 := &ast.Identifier{
Position: &position.Position{
StartLine: 2,
EndLine: 2,
StartPos: 9,
EndPos: 17,
2020-05-12 21:16:36 +00:00
},
}
n3 := &ast.Identifier{
Position: &position.Position{
StartLine: 3,
EndLine: 3,
StartPos: 18,
EndPos: 26,
2020-05-12 21:16:36 +00:00
},
}
2020-08-17 17:31:04 +00:00
token1 := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 4,
EndLine: 4,
StartPos: 27,
EndPos: 29,
},
2020-05-12 21:16:36 +00:00
}
2020-08-17 17:31:04 +00:00
token2 := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 5,
EndLine: 5,
StartPos: 30,
EndPos: 32,
},
2020-05-12 21:16:36 +00:00
}
pos := builder.NewBuilder().NewOptionalListTokensPosition([]ast.Vertex{n2, n3}, token1, token2)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 2, EndLine: 5, StartPos: 9, EndPos: 32}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNilNodePos(t *testing.T) {
pos := builder.NewBuilder().NewNodesPosition(nil, nil)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(
t,
&position.Position{
StartLine: -1,
EndLine: -1,
StartPos: -1,
EndPos: -1,
StartCol: -1,
EndCol: -1,
},
pos,
)
2020-05-12 21:16:36 +00:00
}
func TestNilNodeListPos(t *testing.T) {
n1 := &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 8,
2020-05-12 21:16:36 +00:00
},
}
pos := builder.NewBuilder().NewNodeNodeListPosition(n1, nil)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: -1, EndPos: -1, EndCol: -1}, pos)
2020-05-12 21:16:36 +00:00
}
func TestNilNodeListTokenPos(t *testing.T) {
tkn := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 3,
},
2020-05-12 21:16:36 +00:00
}
pos := builder.NewBuilder().NewNodeListTokenPosition(nil, tkn)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(
t,
&position.Position{
StartLine: -1,
EndLine: 1,
StartPos: -1,
EndPos: 3,
StartCol: -1,
EndCol: 0,
},
pos,
)
2020-05-12 21:16:36 +00:00
}
func TestEmptyNodeListPos(t *testing.T) {
n1 := &ast.Identifier{
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 8,
2020-05-12 21:16:36 +00:00
},
}
pos := builder.NewBuilder().NewNodeNodeListPosition(n1, []ast.Vertex{})
2020-05-12 21:16:36 +00:00
assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: -1, EndPos: -1, EndCol: -1}, pos)
2020-05-12 21:16:36 +00:00
}
func TestEmptyNodeListTokenPos(t *testing.T) {
tkn := &token.Token{
Value: []byte(`foo`),
2020-08-17 17:31:04 +00:00
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 3,
},
2020-05-12 21:16:36 +00:00
}
pos := builder.NewBuilder().NewNodeListTokenPosition([]ast.Vertex{}, tkn)
2020-05-12 21:16:36 +00:00
assert.DeepEqual(
t,
&position.Position{StartLine: -1, EndLine: 1, StartPos: -1, EndPos: 3, StartCol: -1},
pos,
)
2020-05-12 21:16:36 +00:00
}