|
| 1 | +package ast |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// TestEnumParser_TypePrefixes covers parseTypeExpr's handling of pointer (*), |
| 9 | +// slice ([]), and any nesting/order of the two. The original implementation |
| 10 | +// accepted only `**T` or `*[]T`-shaped prefixes (pointers first, then a single |
| 11 | +// slice), which silently rejected `[]*T`, `[][]T`, `[]*[]T`, etc. The |
| 12 | +// transpilation pipeline turned that rejection into a confusing |
| 13 | +// "expected declaration, found enum" error downstream. |
| 14 | +func TestEnumParser_TypePrefixes(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + src string |
| 18 | + wantType string // expected Text of the parsed field type |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "plain type", |
| 22 | + src: `enum E { V { x: int } }`, |
| 23 | + wantType: "int", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "pointer", |
| 27 | + src: `enum E { V { x: *Foo } }`, |
| 28 | + wantType: "*Foo", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "double pointer", |
| 32 | + src: `enum E { V { x: **Foo } }`, |
| 33 | + wantType: "**Foo", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "slice of value", |
| 37 | + src: `enum E { V { x: []Foo } }`, |
| 38 | + wantType: "[]Foo", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "slice of pointer", |
| 42 | + src: `enum E { V { x: []*Foo } }`, |
| 43 | + wantType: "[]*Foo", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "pointer to slice", |
| 47 | + src: `enum E { V { x: *[]Foo } }`, |
| 48 | + wantType: "*[]Foo", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "slice of slice", |
| 52 | + src: `enum E { V { x: [][]int } }`, |
| 53 | + wantType: "[][]int", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "slice of slice of pointer", |
| 57 | + src: `enum E { V { x: [][]*Foo } }`, |
| 58 | + wantType: "[][]*Foo", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "generic with slice arg", |
| 62 | + src: `enum E { V { x: Result[[]int, error] } }`, |
| 63 | + wantType: "Result[[]int, error]", |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + p := NewEnumParser([]byte(tt.src), 0) |
| 70 | + decl, _, err := p.ParseEnumDecl() |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("ParseEnumDecl(%q) returned error: %v", tt.src, err) |
| 73 | + } |
| 74 | + if len(decl.Variants) != 1 { |
| 75 | + t.Fatalf("expected 1 variant, got %d", len(decl.Variants)) |
| 76 | + } |
| 77 | + v := decl.Variants[0] |
| 78 | + if v.Kind != StructVariant { |
| 79 | + t.Fatalf("expected struct variant, got %v", v.Kind) |
| 80 | + } |
| 81 | + if len(v.Fields) != 1 { |
| 82 | + t.Fatalf("expected 1 field, got %d", len(v.Fields)) |
| 83 | + } |
| 84 | + got := v.Fields[0].Type.Text |
| 85 | + if got != tt.wantType { |
| 86 | + t.Errorf("field type = %q, want %q", got, tt.wantType) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// TestEnumParser_TupleTypePrefixes covers the same prefix handling in tuple |
| 93 | +// variant fields. parseTupleFields shares parseTypeExpr with parseStructFields, |
| 94 | +// so a regression in either context surfaces here too. |
| 95 | +func TestEnumParser_TupleTypePrefixes(t *testing.T) { |
| 96 | + tests := []struct { |
| 97 | + name string |
| 98 | + src string |
| 99 | + wantTypes []string |
| 100 | + }{ |
| 101 | + { |
| 102 | + name: "single slice-of-pointer", |
| 103 | + src: `enum E { V([]*Foo) }`, |
| 104 | + wantTypes: []string{"[]*Foo"}, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "mixed prefixes", |
| 108 | + src: `enum E { V(*Foo, []*Bar, [][]int) }`, |
| 109 | + wantTypes: []string{"*Foo", "[]*Bar", "[][]int"}, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + for _, tt := range tests { |
| 114 | + t.Run(tt.name, func(t *testing.T) { |
| 115 | + p := NewEnumParser([]byte(tt.src), 0) |
| 116 | + decl, _, err := p.ParseEnumDecl() |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("ParseEnumDecl(%q) returned error: %v", tt.src, err) |
| 119 | + } |
| 120 | + if len(decl.Variants) != 1 { |
| 121 | + t.Fatalf("expected 1 variant, got %d", len(decl.Variants)) |
| 122 | + } |
| 123 | + v := decl.Variants[0] |
| 124 | + if v.Kind != TupleVariant { |
| 125 | + t.Fatalf("expected tuple variant, got %v", v.Kind) |
| 126 | + } |
| 127 | + if len(v.Fields) != len(tt.wantTypes) { |
| 128 | + t.Fatalf("expected %d fields, got %d", len(tt.wantTypes), len(v.Fields)) |
| 129 | + } |
| 130 | + for i, want := range tt.wantTypes { |
| 131 | + got := v.Fields[i].Type.Text |
| 132 | + if got != want { |
| 133 | + t.Errorf("field %d type = %q, want %q", i, got, want) |
| 134 | + } |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// TestTransformSource_SliceOfPointer is an end-to-end regression test: |
| 141 | +// before the fix, a `[]*T` field on an enum variant caused the dingo→go |
| 142 | +// transformation to leave the `enum` keyword in the output, producing a |
| 143 | +// downstream go/parser error of the form |
| 144 | +// |
| 145 | +// "expected declaration, found enum" |
| 146 | +// |
| 147 | +// After the fix the source is transformed cleanly and the generated Go |
| 148 | +// contains the variant struct with the correct slice-of-pointer field. |
| 149 | +func TestTransformSource_SliceOfPointer(t *testing.T) { |
| 150 | + src := []byte(`package main |
| 151 | +
|
| 152 | +enum D { Foo { xs: []*Name, n: int } } |
| 153 | +
|
| 154 | +type Name struct { v string } |
| 155 | +`) |
| 156 | + got, err := TransformSource(src, "test.dingo") |
| 157 | + if err != nil { |
| 158 | + t.Fatalf("TransformSource returned error: %v", err) |
| 159 | + } |
| 160 | + out := string(got) |
| 161 | + if strings.Contains(out, "enum ") { |
| 162 | + t.Errorf("transformed output still contains 'enum' keyword:\n%s", out) |
| 163 | + } |
| 164 | + // The variant struct should carry the field with its original slice-of-pointer type. |
| 165 | + if !strings.Contains(out, "xs []*Name") { |
| 166 | + t.Errorf("expected `xs []*Name` field in generated Go, got:\n%s", out) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +// TestTransformSource_MultiLineVariantBody confirms that a struct variant |
| 171 | +// whose fields are newline-separated (no trailing commas) still parses. |
| 172 | +// This was suspected to be broken alongside the slice-of-pointer bug but is |
| 173 | +// actually fine; this test guards against future regressions in |
| 174 | +// parseStructFields, which relies on skipWhitespaceAndCommas treating |
| 175 | +// newlines as separators. |
| 176 | +func TestTransformSource_MultiLineVariantBody(t *testing.T) { |
| 177 | + src := []byte(`package main |
| 178 | +
|
| 179 | +enum D { Foo { |
| 180 | + a: int |
| 181 | + b: *Name |
| 182 | + c: []*Name |
| 183 | +} } |
| 184 | +
|
| 185 | +type Name struct { v string } |
| 186 | +`) |
| 187 | + got, err := TransformSource(src, "test.dingo") |
| 188 | + if err != nil { |
| 189 | + t.Fatalf("TransformSource returned error: %v", err) |
| 190 | + } |
| 191 | + out := string(got) |
| 192 | + if strings.Contains(out, "enum ") { |
| 193 | + t.Errorf("transformed output still contains 'enum' keyword:\n%s", out) |
| 194 | + } |
| 195 | + for _, want := range []string{"a int", "b *Name", "c []*Name"} { |
| 196 | + if !strings.Contains(out, want) { |
| 197 | + t.Errorf("expected %q in generated Go, got:\n%s", want, out) |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments