| 45 | } |
| 46 | |
| 47 | func TestVisitor(t *testing.T) { |
| 48 | // Create a simple AST |
| 49 | ast := &AST{ |
| 50 | Statements: []Statement{ |
| 51 | &SelectStatement{ |
| 52 | Columns: []Expression{ |
| 53 | &Identifier{Name: "id"}, |
| 54 | &Identifier{Name: "name"}, |
| 55 | }, |
| 56 | TableName: "users", |
| 57 | Where: &BinaryExpression{ |
| 58 | Left: &Identifier{Name: "id"}, |
| 59 | Operator: "=", |
| 60 | Right: &Identifier{Name: "1"}, |
| 61 | }, |
| 62 | }, |
| 63 | }, |
| 64 | } |
| 65 | |
| 66 | counter := &nodeCounter{} |
| 67 | err := Walk(counter, ast) |
| 68 | if err != nil { |
| 69 | t.Errorf("unexpected error: %v", err) |
| 70 | } |
| 71 | |
| 72 | // Count expected nodes: |
| 73 | // 1. AST |
| 74 | // 2. SelectStatement |
| 75 | // 3-4. Two column Identifiers |
| 76 | // 5. BinaryExpression |
| 77 | // 6-7. Left and right Identifiers in BinaryExpression |
| 78 | expectedCount := 7 |
| 79 | if counter.count != expectedCount { |
| 80 | t.Errorf("expected %d nodes, got %d", expectedCount, counter.count) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestInspector(t *testing.T) { |
| 85 | // Create a simple AST |