Skip to content
quarry Artifact [a08a5d1bad]
Login

Artifact [a08a5d1bad]

Artifact a08a5d1bad6e9d7a0c55a8cc5889b993b093616e8e64516c56d6ded3fa12c702:


package web

import "testing"

func contentsIDs(hs []wikiHeading) []string {
	var out []string
	for _, h := range hs {
		out = append(out, h.ID)
	}
	return out
}

// A README whose chapters are `####` and whose sections are `#####` (h5
// and h6 once the page's own h1 is accounted for) has no heading between
// those and its `##` parts: the rail must list the chapters.
func TestContentsListsLevelFourChapters(t *testing.T) {
	hs := []wikiHeading{
		{Level: 3, ID: "intro"},
		{Level: 5, ID: "chapter-a"}, {Level: 6, ID: "section-a1"},
		{Level: 5, ID: "chapter-b"}, {Level: 6, ID: "section-b1"},
	}
	got := wikiContents(hs)
	if len(got) != 5 {
		t.Fatalf("FAIL: rail has %d entries, want all 5: %v", len(got), contentsIDs(got))
	}
	for _, h := range got {
		if h.Sub != (h.Level > 3) {
			t.Errorf("FAIL: %s Sub=%v at level %d", h.ID, h.Sub, h.Level)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// With a level-3 heading (h4 rendered) the rail stops at level 4 (h5):
// level-5 sections are the body of a chapter, not navigation.
func TestContentsStopsAtLevelFourWhenThereAreLevelThrees(t *testing.T) {
	hs := []wikiHeading{
		{Level: 2, ID: "a"}, {Level: 3, ID: "b"}, {Level: 4, ID: "c"},
		{Level: 5, ID: "d"}, {Level: 6, ID: "too-deep"},
	}
	got := contentsIDs(wikiContents(hs))
	if len(got) != 4 || got[3] != "d" {
		t.Errorf("FAIL: rail = %v, want a b c d", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A long document gives up its deepest level before it is truncated.
func TestContentsDropsDeepestLevelPastTheLimit(t *testing.T) {
	var hs []wikiHeading
	for i := 0; i < 5; i++ {
		hs = append(hs, wikiHeading{Level: 3, ID: "part"})
		for j := 0; j < 10; j++ {
			hs = append(hs, wikiHeading{Level: 5, ID: "chapter"})
		}
	}
	got := wikiContents(hs)
	if len(got) != 5 {
		t.Errorf("FAIL: want the 5 parts once 55 entries is too long, got %d", len(got))
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestContentsTooFewHeadings(t *testing.T) {
	if got := wikiContents([]wikiHeading{{Level: 2}, {Level: 3}}); got != nil {
		t.Errorf("FAIL: two headings earned a rail: %v", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}