gitea源码

highlightdiff_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package gitdiff
  4. import (
  5. "fmt"
  6. "html/template"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestDiffWithHighlight(t *testing.T) {
  12. t.Run("DiffLineAddDel", func(t *testing.T) {
  13. hcd := newHighlightCodeDiff()
  14. codeA := template.HTML(`x <span class="k">foo</span> y`)
  15. codeB := template.HTML(`x <span class="k">bar</span> y`)
  16. outDel := hcd.diffLineWithHighlight(DiffLineDel, codeA, codeB)
  17. assert.Equal(t, `x <span class="k"><span class="removed-code">foo</span></span> y`, string(outDel))
  18. outAdd := hcd.diffLineWithHighlight(DiffLineAdd, codeA, codeB)
  19. assert.Equal(t, `x <span class="k"><span class="added-code">bar</span></span> y`, string(outAdd))
  20. })
  21. t.Run("CleanUp", func(t *testing.T) {
  22. hcd := newHighlightCodeDiff()
  23. codeA := template.HTML(`<span class="cm>this is a comment</span>`)
  24. codeB := template.HTML(`<span class="cm>this is updated comment</span>`)
  25. outDel := hcd.diffLineWithHighlight(DiffLineDel, codeA, codeB)
  26. assert.Equal(t, `<span class="cm>this is <span class="removed-code">a</span> comment</span>`, string(outDel))
  27. outAdd := hcd.diffLineWithHighlight(DiffLineAdd, codeA, codeB)
  28. assert.Equal(t, `<span class="cm>this is <span class="added-code">updated</span> comment</span>`, string(outAdd))
  29. })
  30. t.Run("OpenCloseTags", func(t *testing.T) {
  31. hcd := newHighlightCodeDiff()
  32. hcd.placeholderTokenMap['O'], hcd.placeholderTokenMap['C'] = "<span>", "</span>"
  33. assert.Equal(t, "<span></span>", string(hcd.recoverOneDiff("OC")))
  34. assert.Equal(t, "<span></span>", string(hcd.recoverOneDiff("O")))
  35. assert.Empty(t, string(hcd.recoverOneDiff("C")))
  36. })
  37. }
  38. func TestDiffWithHighlightPlaceholder(t *testing.T) {
  39. hcd := newHighlightCodeDiff()
  40. output := hcd.diffLineWithHighlight(DiffLineDel, "a='\U00100000'", "a='\U0010FFFD''")
  41. assert.Empty(t, hcd.placeholderTokenMap[0x00100000])
  42. assert.Empty(t, hcd.placeholderTokenMap[0x0010FFFD])
  43. expected := fmt.Sprintf(`a='<span class="removed-code">%s</span>'`, "\U00100000")
  44. assert.Equal(t, expected, string(output))
  45. hcd = newHighlightCodeDiff()
  46. output = hcd.diffLineWithHighlight(DiffLineAdd, "a='\U00100000'", "a='\U0010FFFD'")
  47. expected = fmt.Sprintf(`a='<span class="added-code">%s</span>'`, "\U0010FFFD")
  48. assert.Equal(t, expected, string(output))
  49. }
  50. func TestDiffWithHighlightPlaceholderExhausted(t *testing.T) {
  51. hcd := newHighlightCodeDiff()
  52. hcd.placeholderMaxCount = 0
  53. placeHolderAmp := string(rune(0xFFFD))
  54. output := hcd.diffLineWithHighlight(DiffLineDel, `<span class="k">&lt;</span>`, `<span class="k">&gt;</span>`)
  55. assert.Equal(t, placeHolderAmp+"lt;", string(output))
  56. output = hcd.diffLineWithHighlight(DiffLineAdd, `<span class="k">&lt;</span>`, `<span class="k">&gt;</span>`)
  57. assert.Equal(t, placeHolderAmp+"gt;", string(output))
  58. }
  59. func TestDiffWithHighlightTagMatch(t *testing.T) {
  60. f := func(t *testing.T, lineType DiffLineType) {
  61. totalOverflow := 0
  62. for i := 0; ; i++ {
  63. hcd := newHighlightCodeDiff()
  64. hcd.placeholderMaxCount = i
  65. output := string(hcd.diffLineWithHighlight(lineType, `<span class="k">&lt;</span>`, `<span class="k">&gt;</span>`))
  66. totalOverflow += hcd.placeholderOverflowCount
  67. assert.Equal(t, strings.Count(output, "<span"), strings.Count(output, "</span"))
  68. if hcd.placeholderOverflowCount == 0 {
  69. break
  70. }
  71. }
  72. assert.NotZero(t, totalOverflow)
  73. }
  74. t.Run("DiffLineAdd", func(t *testing.T) { f(t, DiffLineAdd) })
  75. t.Run("DiffLineDel", func(t *testing.T) { f(t, DiffLineDel) })
  76. }