MCPcopy
hub / github.com/MadAppGang/dingo / TestMatchCodeGen_ValueEnumCodeGen

Function TestMatchCodeGen_ValueEnumCodeGen

pkg/codegen/match_test.go:442–498  ·  view source on GitHub ↗

TestMatchCodeGen_ValueEnumCodeGen tests code generation for value enum match.

(t *testing.T)

Source from the content-addressed store, hash-verified

440
441// TestMatchCodeGen_ValueEnumCodeGen tests code generation for value enum match.
442func 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

Callers

nothing calls this directly

Calls 6

RegisterValueEnumMethod · 0.95
GenerateMethod · 0.95
NewEnumRegistryFunction · 0.92
NewBaseGeneratorFunction · 0.85
ContainsMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected