summaryrefslogtreecommitdiffstats
path: root/services/gitdiff/highlightdiff_test.go
blob: 2ff4472bcc1e20f832a742e0b61fbf289bfaa218 (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
116
117
118
119
120
121
122
123
124
125
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package gitdiff

import (
	"fmt"
	"strings"
	"testing"

	"github.com/sergi/go-diff/diffmatchpatch"
	"github.com/stretchr/testify/assert"
)

func TestDiffWithHighlight(t *testing.T) {
	hcd := NewHighlightCodeDiff()
	diffs := hcd.diffWithHighlight(
		"main.v", "",
		"		run('<>')\n",
		"		run(db)\n",
	)

	expected := `		<span class="n">run</span><span class="o">(</span><span class="removed-code"><span class="k">&#39;</span><span class="o">&lt;</span><span class="o">&gt;</span><span class="k">&#39;</span></span><span class="o">)</span>`
	output := diffToHTML(nil, diffs, DiffLineDel)
	assert.Equal(t, expected, output)

	expected = `		<span class="n">run</span><span class="o">(</span><span class="added-code"><span class="n">db</span></span><span class="o">)</span>`
	output = diffToHTML(nil, diffs, DiffLineAdd)
	assert.Equal(t, expected, output)

	hcd = NewHighlightCodeDiff()
	hcd.PlaceholderTokenMap['O'] = "<span>"
	hcd.PlaceholderTokenMap['C'] = "</span>"
	diff := diffmatchpatch.Diff{}

	diff.Text = "OC"
	hcd.recoverOneDiff(&diff)
	assert.Equal(t, "<span></span>", diff.Text)

	diff.Text = "O"
	hcd.recoverOneDiff(&diff)
	assert.Equal(t, "<span></span>", diff.Text)

	diff.Text = "C"
	hcd.recoverOneDiff(&diff)
	assert.Equal(t, "", diff.Text)
}

func TestDiffWithHighlightPlaceholder(t *testing.T) {
	hcd := NewHighlightCodeDiff()
	diffs := hcd.diffWithHighlight(
		"main.js", "",
		"a='\U00100000'",
		"a='\U0010FFFD''",
	)
	assert.Equal(t, "", hcd.PlaceholderTokenMap[0x00100000])
	assert.Equal(t, "", hcd.PlaceholderTokenMap[0x0010FFFD])

	expected := fmt.Sprintf(`<span class="nx">a</span><span class="o">=</span><span class="s1">&#39;</span><span class="removed-code">%s</span>&#39;`, "\U00100000")
	output := diffToHTML(hcd.lineWrapperTags, diffs, DiffLineDel)
	assert.Equal(t, expected, output)

	hcd = NewHighlightCodeDiff()
	diffs = hcd.diffWithHighlight(
		"main.js", "",
		"a='\U00100000'",
		"a='\U0010FFFD'",
	)
	expected = fmt.Sprintf(`<span class="nx">a</span><span class="o">=</span><span class="s1">&#39;</span><span class="added-code">%s</span>&#39;`, "\U0010FFFD")
	output = diffToHTML(nil, diffs, DiffLineAdd)
	assert.Equal(t, expected, output)
}

func TestDiffWithHighlightPlaceholderExhausted(t *testing.T) {
	hcd := NewHighlightCodeDiff()
	hcd.placeholderMaxCount = 0
	diffs := hcd.diffWithHighlight(
		"main.js", "",
		"'",
		``,
	)
	output := diffToHTML(nil, diffs, DiffLineDel)
	expected := fmt.Sprintf(`<span class="removed-code">%s#39;</span>`, "\uFFFD")
	assert.Equal(t, expected, output)

	hcd = NewHighlightCodeDiff()
	hcd.placeholderMaxCount = 0
	diffs = hcd.diffWithHighlight(
		"main.js", "",
		"a < b",
		"a > b",
	)
	output = diffToHTML(nil, diffs, DiffLineDel)
	expected = fmt.Sprintf(`a %s<span class="removed-code">l</span>t; b`, "\uFFFD")
	assert.Equal(t, expected, output)

	output = diffToHTML(nil, diffs, DiffLineAdd)
	expected = fmt.Sprintf(`a %s<span class="added-code">g</span>t; b`, "\uFFFD")
	assert.Equal(t, expected, output)
}

func TestDiffWithHighlightTagMatch(t *testing.T) {
	totalOverflow := 0
	for i := 0; i < 100; i++ {
		hcd := NewHighlightCodeDiff()
		hcd.placeholderMaxCount = i
		diffs := hcd.diffWithHighlight(
			"main.js", "",
			"a='1'",
			"b='2'",
		)
		totalOverflow += hcd.placeholderOverflowCount

		output := diffToHTML(nil, diffs, DiffLineDel)
		c1 := strings.Count(output, "<span")
		c2 := strings.Count(output, "</span")
		assert.Equal(t, c1, c2)

		output = diffToHTML(nil, diffs, DiffLineAdd)
		c1 = strings.Count(output, "<span")
		c2 = strings.Count(output, "</span")
		assert.Equal(t, c1, c2)
	}
	assert.NotZero(t, totalOverflow)
}