mirror of
https://github.com/neilotoole/sq.git
synced 2024-12-18 05:31:38 +03:00
db55986980
- Support for ingest cache, download cache, and progress bars.
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package ast
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/lg/lgt"
|
|
)
|
|
|
|
func TestWalker(t *testing.T) {
|
|
const q1 = `@mydb1 | .user | join(.address, .user.uid == .address.uid) | .uid, .username, .country`
|
|
|
|
p := getSLQParser(q1)
|
|
query := p.Query()
|
|
ast, err := buildAST(lgt.New(t), query)
|
|
|
|
assert.Nil(t, err)
|
|
assert.NotNil(t, ast)
|
|
|
|
walker := NewWalker(ast)
|
|
count := 0
|
|
|
|
visitor := func(w *Walker, node Node) error {
|
|
count++
|
|
return w.visitChildren(node)
|
|
}
|
|
|
|
walker.AddVisitor(typeJoinNode, visitor)
|
|
err = walker.Walk()
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 1, count)
|
|
|
|
// test multiple visitors on the same node type
|
|
walker = NewWalker(ast)
|
|
countA := 0
|
|
visitorA := func(w *Walker, node Node) error {
|
|
countA++
|
|
return w.visitChildren(node)
|
|
}
|
|
countB := 0
|
|
visitorB := func(w *Walker, node Node) error {
|
|
countB++
|
|
return w.visitChildren(node)
|
|
}
|
|
|
|
walker.AddVisitor(typeTblSelectorNode, visitorA)
|
|
walker.AddVisitor(typeColSelectorNode, visitorB)
|
|
err = walker.Walk()
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, 1, countA)
|
|
assert.Equal(t, 3, countB)
|
|
}
|