TestMatchCodeGen_LiteralPattern tests code generation for literal patterns.
(t *testing.T)
| 110 | |
| 111 | // TestMatchCodeGen_LiteralPattern tests code generation for literal patterns. |
| 112 | func TestMatchCodeGen_LiteralPattern(t *testing.T) { |
| 113 | match := &ast.MatchExpr{ |
| 114 | Match: 1, |
| 115 | Scrutinee: &ast.RawExpr{ |
| 116 | Text: "status", |
| 117 | }, |
| 118 | Arms: []*ast.MatchArm{ |
| 119 | { |
| 120 | Pattern: &ast.LiteralPattern{ |
| 121 | Value: "1", |
| 122 | Kind: ast.IntLiteral, |
| 123 | }, |
| 124 | Body: &ast.RawExpr{Text: "\"active\""}, |
| 125 | }, |
| 126 | { |
| 127 | Pattern: &ast.LiteralPattern{ |
| 128 | Value: "2", |
| 129 | Kind: ast.IntLiteral, |
| 130 | }, |
| 131 | Body: &ast.RawExpr{Text: "\"pending\""}, |
| 132 | }, |
| 133 | { |
| 134 | Pattern: &ast.WildcardPattern{}, |
| 135 | Body: &ast.RawExpr{Text: "\"unknown\""}, |
| 136 | }, |
| 137 | }, |
| 138 | IsExpr: false, |
| 139 | } |
| 140 | |
| 141 | gen := NewMatchCodeGen(match).(*MatchCodeGen) |
| 142 | result := gen.Generate() |
| 143 | |
| 144 | code := string(result.Output) |
| 145 | |
| 146 | // Check for value caching |
| 147 | if !strings.Contains(code, "val := status") { |
| 148 | t.Errorf("Expected value caching 'val := status', got: %s", code) |
| 149 | } |
| 150 | |
| 151 | // Check for switch statement using cached variable |
| 152 | if !strings.Contains(code, "switch val") { |
| 153 | t.Errorf("Expected switch statement using cached val, got: %s", code) |
| 154 | } |
| 155 | |
| 156 | // Check for case clauses |
| 157 | if !strings.Contains(code, "case 1:") { |
| 158 | t.Errorf("Expected 'case 1:', got: %s", code) |
| 159 | } |
| 160 | if !strings.Contains(code, "case 2:") { |
| 161 | t.Errorf("Expected 'case 2:', got: %s", code) |
| 162 | } |
| 163 | if !strings.Contains(code, "default:") { |
| 164 | t.Errorf("Expected 'default:', got: %s", code) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // TestMatchCodeGen_WildcardPattern tests wildcard pattern generation. |
| 169 | func TestMatchCodeGen_WildcardPattern(t *testing.T) { |
nothing calls this directly
no test coverage detected