Skip to content

Commit

Permalink
test: add test for Visitable Rewrite and Walk
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Feb 27, 2025
1 parent 4a4eea7 commit 3546eda
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions go/tools/asthelpergen/integration/integration_rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,25 @@ func TestRefSliceContainerReplace(t *testing.T) {
}, ast)
}

func TestVisitableRewrite(t *testing.T) {
leaf := &Leaf{v: 1}
visitable := &testVisitable{inner: leaf}
refContainer := &RefContainer{ASTType: visitable}

tv := &rewriteTestVisitor{}

_ = Rewrite(refContainer, tv.pre, tv.post)

tv.assertEquals(t, []step{
Pre{refContainer},
Pre{visitable},
Pre{leaf},
Post{leaf},
Post{visitable},
Post{refContainer},
})
}

type step interface {
String() string
}
Expand Down
33 changes: 33 additions & 0 deletions go/tools/asthelpergen/integration/integration_visit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,39 @@ func TestVisitInterfaceSlice(t *testing.T) {
})
}

// testVisitable is declared in a test package, so it lacks the normal generated AST helper methods.
// Instead, we lean on the Visitable interface to make sure we can visit nodes inside it.
type testVisitable struct {
inner AST
}

func (t *testVisitable) String() string {
return t.inner.String()
}

func (t *testVisitable) VisitThis() AST {
return t.inner
}

var _ Visitable = (*testVisitable)(nil)

func TestVisitableVisit(t *testing.T) {
leaf := &Leaf{v: 1}
visitable := &testVisitable{inner: leaf}
refContainer := &RefContainer{ASTType: visitable}

tv := &testVisitor{}

require.NoError(t,
VisitAST(refContainer, tv.visit))

tv.assertVisitOrder(t, []AST{
refContainer,
visitable,
leaf,
})
}

func (tv *testVisitor) assertVisitOrder(t *testing.T, expected []AST) {
t.Helper()
var lines []string
Expand Down

0 comments on commit 3546eda

Please sign in to comment.