summaryrefslogtreecommitdiffstats
path: root/utils/markdown/text_range_test.go
blob: de3dd09850ac5934843d189d2172023717ffb171 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package markdown

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestTextRanges(t *testing.T) {
	for name, tc := range map[string]struct {
		Markdown       string
		ExpectedRanges []Range
		ExpectedValues []string
	}{
		"simple": {
			Markdown:       "hello",
			ExpectedRanges: []Range{{0, 5}},
			ExpectedValues: []string{"hello"},
		},
		"simple2": {
			Markdown:       "hello!",
			ExpectedRanges: []Range{{0, 6}},
			ExpectedValues: []string{"hello!"},
		},
		"multiline": {
			Markdown:       "hello world\nfoobar",
			ExpectedRanges: []Range{{0, 11}, {12, 18}},
			ExpectedValues: []string{"hello world", "foobar"},
		},
		"code": {
			Markdown:       "hello `code` world",
			ExpectedRanges: []Range{{0, 6}, {12, 18}},
			ExpectedValues: []string{"hello ", " world"},
		},
		"notcode": {
			Markdown:       "hello ` world",
			ExpectedRanges: []Range{{0, 13}},
			ExpectedValues: []string{"hello ` world"},
		},
		"escape": {
			Markdown:       "\\*hello\\*",
			ExpectedRanges: []Range{{1, 7}, {8, 9}},
			ExpectedValues: []string{"*hello", "*"},
		},
		"escapeescape": {
			Markdown:       "\\\\",
			ExpectedRanges: []Range{{1, 2}},
			ExpectedValues: []string{"\\"},
		},
		"notescape": {
			Markdown:       "foo\\x",
			ExpectedRanges: []Range{{0, 5}},
			ExpectedValues: []string{"foo\\x"},
		},
		"notlink": {
			Markdown:       "[foo",
			ExpectedRanges: []Range{{0, 4}},
			ExpectedValues: []string{"[foo"},
		},
		"notlinkend": {
			Markdown:       "[foo]",
			ExpectedRanges: []Range{{0, 5}},
			ExpectedValues: []string{"[foo]"},
		},
		"notimage": {
			Markdown:       "![foo",
			ExpectedRanges: []Range{{0, 5}},
			ExpectedValues: []string{"![foo"},
		},
		"notimage2": {
			Markdown:       "!foo",
			ExpectedRanges: []Range{{0, 4}},
			ExpectedValues: []string{"!foo"},
		},
		"charref": {
			Markdown:       ""test",
			ExpectedRanges: []Range{{0, 1}, {6, 10}},
			ExpectedValues: []string{"\"", "test"},
		},
		"notcharref": {
			Markdown:       "&amp test",
			ExpectedRanges: []Range{{0, 9}},
			ExpectedValues: []string{"&amp test"},
		},
		"notcharref2": {
			Markdown:       "this is &mattermost;",
			ExpectedRanges: []Range{{0, 20}},
			ExpectedValues: []string{"this is &mattermost;"},
		},
		"standalone-ampersand": {
			Markdown:       "Hello & World",
			ExpectedRanges: []Range{{0, 13}},
			ExpectedValues: []string{"Hello & World"},
		},
	} {
		t.Run(name, func(t *testing.T) {
			var ranges []Range
			var values []string
			Inspect(tc.Markdown, func(node interface{}) bool {
				if textNode, ok := node.(*Text); ok {
					ranges = append(ranges, textNode.Range)
					values = append(values, textNode.Text)
				}
				return true
			})
			assert.Equal(t, tc.ExpectedRanges, ranges)
			assert.Equal(t, tc.ExpectedValues, values)

		})
	}

}