|
| 1 | +package printer |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/withastro/compiler/internal/test_utils" |
| 8 | +) |
| 9 | + |
| 10 | +type paramsTestcase struct { |
| 11 | + name string |
| 12 | + want string |
| 13 | +} |
| 14 | + |
| 15 | +func TestUtilParamsType(t *testing.T) { |
| 16 | + tests := []paramsTestcase{ |
| 17 | + { |
| 18 | + name: "/src/pages/index.astro", |
| 19 | + want: `Record<string, string | number>`, |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "/src/pages/blog/[slug].astro", |
| 23 | + want: `Record<"slug", string | number>`, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "/src/pages/[lang]/blog/[slug].astro", |
| 27 | + want: `Record<"lang" | "slug", string | number>`, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "/src/pages/[...fallback].astro", |
| 31 | + want: `Record<"fallback", string | number>`, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "/src/pages/[year]-[month]-[day]/[post].astro", |
| 35 | + want: `Record<"year" | "month" | "day" | "post", string | number>`, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "/src/pages/post-[id]/[post].astro", |
| 39 | + want: `Record<"id" | "post", string | number>`, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + result := getParamsTypeFromFilename(tt.name) |
| 46 | + // compare to expected string, show diff if mismatch |
| 47 | + if diff := test_utils.ANSIDiff(strings.TrimSpace(tt.want), strings.TrimSpace(string(result))); diff != "" { |
| 48 | + t.Errorf("mismatch (-want +got):\n%s", diff) |
| 49 | + } |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
0 commit comments