2022-07-23 11:28:02 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-07-23 11:28:02 +00:00
|
|
|
|
|
|
|
package gitdiff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sergi/go-diff/diffmatchpatch"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiffWithHighlight(t *testing.T) {
|
2024-08-06 05:57:25 +00:00
|
|
|
hcd := NewHighlightCodeDiff()
|
2022-07-23 11:28:02 +00:00
|
|
|
diffs := hcd.diffWithHighlight(
|
|
|
|
"main.v", "",
|
|
|
|
" run('<>')\n",
|
|
|
|
" run(db)\n",
|
|
|
|
)
|
|
|
|
|
2022-09-26 05:50:03 +00:00
|
|
|
expected := ` <span class="n">run</span><span class="o">(</span><span class="removed-code"><span class="k">'</span><span class="o"><</span><span class="o">></span><span class="k">'</span></span><span class="o">)</span>`
|
2022-07-23 11:28:02 +00:00
|
|
|
output := diffToHTML(nil, diffs, DiffLineDel)
|
|
|
|
assert.Equal(t, expected, output)
|
|
|
|
|
2022-09-26 05:50:03 +00:00
|
|
|
expected = ` <span class="n">run</span><span class="o">(</span><span class="added-code"><span class="n">db</span></span><span class="o">)</span>`
|
2022-07-23 11:28:02 +00:00
|
|
|
output = diffToHTML(nil, diffs, DiffLineAdd)
|
|
|
|
assert.Equal(t, expected, output)
|
|
|
|
|
2024-08-06 05:57:25 +00:00
|
|
|
hcd = NewHighlightCodeDiff()
|
|
|
|
hcd.PlaceholderTokenMap['O'] = "<span>"
|
|
|
|
hcd.PlaceholderTokenMap['C'] = "</span>"
|
2022-07-23 11:28:02 +00:00
|
|
|
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) {
|
2024-08-06 05:57:25 +00:00
|
|
|
hcd := NewHighlightCodeDiff()
|
2022-07-23 11:28:02 +00:00
|
|
|
diffs := hcd.diffWithHighlight(
|
|
|
|
"main.js", "",
|
|
|
|
"a='\U00100000'",
|
|
|
|
"a='\U0010FFFD''",
|
|
|
|
)
|
2024-08-06 05:57:25 +00:00
|
|
|
assert.Equal(t, "", hcd.PlaceholderTokenMap[0x00100000])
|
|
|
|
assert.Equal(t, "", hcd.PlaceholderTokenMap[0x0010FFFD])
|
2022-07-23 11:28:02 +00:00
|
|
|
|
2022-09-26 05:50:03 +00:00
|
|
|
expected := fmt.Sprintf(`<span class="nx">a</span><span class="o">=</span><span class="s1">'</span><span class="removed-code">%s</span>'`, "\U00100000")
|
2022-07-23 11:28:02 +00:00
|
|
|
output := diffToHTML(hcd.lineWrapperTags, diffs, DiffLineDel)
|
|
|
|
assert.Equal(t, expected, output)
|
|
|
|
|
2024-08-06 05:57:25 +00:00
|
|
|
hcd = NewHighlightCodeDiff()
|
2022-07-23 11:28:02 +00:00
|
|
|
diffs = hcd.diffWithHighlight(
|
|
|
|
"main.js", "",
|
|
|
|
"a='\U00100000'",
|
|
|
|
"a='\U0010FFFD'",
|
|
|
|
)
|
|
|
|
expected = fmt.Sprintf(`<span class="nx">a</span><span class="o">=</span><span class="s1">'</span><span class="added-code">%s</span>'`, "\U0010FFFD")
|
|
|
|
output = diffToHTML(nil, diffs, DiffLineAdd)
|
|
|
|
assert.Equal(t, expected, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiffWithHighlightPlaceholderExhausted(t *testing.T) {
|
2024-08-06 05:57:25 +00:00
|
|
|
hcd := NewHighlightCodeDiff()
|
2022-07-23 11:28:02 +00:00
|
|
|
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)
|
|
|
|
|
2024-08-06 05:57:25 +00:00
|
|
|
hcd = NewHighlightCodeDiff()
|
2022-07-23 11:28:02 +00:00
|
|
|
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++ {
|
2024-08-06 05:57:25 +00:00
|
|
|
hcd := NewHighlightCodeDiff()
|
2022-07-23 11:28:02 +00:00
|
|
|
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)
|
|
|
|
}
|