internal/nixdocs/manpage/manpage_test.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | package manpage
import (
"bytes"
"testing"
"github.com/yuin/goldmark"
)
func TestManPageExtension(t *testing.T) {
markdown := goldmark.New(
goldmark.WithExtensions(
New(),
),
)
tests := []struct {
name string
input string
expected string
}{
{
name: "basic manpage reference",
input: "{manpage}`nix.conf(5)`",
expected: "<p><a class=\"manpage\" href=\"/man/5/nix.conf\">nix.conf(5)</a></p>\n",
},
{
name: "manpage reference in sentence",
input: "See {manpage}`git(1)` for more information.",
expected: "<p>See <a class=\"manpage\" href=\"/man/1/git\">git(1)</a> for more information.</p>\n",
},
{
name: "multiple manpage references",
input: "Both {manpage}`ssh(1)` and {manpage}`sshd(8)` have configuration options.",
expected: "<p>Both <a class=\"manpage\" href=\"/man/1/ssh\">ssh(1)</a> and " +
"<a class=\"manpage\" href=\"/man/8/sshd\">sshd(8)</a> have configuration options.</p>\n",
},
{
name: "incomplete manpage reference - no closing backtick",
input: "{manpage}`missing.backtick",
expected: "<p>{manpage}`missing.backtick</p>\n",
},
{
name: "incomplete manpage reference - empty reference",
input: "{manpage}``",
expected: "<p>{manpage}``</p>\n",
},
{
name: "invalid manpage reference - no section",
input: "{manpage}`nixos`",
expected: "<p>nixos</p>\n",
},
{
name: "invalid manpage reference - invalid section format",
input: "{manpage}`bash(x)`",
expected: "<p>bash(x)</p>\n",
},
{
name: "manpage reference with code block",
input: "Configure {manpage}`httpd.conf(5)` in your `apache` directory.",
expected: "<p>Configure <a class=\"manpage\" href=\"/man/5/httpd.conf\">httpd.conf(5)</a> " +
"in your <code>apache</code> directory.</p>\n",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var buf bytes.Buffer
if err := markdown.Convert([]byte(test.input), &buf); err != nil {
t.Fatalf("Failed to convert markdown: %v", err)
}
if got := buf.String(); got != test.expected {
t.Errorf("Expected:\n%q\nGot:\n%q", test.expected, got)
}
})
}
}
func TestManPageNodeKind(t *testing.T) {
n := &Node{}
if kind := n.Kind(); kind != KindManPage {
t.Errorf("Expected node kind %v, got %v", KindManPage, kind)
}
if !n.Inline() {
t.Error("Expected node to be inline")
}
}
|