refactoring: update position builder tests
This commit is contained in:
		
							parent
							
								
									90aca88c2f
								
							
						
					
					
						commit
						286dd50319
					
				| @ -21,7 +21,7 @@ func TestNewTokenPosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewTokenPosition(tkn) | ||||
| 	pos := builder.NewBuilder().NewTokenPosition(tkn) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 1, EndPos: 3}, pos) | ||||
| } | ||||
| @ -46,7 +46,7 @@ func TestNewTokensPosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewTokensPosition(token1, token2) | ||||
| 	pos := builder.NewBuilder().NewTokensPosition(token1, token2) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 6}, pos) | ||||
| } | ||||
| @ -61,7 +61,7 @@ func TestNewNodePosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodePosition(n) | ||||
| 	pos := builder.NewBuilder().NewNodePosition(n) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 1, EndPos: 3}, pos) | ||||
| } | ||||
| @ -85,7 +85,7 @@ func TestNewTokenNodePosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewTokenNodePosition(tkn, n) | ||||
| 	pos := builder.NewBuilder().NewTokenNodePosition(tkn, n) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 12}, pos) | ||||
| } | ||||
| @ -110,7 +110,7 @@ func TestNewNodeTokenPosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodeTokenPosition(n, tkn) | ||||
| 	pos := builder.NewBuilder().NewNodeTokenPosition(n, tkn) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 12}, pos) | ||||
| } | ||||
| @ -134,7 +134,7 @@ func TestNewNodeListPosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodeListPosition([]ast.Vertex{n1, n2}) | ||||
| 	pos := builder.NewBuilder().NewNodeListPosition([]ast.Vertex{n1, n2}) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 19}, pos) | ||||
| } | ||||
| @ -158,7 +158,7 @@ func TestNewNodesPosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodesPosition(n1, n2) | ||||
| 	pos := builder.NewBuilder().NewNodesPosition(n1, n2) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 19}, pos) | ||||
| } | ||||
| @ -192,7 +192,7 @@ func TestNewNodeListTokenPosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodeListTokenPosition([]ast.Vertex{n1, n2}, tkn) | ||||
| 	pos := builder.NewBuilder().NewNodeListTokenPosition([]ast.Vertex{n1, n2}, tkn) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 22}, pos) | ||||
| } | ||||
| @ -226,7 +226,7 @@ func TestNewTokenNodeListPosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewTokenNodeListPosition(tkn, []ast.Vertex{n1, n2}) | ||||
| 	pos := builder.NewBuilder().NewTokenNodeListPosition(tkn, []ast.Vertex{n1, n2}) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 20}, pos) | ||||
| } | ||||
| @ -259,7 +259,7 @@ func TestNewNodeNodeListPosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodeNodeListPosition(n1, []ast.Vertex{n2, n3}) | ||||
| 	pos := builder.NewBuilder().NewNodeNodeListPosition(n1, []ast.Vertex{n2, n3}) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 26}, pos) | ||||
| } | ||||
| @ -290,7 +290,7 @@ func TestNewNodeListNodePosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodeListNodePosition([]ast.Vertex{n1, n2}, n3) | ||||
| 	pos := builder.NewBuilder().NewNodeListNodePosition([]ast.Vertex{n1, n2}, n3) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 26}, pos) | ||||
| } | ||||
| @ -315,7 +315,7 @@ func TestNewOptionalListTokensPosition(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewOptionalListTokensPosition(nil, token1, token2) | ||||
| 	pos := builder.NewBuilder().NewOptionalListTokensPosition(nil, token1, token2) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 6}, pos) | ||||
| } | ||||
| @ -357,13 +357,13 @@ func TestNewOptionalListTokensPosition2(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewOptionalListTokensPosition([]ast.Vertex{n2, n3}, token1, token2) | ||||
| 	pos := builder.NewBuilder().NewOptionalListTokensPosition([]ast.Vertex{n2, n3}, token1, token2) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 2, EndLine: 5, StartPos: 9, EndPos: 32}, pos) | ||||
| } | ||||
| 
 | ||||
