Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/go-test/deep" |
| 7 | ) |
| 8 | |
| 9 | func TestParse(t *testing.T) { |
| 10 | for _, test := range []struct { |
| 11 | name string |
| 12 | data string |
| 13 | want *configToml |
| 14 | }{ |
| 15 | { |
| 16 | name: "normal config", |
| 17 | data: ` |
| 18 | default_index = ["foo.md", "bar.md"] |
| 19 | [template.default] |
| 20 | sources = ["hackdoc/bar.html", "hackdoc/baz.html"] |
| 21 | [template.foo] |
| 22 | sources = ["foo/bar.html", "foo/baz.html"] |
| 23 | `, |
| 24 | want: &configToml{ |
| 25 | DefaultIndex: []string{"foo.md", "bar.md"}, |
| 26 | Templates: map[string]*configTomlTemplate{ |
| 27 | "default": &configTomlTemplate{ |
| 28 | Sources: []string{"hackdoc/bar.html", "hackdoc/baz.html"}, |
| 29 | }, |
| 30 | "foo": &configTomlTemplate{ |
| 31 | Sources: []string{"foo/bar.html", "foo/baz.html"}, |
| 32 | }, |
| 33 | }, |
| 34 | }, |
| 35 | }, { |
| 36 | name: "empty config", |
| 37 | data: "", |
| 38 | want: &configToml{ |
| 39 | DefaultIndex: nil, |
| 40 | Templates: map[string]*configTomlTemplate{}, |
| 41 | }, |
| 42 | }, |
| 43 | } { |
| 44 | t.Run(test.name, func(t *testing.T) { |
| 45 | got, err := parseToml([]byte(test.data)) |
| 46 | if err != nil { |
Serge Bazanski | 939eaaa | 2024-01-15 13:19:16 +0000 | [diff] [blame] | 47 | t.Fatalf("could not parse config: %v", err) |
Sergiusz Bazanski | c881cf3 | 2020-04-08 20:03:12 +0200 | [diff] [blame] | 48 | } |
| 49 | if diff := deep.Equal(test.want, got); diff != nil { |
| 50 | t.Fatal(diff) |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestLocations(t *testing.T) { |
| 57 | for _, test := range []struct { |
| 58 | name string |
| 59 | path string |
| 60 | want []string |
| 61 | }{ |
| 62 | { |
| 63 | name: "perforce-style path", |
| 64 | path: "//foo/bar/baz", |
| 65 | want: []string{"//hackdoc.toml", "//foo/hackdoc.toml", "//foo/bar/hackdoc.toml", "//foo/bar/baz/hackdoc.toml"}, |
| 66 | }, { |
| 67 | name: "unix-style path", |
| 68 | path: "/foo/bar/baz", |
| 69 | want: []string{"/hackdoc.toml", "/foo/hackdoc.toml", "/foo/bar/hackdoc.toml", "/foo/bar/baz/hackdoc.toml"}, |
| 70 | }, { |
| 71 | name: "relative-style path", |
| 72 | path: "foo/bar/baz", |
| 73 | want: []string{"hackdoc.toml", "foo/hackdoc.toml", "foo/bar/hackdoc.toml", "foo/bar/baz/hackdoc.toml"}, |
| 74 | }, { |
| 75 | name: "root perforce-style path", |
| 76 | path: "//", |
| 77 | want: []string{"//hackdoc.toml"}, |
| 78 | }, { |
| 79 | name: "root unix-style path", |
| 80 | path: "/", |
| 81 | want: []string{"/hackdoc.toml"}, |
| 82 | }, { |
| 83 | name: "empty path", |
| 84 | path: "", |
| 85 | want: []string{"hackdoc.toml"}, |
| 86 | }, { |
| 87 | name: "weird path", |
| 88 | path: "///what/is///this///", |
| 89 | want: nil, |
| 90 | }, |
| 91 | } { |
| 92 | t.Run(test.name, func(t *testing.T) { |
| 93 | got := configFileLocations(test.path) |
| 94 | if diff := deep.Equal(test.want, got); diff != nil { |
| 95 | t.Fatal(diff) |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |