TestMatchCodeGen_ValueEnumCodeGen tests code generation for value enum match.
(t *testing.T)
| 440 | |
| 441 | // TestMatchCodeGen_ValueEnumCodeGen tests code generation for value enum match. |
| 442 | func TestMatchCodeGen_ValueEnumCodeGen(t *testing.T) { |
| 443 | // Create a value enum registry |
| 444 | registry := ast.NewEnumRegistry() |
| 445 | registry.RegisterValueEnum("Status", []string{"Pending", "Active", "Closed"}, true) |
| 446 | |
| 447 | match := &ast.MatchExpr{ |
| 448 | Match: 1, |
| 449 | Scrutinee: &ast.RawExpr{ |
| 450 | Text: "status", |
| 451 | }, |
| 452 | Arms: []*ast.MatchArm{ |
| 453 | { |
| 454 | Pattern: &ast.ConstructorPattern{Name: "Pending"}, |
| 455 | Body: &ast.RawExpr{Text: `"waiting"`}, |
| 456 | }, |
| 457 | { |
| 458 | Pattern: &ast.ConstructorPattern{Name: "Active"}, |
| 459 | Body: &ast.RawExpr{Text: `"running"`}, |
| 460 | }, |
| 461 | { |
| 462 | Pattern: &ast.ConstructorPattern{Name: "Closed"}, |
| 463 | Body: &ast.RawExpr{Text: `"done"`}, |
| 464 | }, |
| 465 | }, |
| 466 | IsExpr: false, |
| 467 | } |
| 468 | |
| 469 | gen := &MatchCodeGen{ |
| 470 | BaseGenerator: NewBaseGenerator(), |
| 471 | Match: match, |
| 472 | ValueEnumReg: registry, |
| 473 | } |
| 474 | |
| 475 | result := gen.Generate() |
| 476 | code := string(result.Output) |
| 477 | |
| 478 | // Should use value switch (NOT type switch) |
| 479 | if strings.Contains(code, ".(type)") { |
| 480 | t.Errorf("Value enum should use value switch, not type switch. Got: %s", code) |
| 481 | } |
| 482 | |
| 483 | // Should have prefixed const names |
| 484 | if !strings.Contains(code, "case StatusPending:") { |
| 485 | t.Errorf("Expected 'case StatusPending:', got: %s", code) |
| 486 | } |
| 487 | if !strings.Contains(code, "case StatusActive:") { |
| 488 | t.Errorf("Expected 'case StatusActive:', got: %s", code) |
| 489 | } |
| 490 | if !strings.Contains(code, "case StatusClosed:") { |
| 491 | t.Errorf("Expected 'case StatusClosed:', got: %s", code) |
| 492 | } |
| 493 | |
| 494 | // Should have body expressions |
| 495 | if !strings.Contains(code, `"waiting"`) { |
| 496 | t.Errorf("Expected body \"waiting\", got: %s", code) |
| 497 | } |
| 498 | } |
| 499 |
nothing calls this directly
no test coverage detected