TestMatchCodeGen_ConstructorPattern tests code generation for constructor patterns.
(t *testing.T)
| 9 | |
| 10 | // TestMatchCodeGen_ConstructorPattern tests code generation for constructor patterns. |
| 11 | func TestMatchCodeGen_ConstructorPattern(t *testing.T) { |
| 12 | tests := []struct { |
| 13 | name string |
| 14 | match *ast.MatchExpr |
| 15 | expectedPart string // Part of the output we expect to see |
| 16 | }{ |
| 17 | { |
| 18 | name: "Simple Ok/Err match", |
| 19 | match: &ast.MatchExpr{ |
| 20 | Match: 1, |
| 21 | Scrutinee: &ast.RawExpr{ |
| 22 | StartPos: 7, |
| 23 | EndPos: 13, |
| 24 | Text: "result", |
| 25 | }, |
| 26 | Arms: []*ast.MatchArm{ |
| 27 | { |
| 28 | Pattern: &ast.ConstructorPattern{ |
| 29 | Name: "Ok", |
| 30 | Params: []ast.Pattern{ |
| 31 | &ast.VariablePattern{Name: "value"}, |
| 32 | }, |
| 33 | }, |
| 34 | Body: &ast.RawExpr{Text: "value"}, |
| 35 | }, |
| 36 | { |
| 37 | Pattern: &ast.ConstructorPattern{ |
| 38 | Name: "Err", |
| 39 | Params: []ast.Pattern{ |
| 40 | &ast.VariablePattern{Name: "e"}, |
| 41 | }, |
| 42 | }, |
| 43 | Body: &ast.RawExpr{Text: "0"}, |
| 44 | }, |
| 45 | }, |
| 46 | IsExpr: false, |
| 47 | }, |
| 48 | expectedPart: "res.IsOk()", // NEW: method-based Result matching |
| 49 | }, |
| 50 | { |
| 51 | name: "Some/None match", |
| 52 | match: &ast.MatchExpr{ |
| 53 | Match: 1, |
| 54 | Scrutinee: &ast.RawExpr{ |
| 55 | Text: "opt", |
| 56 | }, |
| 57 | Arms: []*ast.MatchArm{ |
| 58 | { |
| 59 | Pattern: &ast.ConstructorPattern{ |
| 60 | Name: "Some", |
| 61 | Params: []ast.Pattern{ |
| 62 | &ast.VariablePattern{Name: "x"}, |
| 63 | }, |
| 64 | }, |
| 65 | Body: &ast.RawExpr{Text: "x"}, |
| 66 | }, |
| 67 | { |
| 68 | Pattern: &ast.ConstructorPattern{ |
nothing calls this directly
no test coverage detected