package sites import ( "os" "path/filepath" "slices" "strings" "testing" ) // Files is a small site built from sections, for tests. var Files = map[string]string{ "README.md": "# Site\n\n| Type | Fields |\n|---|---|\n| `hero` | `title`, `subhead`, `ctas` |\n\n- `faq` — questions: `items` of `q` + `a`\n", "layouts/partials/render-sections.html": `{{ range .Params.sections }}{{ if eq .type "hero" }}{{ partial "sections/hero.html" . }}{{ else }}{{ partial "sections/wrapper.html" . }}{{ end }}{{ end }}`, "layouts/partials/sections/wrapper.html": `
{{ partial (printf "sections/%s.html" .type) . }}
`, "layouts/partials/sections/head.html": `{{/* the heading: .ignored */}}{{ with .kicker }}

{{ . }}

{{ end }}{{ with .headline }}

{{ . }}

{{ end }}`, "layouts/partials/sections/ctas.html": `{{ range . }}{{ .label }}{{ end }}`, "layouts/partials/sections/hero.html": `

{{ .title }}

{{ .subhead }}

{{ partial "sections/ctas.html" .ctas }}`, "layouts/partials/sections/faq.html": "{{ $s := . }}{{ partial \"sections/head.html\" $s }}{{ range $s.items }}

{{ .q }}

{{ .a }}{{ end }}", "layouts/partials/sections/stats.html": "{{ $s := . }}{{ range $s.items }}{{ .value }}{{ end }}", "content/_index.md": "---\ntitle: Home\nsections:\n - type: hero\n title: Hello\n subhead: There\n ctas:\n - label: Start\n url: /start/\n - type: faq\n headline: Questions\n items:\n - q: Why?\n a: Because.\n---\n", "content/pricing.md": "---\ntitle: Pricing\nsections:\n - type: hero\n title: Prices\n - type: widget\n size: 3\n---\n", "content/blog/post.md": "+++\ntitle = \"A post\"\n+++\nHello.\n", "content/notes.md": "---\ntitle: Notes\ndraft: true\n---\nJust text.\n", } func write(t *testing.T, files map[string]string) string { dir := t.TempDir() for name, body := range files { p := filepath.Join(dir, name) os.MkdirAll(filepath.Dir(p), 0o755) if err := os.WriteFile(p, []byte(body), 0o644); err != nil { t.Fatal(err) } } return dir } func path(rel string) string { rel = strings.TrimSuffix(filepath.ToSlash(rel), ".md") rel = strings.Trim(strings.TrimSuffix(rel, "_index"), "/") if rel == "" { return "/" } return "/" + rel + "/" } func TestRead(t *testing.T) { c, err := Read(write(t, Files), path) if err != nil { t.Fatal(err) } var names []string for _, ty := range c.Types { names = append(names, ty.Name) } // Most used first; stats has a layout but no page uses it yet; the // widget has no layout. if want := []string{"hero", "faq", "widget", "stats"}; !slices.Equal(names, want) { t.Errorf("types: %v, want %v", names, want) } if want := []string{"ctas", "head", "wrapper"}; !slices.Equal(c.Helpers, want) { t.Errorf("helpers: %v", c.Helpers) } hero := c.Type("hero") if hero.Uses != 2 || !slices.Equal(hero.Pages, []string{"/", "/pricing/"}) || hero.Doc != "`title`, `subhead`, `ctas`" { t.Errorf("hero: %+v", hero) } if hero.Fields[0].Name != "title" || hero.Fields[0].Count != 2 { t.Errorf("hero fields: %+v", hero.Fields) } for _, f := range hero.Fields { if f.Name == "ctas" && !slices.Equal(f.Items, []string{"label", "url"}) { t.Errorf("ctas items: %v", f.Items) } } // ctas is handed .ctas, not the section, so its fields aren't the hero's. if !slices.Equal(hero.Layout, []string{"ctas", "subhead", "title"}) { t.Errorf("hero layout: %v", hero.Layout) } faq := c.Type("faq") if faq.Doc != "questions: `items` of `q` + `a`" || !slices.Equal(faq.Layout, []string{"headline", "items", "kicker"}) { t.Errorf("faq: %+v", faq) } if !strings.HasPrefix(faq.Example, "- type: faq\n headline: Questions") || !strings.Contains(faq.Example, "- q: Why?") { t.Errorf("faq example: %s", faq.Example) } if w := c.Type("widget"); !w.Missing || c.Has("widget") || !c.Has("stats") || c.Type("stats").Uses != 0 { t.Errorf("widget: %+v", w) } if len(c.Pages) != 4 || c.Pages[1].Path != "/blog/post/" || c.Pages[1].Title != "A post" { t.Fatalf("pages: %+v", c.Pages) } home := c.Pages[0] if home.Path != "/" || home.Title != "Home" || !home.Sections || !slices.Equal(home.Types, []string{"hero", "faq"}) || home.File != "content/_index.md" { t.Errorf("home: %+v", home) } for _, p := range c.Pages { if p.Path == "/notes/" && (!p.Draft || p.Sections) { t.Errorf("notes: %+v", p) } } } func TestReadEmpty(t *testing.T) { c, err := Read(t.TempDir(), path) if err != nil || len(c.Types) != 0 || len(c.Pages) != 0 { t.Errorf("%+v %v", c, err) } }