package sites import ( "strings" "testing" ) const pageFile = `--- title: "FreeScout" description: Help desk you own sections: - type: hero heading: FreeScout, hosted - type: text body: | Two lines of text. - type: final-cta heading: Start now menu: main --- Body text. ` func TestInsertSection(t *testing.T) { yamlText, types, err := PageSections([]byte(pageFile)) if err != nil || strings.Join(types, ",") != "hero,text,final-cta" || !strings.Contains(yamlText, "FreeScout, hosted") { t.Fatalf("sections: %v %q %v", types, yamlText, err) } faq := "- type: faq\n heading: Questions\n items:\n - q: Is it hosted?\n a: Yes.\n" out, err := InsertSection([]byte(pageFile), faq, 2) if err != nil { t.Fatal(err) } got := string(out) want := " of text.\n - type: faq\n heading: Questions\n items:\n - q: Is it hosted?\n a: Yes.\n - type: final-cta\n" if !strings.Contains(got, want) || !strings.HasSuffix(got, "menu: main\n---\nBody text.\n") || !strings.HasPrefix(got, "---\ntitle: \"FreeScout\"\n") { t.Errorf("inserted:\n%s", got) } if _, types, _ := PageSections(out); strings.Join(types, ",") != "hero,text,faq,final-cta" { t.Errorf("types after: %v", types) } // At the end: before the next key. out, err = InsertSection([]byte(pageFile), faq, 99) if _, types, _ := PageSections(out); err != nil || strings.Join(types, ",") != "hero,text,final-cta,faq" || !strings.Contains(string(out), " a: Yes.\nmenu: main\n") { t.Errorf("at the end: %v %v\n%s", types, err, out) } // Sections last in the front matter. last := "---\ntitle: X\nsections:\n- type: hero\n heading: Hi\n---\n" out, err = InsertSection([]byte(last), faq, 1) if _, types, _ := PageSections(out); err != nil || strings.Join(types, ",") != "hero,faq" || !strings.HasSuffix(string(out), " a: Yes.\n---\n") { t.Errorf("last: %v %v\n%s", types, err, out) } if _, err := InsertSection([]byte(pageFile), "- type: faq\n- type: text\n", 0); err == nil { t.Error("took two sections") } if _, err := InsertSection([]byte("+++\ntitle = 'x'\n+++\n"), faq, 0); err == nil { t.Error("took TOML") } }