all repos — searchix @ cb907ad36521036db199a226f3e357ad3b7b0a1c

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

internal/nixdocs/command/command_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
package command

import (
	"bytes"
	"testing"

	"github.com/yuin/goldmark"
)

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

	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:     "basic command reference",
			input:    "{command}`git clone`",
			expected: "<p><span class=\"command\"><code>git clone</code></span></p>\n",
		},
		{
			name:     "command reference in sentence",
			input:    "You can use {command}`mkdir -p` to create nested directories.",
			expected: "<p>You can use <span class=\"command\"><code>mkdir -p</code></span> to create nested directories.</p>\n",
		},
		{
			name:  "multiple command references",
			input: "Both {command}`ls -la` and {command}`find . -name \"*.go\"` can be used.",
			expected: "<p>Both <span class=\"command\"><code>ls -la</code></span> and <span class=\"command\">" +
				"<code>find . -name \"*.go\"</code></span> can be used.</p>\n",
		},
		{
			name:     "incomplete command reference - no closing backtick",
			input:    "{command}`missingBacktick",
			expected: "<p>{command}`missingBacktick</p>\n",
		},
		{
			name:     "incomplete command reference - empty command name",
			input:    "{command}``",
			expected: "<p>{command}``</p>\n",
		},
		{
			name:  "command reference with code block",
			input: "{command}`grep -r` is used in `terminal.sh`",
			expected: "<p><span class=\"command\"><code>grep -r</code></span> " +
				"is used in <code>terminal.sh</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 TestCommandNodeKind(t *testing.T) {
	n := &Node{}
	if kind := n.Kind(); kind != KindCommand {
		t.Errorf("Expected node kind %v, got %v", KindCommand, kind)
	}
	if !n.Inline() {
		t.Error("Expected node to be inline")
	}
}