Review fixes: ordered doc-snippet check, one page and doc per ID, fade test clock, listen-address docs, false placeholder gap, Today param

This commit is contained in:
ZacharyZhang-NY committed 2026-10-05 14:14:38 -04:00
1 parent 3e011fa47f
commit af13fe3742
10 files changed
+98 -69

No files matched your search

+79 -57
View File
@@ -14,71 +14,93 @@ var (
docsPageID = regexp.MustCompile(`ID: "(\d{3})"`)
)
// TestDocSnippetsAreCompiledExamples keeps every doc's first Go block inside a compiled Example.
// docsLines normalizes code into its non-empty, non-comment lines without whitespace.
func docsLines(code string) []string {
var out []string
for _, l := range strings.Split(code, "\n") {
s := strings.TrimSpace(l)
if s != "" && !strings.HasPrefix(s, "//") {
out = append(out, docsSpace.ReplaceAllString(s, ""))
}
}
return out
}
// docsContains reports whether want appears in order inside have.
func docsContains(have, want []string) bool {
i := 0
for _, h := range have {
if i < len(want) && h == want[i] {
i++
}
}
return i == len(want)
}
func readGlob(t *testing.T, pattern string) map[string]string {
t.Helper()
files, err := filepath.Glob(pattern)
if err != nil {
t.Fatal(err)
}
out := map[string]string{}
for _, f := range files {
b, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
out[f] = string(b)
}
return out
}
// TestDocSnippetsAreCompiledExamples keeps each doc's first Go block, in order, inside one Example.
func TestDocSnippetsAreCompiledExamples(t *testing.T) {
examples, err := filepath.Glob("example_*_test.go")
if err != nil {
t.Fatal(err)
}
var all strings.Builder
for _, f := range examples {
b, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
all.Write(b)
}
compiled := docsSpace.ReplaceAllString(all.String(), "")
docs, err := filepath.Glob("docs/components/*.md")
if err != nil {
t.Fatal(err)
}
pages, err := filepath.Glob("examples/gallery/pages_*.go")
if err != nil {
t.Fatal(err)
}
ids := map[string]bool{}
for _, f := range pages {
b, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
for _, m := range docsPageID.FindAllSubmatch(b, -1) {
ids[string(m[1])] = true
var examples [][]string
for _, src := range readGlob(t, "example_*_test.go") {
for _, fn := range strings.Split(src, "\nfunc Example")[1:] {
examples = append(examples, docsLines(fn))
}
}
documented := map[string]bool{}
for _, d := range docs {
documented[filepath.Base(d)[:3]] = true
}
for id := range ids {
if !documented[id] {
t.Errorf("gallery page %s has no doc", id)
}
}
for id := range documented {
if !ids[id] {
t.Errorf("doc %s has no gallery page", id)
}
}
for _, d := range docs {
b, err := os.ReadFile(d)
if err != nil {
t.Fatal(err)
}
m := docsGoBlock.FindSubmatch(b)
for d, src := range readGlob(t, "docs/components/*.md") {
m := docsGoBlock.FindStringSubmatch(src)
if m == nil {
t.Errorf("%s has no Go example", d)
continue
}
for _, line := range strings.Split(string(m[1]), "\n") {
s := strings.TrimSpace(line)
if s == "" || strings.HasPrefix(s, "//") {
continue
}
if !strings.Contains(compiled, docsSpace.ReplaceAllString(s, "")) {
t.Errorf("%s: %q is not in any compiled Example", d, s)
want, found := docsLines(m[1]), false
for _, ex := range examples {
if docsContains(ex, want) {
found = true
break
}
}
if !found {
t.Errorf("%s: its Go block is not, in order, inside any compiled Example", d)
}
}
}
// TestDocsMatchGalleryPages wants exactly one doc and one gallery page per component ID.
func TestDocsMatchGalleryPages(t *testing.T) {
pages := map[string]int{}
for _, src := range readGlob(t, "examples/gallery/pages_*.go") {
for _, m := range docsPageID.FindAllStringSubmatch(src, -1) {
pages[m[1]]++
}
}
docs := map[string]int{}
for d := range readGlob(t, "docs/components/*.md") {
docs[filepath.Base(d)[:3]]++
}
for id, n := range pages {
if n != 1 || docs[id] != 1 {
t.Errorf("component %s: %d gallery pages, %d docs; want 1 and 1", id, n, docs[id])
}
}
for id, n := range docs {
if pages[id] == 0 {
t.Errorf("doc %s (%d files) has no gallery page", id, n)
}
}
}