all repos — searchix @ main

Search engine for NixOS, nix-darwin, home-manager and NUR users

internal/nixdocs/optlink/optlink_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
package optlink

import (
	"bytes"
	"testing"

	"github.com/yuin/goldmark"
)

func TestOptLinkExtension(t *testing.T) {
	markdown := goldmark.New(
		goldmark.WithExtensions(
			New(),
		),
	)

	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:  "basic option link",
			input: "[](#opt-systemd.sysusers.enable)",
			expected: "<p><a class=\"option\" href=\"/?query=systemd.sysusers.enable\">" +
				"systemd.sysusers.enable</a></p>\n",
		},
		{
			name:  "option link in sentence",
			input: "You can enable this with [](#opt-systemd.sysusers.enable) in your configuration.",
			expected: "<p>You can enable this with <a class=\"option\" href=\"/?query=systemd.sysusers.enable\">" +
				"systemd.sysusers.enable</a> in your configuration.</p>\n",
		},
		{
			name:  "multiple option links",
			input: "Both [](#opt-systemd.sysusers.enable) and [](#opt-networking.firewall.enable) can be used.",
			expected: "<p>Both <a class=\"option\" href=\"/?query=systemd.sysusers.enable\">" +
				"systemd.sysusers.enable</a> and <a class=\"option\" href=\"/?query=networking.firewall.enable\">" +
				"networking.firewall.enable</a> can be used.</p>\n",
		},
		{
			name:     "not an option link - normal link",
			input:    "[regular link](https://example.com)",
			expected: "<p><a href=\"https://example.com\">regular link</a></p>\n",
		},
		{
			name:     "not an option link - empty link with different format",
			input:    "[](#other-prefix)",
			expected: "<p><a href=\"#other-prefix\"></a></p>\n",
		},
		{
			name:  "option link with special characters",
			input: "[](#opt-users.users.<n>.hashedPassword)",
			expected: "<p><a class=\"option\" href=\"/?query=users.users.%3Cn%3E.hashedPassword\">" +
				"users.users.&lt;n&gt;.hashedPassword</a></p>\n",
		},
		{
			name:  "option link with normal link in sentence",
			input: "Use [](#opt-networking.firewall.enable) and see [the docs](https://nixos.org) for more information.",
			expected: "<p>Use <a class=\"option\" href=\"/?query=networking.firewall.enable\">" +
				"networking.firewall.enable</a> and see <a href=\"https://nixos.org\">the docs</a> for more information.</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 TestOptLinkNodeKind(t *testing.T) {
	n := &Node{}
	if kind := n.Kind(); kind != KindOptLink {
		t.Errorf("Expected node kind %v, got %v", KindOptLink, kind)
	}
	if !n.Inline() {
		t.Error("Expected node to be inline")
	}
}