|
| 1 | +package testing |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/amterp/rad/rts" |
| 10 | +) |
| 11 | + |
| 12 | +// TestFuncDocsValid verifies every docs/funcs/*.md file in the |
| 13 | +// embedded set parses cleanly through ParseFuncDoc. Catches |
| 14 | +// authors landing a malformed doc that the hover layer would |
| 15 | +// silently drop. |
| 16 | +func TestFuncDocsValid(t *testing.T) { |
| 17 | + names := rts.FuncDocNames() |
| 18 | + if len(names) == 0 { |
| 19 | + t.Skip("no embedded func docs yet") |
| 20 | + } |
| 21 | + for _, name := range names { |
| 22 | + name := name |
| 23 | + t.Run(name, func(t *testing.T) { |
| 24 | + doc := rts.GetFuncDoc(name) |
| 25 | + if doc == nil { |
| 26 | + t.Fatalf("FuncDocNames listed %q but GetFuncDoc returned nil", name) |
| 27 | + } |
| 28 | + if doc.Name != name { |
| 29 | + t.Errorf("name mismatch: stem=%q, doc.Name=%q", name, doc.Name) |
| 30 | + } |
| 31 | + if doc.Signature == "" { |
| 32 | + t.Errorf("%s: empty signature", name) |
| 33 | + } |
| 34 | + if !strings.Contains(doc.Signature, name+"(") { |
| 35 | + t.Errorf("%s: signature %q doesn't start with the function name", |
| 36 | + name, doc.Signature) |
| 37 | + } |
| 38 | + if len(doc.Examples) == 0 { |
| 39 | + t.Errorf("%s: no example code blocks", name) |
| 40 | + } |
| 41 | + if doc.Category == "" { |
| 42 | + t.Errorf("%s: empty category", name) |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// TestFuncDocsMatchRegisteredBuiltins verifies that every embedded |
| 49 | +// doc names a function the runtime actually registers. Catches the |
| 50 | +// reverse-drift case: a doc author renames the file to `say.md` |
| 51 | +// while the runtime still exposes `print`. |
| 52 | +// |
| 53 | +// Note: the opposite assertion - every registered builtin has a |
| 54 | +// doc - is intentionally NOT in this test yet. The doc migration |
| 55 | +// is incremental; gating CI on 100% coverage would block landing |
| 56 | +// any incremental work. The completeness assertion will land once |
| 57 | +// the migration finishes. |
| 58 | +func TestFuncDocsMatchRegisteredBuiltins(t *testing.T) { |
| 59 | + for _, name := range rts.FuncDocNames() { |
| 60 | + if _, ok := rts.FnSignaturesByName[name]; !ok { |
| 61 | + t.Errorf("doc exists for %q but no such builtin is registered", name) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// TestFuncDocsSignatureMatchesRegistered verifies the signature |
| 67 | +// line in each doc matches the registered builtin's signature |
| 68 | +// byte-for-byte. Catches the case where a doc author updates the |
| 69 | +// signature in source without updating the doc (or vice versa). |
| 70 | +func TestFuncDocsSignatureMatchesRegistered(t *testing.T) { |
| 71 | + for _, name := range rts.FuncDocNames() { |
| 72 | + doc := rts.GetFuncDoc(name) |
| 73 | + sig, ok := rts.FnSignaturesByName[name] |
| 74 | + if !ok { |
| 75 | + continue // covered by TestFuncDocsMatchRegisteredBuiltins |
| 76 | + } |
| 77 | + if doc.Signature != sig.Signature { |
| 78 | + t.Errorf("%s: doc signature %q != registered %q", |
| 79 | + name, doc.Signature, sig.Signature) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// TestFuncDocsSourceMatchesEmbedded verifies the source docs at |
| 85 | +// docs/funcs/ are in sync with the embedded copy in |
| 86 | +// rts/embedded_funcs/. The embedded files are the artefact the |
| 87 | +// runtime actually reads; the docs/funcs/ tree is the canonical |
| 88 | +// editable source. Manual sync today; codegen later. |
| 89 | +func TestFuncDocsSourceMatchesEmbedded(t *testing.T) { |
| 90 | + sourceDir := "../../docs/funcs" |
| 91 | + embeddedDir := "../../rts/embedded_funcs" |
| 92 | + if _, err := os.Stat(sourceDir); os.IsNotExist(err) { |
| 93 | + t.Skipf("source dir %s missing", sourceDir) |
| 94 | + } |
| 95 | + if _, err := os.Stat(embeddedDir); os.IsNotExist(err) { |
| 96 | + t.Skipf("embedded dir %s missing", embeddedDir) |
| 97 | + } |
| 98 | + |
| 99 | + sources, err := collectDocSet(sourceDir) |
| 100 | + if err != nil { |
| 101 | + t.Fatalf("read source: %v", err) |
| 102 | + } |
| 103 | + embedded, err := collectDocSet(embeddedDir) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("read embedded: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + for name, content := range sources { |
| 109 | + if name == "README.md" { |
| 110 | + continue |
| 111 | + } |
| 112 | + emb, ok := embedded[name] |
| 113 | + if !ok { |
| 114 | + t.Errorf("%s exists in docs/funcs/ but not in rts/embedded_funcs/", name) |
| 115 | + continue |
| 116 | + } |
| 117 | + if string(content) != string(emb) { |
| 118 | + t.Errorf("%s differs between docs/funcs/ and rts/embedded_funcs/", name) |
| 119 | + } |
| 120 | + } |
| 121 | + for name := range embedded { |
| 122 | + if _, ok := sources[name]; !ok { |
| 123 | + t.Errorf("%s exists in rts/embedded_funcs/ but not in docs/funcs/", name) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func collectDocSet(dir string) (map[string][]byte, error) { |
| 129 | + out := make(map[string][]byte) |
| 130 | + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + base := filepath.Base(path) |
| 135 | + if info.IsDir() || !strings.HasSuffix(base, ".md") { |
| 136 | + return nil |
| 137 | + } |
| 138 | + content, err := os.ReadFile(path) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + out[base] = content |
| 143 | + return nil |
| 144 | + }) |
| 145 | + return out, err |
| 146 | +} |
0 commit comments