// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package gitdiff
import (
"fmt"
"html/template"
"strings"
"testing"
"gitea.dev/modules/highlight"
"gitea.dev/modules/translation"
"github.com/alecthomas/chroma/v2"
"github.com/stretchr/testify/assert"
)
func TestDiffWithHighlight(t *testing.T) {
t.Run("DiffLineAddDel", func(t *testing.T) {
t.Run("WithDiffTags", func(t *testing.T) {
hcd := newHighlightCodeDiff()
codeA := template.HTML(`x foo y`)
codeB := template.HTML(`x bar y`)
outDel, outAdd := hcd.diffLineWithHighlight(codeA, codeB)
assert.Equal(t, `x foo y`, string(outDel))
assert.Equal(t, `x bar y`, string(outAdd))
})
t.Run("NoRedundantTags", func(t *testing.T) {
// the equal parts only contain spaces, in this case, don't use "added/removed" tags
// because the diff lines already have a background color to indicate the change
hcd := newHighlightCodeDiff()
codeA := template.HTML(" \tfoo ")
codeB := template.HTML(" bar \n")
outDel, outAdd := hcd.diffLineWithHighlight(codeA, codeB)
assert.Equal(t, string(codeA), string(outDel))
assert.Equal(t, string(codeB), string(outAdd))
})
})
t.Run("CleanUp", func(t *testing.T) {
hcd := newHighlightCodeDiff()
codeA := template.HTML(` this is a comment`)
codeB := template.HTML(` this is updated comment`)
outDel, outAdd := hcd.diffLineWithHighlight(codeA, codeB)
assert.Equal(t, ` this is a comment`, string(outDel))
assert.Equal(t, ` this is updated comment`, string(outAdd))
codeA = `line1` + "\n" + `line2`
codeB = `line1` + "\n" + `line!`
outDel, outAdd = hcd.diffLineWithHighlight(codeA, codeB)
assert.Equal(t, `line1`+"\n"+`line2`, string(outDel))
assert.Equal(t, `line1`+"\n"+`line!`, string(outAdd))
})
t.Run("OpenCloseTags", func(t *testing.T) {
hcd := newHighlightCodeDiff()
hcd.placeholderTokenMap['O'], hcd.placeholderTokenMap['C'] = "", ""
assert.Equal(t, "", string(hcd.recoverOneDiff("OC")))
assert.Equal(t, "", string(hcd.recoverOneDiff("O")))
assert.Empty(t, string(hcd.recoverOneDiff("C")))
})
t.Run("ComplexDiff1", func(t *testing.T) {
oldCode, _, _ := highlight.RenderCodeSlowGuess("a.go", "Go", `xxx || yyy`)
newCode, _, _ := highlight.RenderCodeSlowGuess("a.go", "Go", `bot&xxx || bot&yyy`)
hcd := newHighlightCodeDiff()
_, add := hcd.diffLineWithHighlight(oldCode, newCode)
assert.Equal(t, strings.ReplaceAll(`
bot&
xxx ||
bot&
yyy`, "\n", ""), string(add))
})
forceTokenAsPlaceholder := func(hcd *highlightCodeDiff, r rune, token string) rune {
// for testing purpose only
hcd.tokenPlaceholderMap[token] = r
hcd.placeholderTokenMap[r] = token
return r
}
t.Run("ComplexDiff2", func(t *testing.T) {
// When running "diffLineWithHighlight", the newly inserted "added-code", and "removed-code" tags may break the original layout.
// The newly inserted tags can appear in any position, because the "diff" algorithm can make outputs like:
// * Equal:
// * Insert: xxyy
// * Equal: zz
// Then the newly inserted tags will make this output, the tags mismatch.
// * xxyy zz
// So we need to fix it to:
// * xx yy zz
hcd := newHighlightCodeDiff()
hcd.diffCodeAddedOpen = forceTokenAsPlaceholder(hcd, '[', "")
hcd.diffCodeClose = forceTokenAsPlaceholder(hcd, ']', "")
forceTokenAsPlaceholder(hcd, '{', "")
forceTokenAsPlaceholder(hcd, '}', "")
assert.Equal(t, `aaxxyyzzbb`, string(hcd.recoverOneDiff("aa{xx[yy]zz}bb")))
assert.Equal(t, `aaxxyyzzbb`, string(hcd.recoverOneDiff("aa[xx{yy}zz]bb")))
assert.Equal(t, `aaxxyyzzbb`, string(hcd.recoverOneDiff("aa{xx[yy}zz]bb")))
assert.Equal(t, `aaxxyyzzbb`, string(hcd.recoverOneDiff("aa[xx{yy]zz}bb")))
assert.Equal(t, `aaxxyyzzbbcc`, string(hcd.recoverOneDiff("aa[xx{yy][zz}bb]cc")))
// And do a simple test for "diffCodeRemovedOpen", it shares the same logic as "diffCodeAddedOpen"
hcd = newHighlightCodeDiff()
hcd.diffCodeRemovedOpen = forceTokenAsPlaceholder(hcd, '[', "")
hcd.diffCodeClose = forceTokenAsPlaceholder(hcd, ']', "")
forceTokenAsPlaceholder(hcd, '{', "")
forceTokenAsPlaceholder(hcd, '}', "")
assert.Equal(t, `aaxxyyzzbbcc`, string(hcd.recoverOneDiff("aa[xx{yy][zz}bb]cc")))
})
}
func TestDiffWithHighlightPlaceholder(t *testing.T) {
hcd := newHighlightCodeDiff()
output, _ := hcd.diffLineWithHighlight("a='\U00100000'", "a='\U0010FFFD''")
assert.Empty(t, hcd.placeholderTokenMap[0x00100000])
assert.Empty(t, hcd.placeholderTokenMap[0x0010FFFD])
expected := fmt.Sprintf(`a='%s'`, "\U00100000")
assert.Equal(t, expected, string(output))
hcd = newHighlightCodeDiff()
_, output = hcd.diffLineWithHighlight("a='\U00100000'", "a='\U0010FFFD'")
expected = fmt.Sprintf(`a='%s'`, "\U0010FFFD")
assert.Equal(t, expected, string(output))
}
func TestDiffWithHighlightPlaceholderExhausted(t *testing.T) {
hcd := newHighlightCodeDiff()
hcd.placeholderMaxCount = 0
placeHolderAmp := string(rune(0xFFFD))
del, add := hcd.diffLineWithHighlight(`<`, `>`)
assert.Equal(t, placeHolderAmp+"lt;", string(del))
assert.Equal(t, placeHolderAmp+"gt;", string(add))
del, add = hcd.diffLineWithHighlight(`foo`, `bar`)
assert.Equal(t, "foo", string(del))
assert.Equal(t, "bar", string(add))
}
func TestDiffWithHighlightTagMatch(t *testing.T) {
totalOverflow := 0
for i := 0; ; i++ {
hcd := newHighlightCodeDiff()
hcd.placeholderMaxCount = i
del, add := hcd.diffLineWithHighlight(`<`, `>`)
totalOverflow += hcd.placeholderOverflowCount
assert.Equal(t, strings.Count(string(del), "foo y`)
codeB := template.HTML(`x bar y`)
hcd.diffLineWithHighlight(codeA, codeB)
}
}
func BenchmarkGetDiffLineForRender(b *testing.B) {
diffSection := &DiffSection{
FileName: "test.go",
highlightLexer: &diffVarMutable[chroma.Lexer]{},
}
leftLine := &DiffLine{LeftIdx: 1, Content: `-x foo y`}
rightLine := &DiffLine{RightIdx: 1, Content: `+x bar y`}
locale := translation.MockLocale{}
b.ResetTimer()
// HINT: CODE-HIGHLIGHT-PERFORMANCE: the real bottleneck is in the Chroma highlighter.
for b.Loop() {
// Clear cache only at the start of rendering the pair
leftLine.cachedDiffInline = nil
rightLine.cachedDiffInline = nil
_ = diffSection.getDiffLineForRender(DiffLineDel, leftLine, rightLine, locale)
_ = diffSection.getDiffLineForRender(DiffLineAdd, leftLine, rightLine, locale)
}
}