|
1 | 1 | package codf
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "fmt"
|
5 | 6 | "reflect"
|
6 | 7 | "testing"
|
7 | 8 | )
|
8 | 9 |
|
| 10 | +type mapFunc func(Node) (Node, error) |
| 11 | + |
| 12 | +var _ WalkMapper = mapFunc(nil) |
| 13 | + |
| 14 | +func (f mapFunc) Map(n Node) (Node, error) { |
| 15 | + return f(n) |
| 16 | +} |
| 17 | + |
| 18 | +func (f mapFunc) Statement(*Statement) error { return nil } |
| 19 | +func (f mapFunc) EnterSection(*Section) (Walker, error) { return f, nil } |
| 20 | + |
9 | 21 | type docWalker struct {
|
10 | 22 | name string
|
11 | 23 | statements map[string]struct{}
|
@@ -313,3 +325,36 @@ func TestWalkMapper(t *testing.T) {
|
313 | 325 | t.Errorf("statements = %q; want %q", walker.statements, wantStatements)
|
314 | 326 | }
|
315 | 327 | }
|
| 328 | + |
| 329 | +func TestMapDelete(t *testing.T) { |
| 330 | + const DocSource = `stmt 1; section arg {}` |
| 331 | + |
| 332 | + defer setlogf(t)() |
| 333 | + |
| 334 | + doc := mustParseNamed(t, "root.conf", DocSource) |
| 335 | + err := Walk(doc, mapFunc(func(Node) (Node, error) { return nil, nil })) |
| 336 | + if err != nil { |
| 337 | + t.Fatalf("err = %v; want nil", err) |
| 338 | + } |
| 339 | + |
| 340 | + if got := doc.Children; len(got) != 0 { |
| 341 | + t.Fatalf("children = %#v; want an empty slice", got) |
| 342 | + } |
| 343 | +} |
| 344 | + |
| 345 | +func TestMapError(t *testing.T) { |
| 346 | + const wantErrMessage = `[1:1:0] root.conf: stmt in main: expected error` |
| 347 | + const DocSource = `stmt 1; section arg {}` |
| 348 | + |
| 349 | + defer setlogf(t)() |
| 350 | + |
| 351 | + doc := mustParseNamed(t, "root.conf", DocSource) |
| 352 | + doc.Children = append([]Node{nil}, doc.Children...) |
| 353 | + |
| 354 | + wantErr := errors.New("expected error") |
| 355 | + err := Walk(doc, mapFunc(func(Node) (Node, error) { return nil, wantErr })) |
| 356 | + |
| 357 | + if got := err.Error(); got != wantErrMessage { |
| 358 | + t.Fatalf("err = %q; want %q", got, wantErrMessage) |
| 359 | + } |
| 360 | +} |
0 commit comments