internal/nixdocs/filepath/filepath_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 | package filepath
import (
"bytes"
"testing"
"github.com/yuin/goldmark"
)
func TestFilePathExtension(t *testing.T) {
markdown := goldmark.New(
goldmark.WithExtensions(
New(),
),
)
tests := []struct {
name string
input string
expected string
}{
{
name: "basic file path reference",
input: "{file}`/etc/passwd`",
expected: "<p><span class=\"file-path\">/etc/passwd</span></p>\n",
},
{
name: "file path reference in sentence",
input: "The configuration file {file}`/etc/fstab` contains file system information.",
expected: "<p>The configuration file <span class=\"file-path\">/etc/fstab</span>" +
" contains file system information.</p>\n",
},
{
name: "multiple file path references",
input: "Both {file}`/etc/hosts` and {file}`/etc/resolv.conf` define DNS settings.",
expected: "<p>Both <span class=\"file-path\">/etc/hosts</span> and <span class=\"file-path\">" +
"/etc/resolv.conf</span> define DNS settings.</p>\n",
},
{
name: "incomplete file path reference - no closing backtick",
input: "{file}`/missing/backtick",
expected: "<p>{file}`/missing/backtick</p>\n",
},
{
name: "incomplete file path reference - empty path",
input: "{file}``",
expected: "<p>{file}``</p>\n",
},
{
name: "file path reference with code block",
input: "Edit {file}`~/.bashrc` using your favorite `text editor`.",
expected: "<p>Edit <span class=\"file-path\">~/.bashrc</span> " +
"using your favorite <code>text editor</code>.</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 TestFilePathNodeKind(t *testing.T) {
n := &Node{}
if kind := n.Kind(); kind != KindFilePath {
t.Errorf("Expected node kind %v, got %v", KindFilePath, kind)
}
if !n.Inline() {
t.Error("Expected node to be inline")
}
}
|