| func TestNilNodePos(t *testing.T) { | ||||
| 	pos := builder.NewNodesPosition(nil, nil) | ||||
| 	pos := builder.NewBuilder().NewNodesPosition(nil, nil) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: -1, EndLine: -1, StartPos: -1, EndPos: -1}, pos) | ||||
| } | ||||
| @ -378,7 +378,7 @@ func TestNilNodeListPos(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodeNodeListPosition(n1, nil) | ||||
| 	pos := builder.NewBuilder().NewNodeNodeListPosition(n1, nil) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: -1, EndPos: -1}, pos) | ||||
| } | ||||
| @ -394,7 +394,7 @@ func TestNilNodeListTokenPos(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodeListTokenPosition(nil, tkn) | ||||
| 	pos := builder.NewBuilder().NewNodeListTokenPosition(nil, tkn) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: -1, EndLine: 1, StartPos: -1, EndPos: 3}, pos) | ||||
| } | ||||
| @ -409,7 +409,7 @@ func TestEmptyNodeListPos(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodeNodeListPosition(n1, []ast.Vertex{}) | ||||
| 	pos := builder.NewBuilder().NewNodeNodeListPosition(n1, []ast.Vertex{}) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: -1, EndPos: -1}, pos) | ||||
| } | ||||
| @ -425,7 +425,7 @@ func TestEmptyNodeListTokenPos(t *testing.T) { | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	pos := builder.NewNodeListTokenPosition([]ast.Vertex{}, tkn) | ||||
| 	pos := builder.NewBuilder().NewNodeListTokenPosition([]ast.Vertex{}, tkn) | ||||
| 
 | ||||
| 	assert.DeepEqual(t, &position.Position{StartLine: -1, EndLine: 1, StartPos: -1, EndPos: 3}, pos) | ||||
| } | ||||
|  | ||||
| @ -1,102 +0,0 @@ | ||||
| package ast_test | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"github.com/z7zmey/php-parser/pkg/ast" | ||||
| 	"github.com/z7zmey/php-parser/pkg/ast/traverser" | ||||
| 	"github.com/z7zmey/php-parser/pkg/ast/visitor" | ||||
| 	"os" | ||||
| 	"strings" | ||||
| ) | ||||
| 
 | ||||
| func ExampleStxTree() { | ||||
| 	stxTree := &ast.Root{ | ||||
| 		Stmts: []ast.Vertex{ | ||||
| 			&ast.Nullable{ | ||||
| 				Expr: &ast.Parameter{ | ||||
| 					Type:         nil, | ||||
| 					Var:          nil, | ||||
| 					DefaultValue: nil, | ||||
| 				}, | ||||
| 			}, | ||||
| 			&ast.Identifier{}, | ||||
| 			&ast.ArgumentList{ | ||||
| 				Arguments: []ast.Vertex{ | ||||
| 					&ast.Argument{}, | ||||
| 					&ast.Argument{ | ||||
| 						Expr: &ast.ScalarDnumber{}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	traverser.NewDFS(&testVisitor{}).Traverse(stxTree) | ||||
| 
 | ||||
| 	//output: | ||||
| 	//=>  *ast.Root | ||||
| 	//=>    Stmts: | ||||
| 	//=>      *ast.Nullable | ||||
| 	//=>        Expr: | ||||
| 	//=>          *ast.Parameter | ||||
| 	//=>      *ast.Identifier | ||||
| 	//=>      *ast.ArgumentList | ||||
| 	//=>        Arguments: | ||||
| 	//=>          *ast.Argument | ||||
| 	//=>          *ast.Argument | ||||
| 	//=>            Expr: | ||||
| 	//=>              *ast.ScalarDnumber | ||||
| } | ||||
| 
 | ||||
| type testVisitor struct { | ||||
| 	visitor.Null | ||||
| 	depth int | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) Enter(key string, _ bool) { | ||||
| 	v.depth++ | ||||
| 	fmt.Fprint(os.Stdout, "=>", strings.Repeat("  ", v.depth), key, ":\n") | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) Leave(key string, _ bool) { | ||||
| 	v.depth-- | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) EnterNode(n ast.Vertex) bool { | ||||
| 	v.depth++ | ||||
| 	n.Accept(v) | ||||
| 
 | ||||
| 	return true | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) LeaveNode(_ ast.Vertex) { | ||||
| 	v.depth-- | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) Root(_ *ast.Root) { | ||||
| 	fmt.Fprintln(os.Stdout, "=>", strings.Repeat("  ", v.depth-1), "*ast.Root") | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) Nullable(_ *ast.Nullable) { | ||||
| 	fmt.Fprintln(os.Stdout, "=>", strings.Repeat("  ", v.depth-1), "*ast.Nullable") | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) Parameter(_ *ast.Parameter) { | ||||
| 	fmt.Fprintln(os.Stdout, "=>", strings.Repeat("  ", v.depth-1), "*ast.Parameter") | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) Identifier(_ *ast.Identifier) { | ||||
| 	fmt.Fprintln(os.Stdout, "=>", strings.Repeat("  ", v.depth-1), "*ast.Identifier") | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) ArgumentList(_ *ast.ArgumentList) { | ||||
| 	fmt.Fprintln(os.Stdout, "=>", strings.Repeat("  ", v.depth-1), "*ast.ArgumentList") | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) Argument(_ *ast.Argument) { | ||||
| 	fmt.Fprintln(os.Stdout, "=>", strings.Repeat("  ", v.depth-1), "*ast.Argument") | ||||
| } | ||||
| 
 | ||||
| func (v *testVisitor) ScalarDnumber(_ *ast.ScalarDnumber) { | ||||
| 	fmt.Fprintln(os.Stdout, "=>", strings.Repeat("  ", v.depth-1), "*ast.ScalarDnumber") | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user