all repos — searchix @ main

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

internal/nixdocs/option/option_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 option

import (
	"bytes"
	"testing"

	"github.com/yuin/goldmark"
)

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

	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:  "basic option reference",
			input: "{option}`initialHashedPassword`",
			expected: "<p><a class=\"option\" href=\"/?query=initialHashedPassword\">" +
				"<code>initialHashedPassword</code></a></p>\n",
		},
		{
			name:  "option reference in sentence",
			input: "You can set {option}`initialHashedPassword` to configure the initial password.",
			expected: "<p>You can set <a class=\"option\" href=\"/?query=initialHashedPassword\">" +
				"<code>initialHashedPassword</code></a> to configure the initial password.</p>\n",
		},
		{
			name:  "multiple option references",
			input: "Both {option}`initialHashedPassword` and {option}`passwordFile` can be used.",
			expected: "<p>Both <a class=\"option\" href=\"/?query=initialHashedPassword\">" +
				"<code>initialHashedPassword</code></a> and <a class=\"option\" href=\"/?query=passwordFile\">" +
				"<code>passwordFile</code></a> can be used.</p>\n",
		},
		{
			name:     "incomplete option reference - no closing backtick",
			input:    "{option}`missingBacktick",
			expected: "<p>{option}`missingBacktick</p>\n",
		},
		{
			name:     "incomplete option reference - empty option name",
			input:    "{option}``",
			expected: "<p>{option}``</p>\n",
		},
		{
			name:  "option reference with code block",
			input: "{option}`config` is set in `config.nix`",
			expected: "<p><a class=\"option\" href=\"/?query=config\"><code>config</code></a> " +
				"is set in <code>config.nix</code></p>\n",
		},
		{
			name:  "option reference with HTML-like content",
			input: "{option}`users.users.<name>.hashedPassword`",
			expected: "<p><a class=\"option\" href=\"/?query=users.users.%3Cname%3E.hashedPassword\">" +
				"<code>users.users.&lt;name&gt;.hashedPassword</code></a></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 TestOptionNodeKind(t *testing.T) {
	n := &Node{}
	if kind := n.Kind(); kind != KindOption {
		t.Errorf("Expected node kind %v, got %v", KindOption, kind)
	}
	if !n.Inline() {
		t.Error("Expected node to be inline")
	}
}