(t *testing.T)
| 500 | } |
| 501 | |
| 502 | func TestMatchParser_BindingExtraction(t *testing.T) { |
| 503 | tests := []struct { |
| 504 | name string |
| 505 | input string |
| 506 | wantBindings []string |
| 507 | }{ |
| 508 | { |
| 509 | name: "simple binding", |
| 510 | input: "match x { Ok(value) => value }", |
| 511 | wantBindings: []string{"value"}, |
| 512 | }, |
| 513 | { |
| 514 | name: "nested bindings", |
| 515 | input: "match x { Ok(Some(value)) => value }", |
| 516 | wantBindings: []string{"value"}, |
| 517 | }, |
| 518 | { |
| 519 | name: "tuple bindings", |
| 520 | input: "match x { (a, b) => a }", |
| 521 | wantBindings: []string{"a", "b"}, |
| 522 | }, |
| 523 | { |
| 524 | name: "nested tuple bindings", |
| 525 | input: "match x { (Ok(x), Err(e)) => x }", |
| 526 | wantBindings: []string{"x", "e"}, |
| 527 | }, |
| 528 | } |
| 529 | |
| 530 | for _, tt := range tests { |
| 531 | t.Run(tt.name, func(t *testing.T) { |
| 532 | tok := tokenizer.New([]byte(tt.input)) |
| 533 | _, err := tok.Tokenize() |
| 534 | if err != nil { |
| 535 | t.Fatalf("tokenize error: %v", err) |
| 536 | } |
| 537 | |
| 538 | parser := NewMatchParser(tok) |
| 539 | expr, err := parser.ParseMatchExpr() |
| 540 | if err != nil { |
| 541 | t.Fatalf("parse error: %v", err) |
| 542 | } |
| 543 | |
| 544 | if len(expr.Arms) == 0 { |
| 545 | t.Fatal("no arms parsed") |
| 546 | } |
| 547 | |
| 548 | bindings := expr.Arms[0].Pattern.GetBindings() |
| 549 | if len(bindings) != len(tt.wantBindings) { |
| 550 | t.Errorf("got %d bindings, want %d", len(bindings), len(tt.wantBindings)) |
| 551 | } |
| 552 | |
| 553 | for i, want := range tt.wantBindings { |
| 554 | if i >= len(bindings) { |
| 555 | break |
| 556 | } |
| 557 | if bindings[i].Name != want { |
| 558 | t.Errorf("binding %d: got %q, want %q", i, bindings[i].Name, want) |
| 559 | } |
nothing calls this directly
no test coverage detected