You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

git_diff_test.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package models
  2. import (
  3. "html/template"
  4. "strings"
  5. "testing"
  6. dmp "github.com/sergi/go-diff/diffmatchpatch"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
  10. if s1 != string(s2) {
  11. t.Errorf("%s should be equal %s", s2, s1)
  12. }
  13. }
  14. func assertLineEqual(t *testing.T, d1 *DiffLine, d2 *DiffLine) {
  15. if d1 != d2 {
  16. t.Errorf("%v should be equal %v", d1, d2)
  17. }
  18. }
  19. func TestDiffToHTML(t *testing.T) {
  20. assertEqual(t, "+foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  21. {Type: dmp.DiffEqual, Text: "foo "},
  22. {Type: dmp.DiffInsert, Text: "bar"},
  23. {Type: dmp.DiffDelete, Text: " baz"},
  24. {Type: dmp.DiffEqual, Text: " biz"},
  25. }, DiffLineAdd))
  26. assertEqual(t, "-foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  27. {Type: dmp.DiffEqual, Text: "foo "},
  28. {Type: dmp.DiffDelete, Text: "bar"},
  29. {Type: dmp.DiffInsert, Text: " baz"},
  30. {Type: dmp.DiffEqual, Text: " biz"},
  31. }, DiffLineDel))
  32. }
  33. const exampleDiff = `diff --git a/README.md b/README.md
  34. --- a/README.md
  35. +++ b/README.md
  36. @@ -1,3 +1,6 @@
  37. # gitea-github-migrator
  38. +
  39. + Build Status
  40. - Latest Release
  41. Docker Pulls
  42. + cut off
  43. + cut off`
  44. func TestCutDiffAroundLine(t *testing.T) {
  45. result := CutDiffAroundLine(strings.NewReader(exampleDiff), 4, false, 3)
  46. resultByLine := strings.Split(result, "\n")
  47. assert.Len(t, resultByLine, 7)
  48. // Check if headers got transferred
  49. assert.Equal(t, "diff --git a/README.md b/README.md", resultByLine[0])
  50. assert.Equal(t, "--- a/README.md", resultByLine[1])
  51. assert.Equal(t, "+++ b/README.md", resultByLine[2])
  52. // Check if hunk header is calculated correctly
  53. assert.Equal(t, "@@ -2,2 +3,2 @@", resultByLine[3])
  54. // Check if line got transferred
  55. assert.Equal(t, "+ Build Status", resultByLine[4])
  56. // Must be same result as before since old line 3 == new line 5
  57. newResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
  58. assert.Equal(t, result, newResult, "Must be same result as before since old line 3 == new line 5")
  59. newResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 300)
  60. assert.Equal(t, exampleDiff, newResult)
  61. emptyResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 0)
  62. assert.Empty(t, emptyResult)
  63. // Line is out of scope
  64. emptyResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 434, false, 0)
  65. assert.Empty(t, emptyResult)
  66. }
  67. func BenchmarkCutDiffAroundLine(b *testing.B) {
  68. for n := 0; n < b.N; n++ {
  69. CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
  70. }
  71. }
  72. func ExampleCutDiffAroundLine() {
  73. const diff = `diff --git a/README.md b/README.md
  74. --- a/README.md
  75. +++ b/README.md
  76. @@ -1,3 +1,6 @@
  77. # gitea-github-migrator
  78. +
  79. + Build Status
  80. - Latest Release
  81. Docker Pulls
  82. + cut off
  83. + cut off`
  84. result := CutDiffAroundLine(strings.NewReader(diff), 4, false, 3)
  85. println(result)
  86. }
  87. func setupDefaultDiff() *Diff {
  88. return &Diff{
  89. Files: []*DiffFile{
  90. {
  91. Name: "README.md",
  92. Sections: []*DiffSection{
  93. {
  94. Lines: []*DiffLine{
  95. {
  96. LeftIdx: 4,
  97. RightIdx: 4,
  98. },
  99. },
  100. },
  101. },
  102. },
  103. },
  104. }
  105. }
  106. func TestDiff_LoadComments(t *testing.T) {
  107. issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
  108. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  109. diff := setupDefaultDiff()
  110. assert.NoError(t, PrepareTestDatabase())
  111. assert.NoError(t, diff.LoadComments(issue, user))
  112. assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
  113. }
  114. func TestDiffLine_CanComment(t *testing.T) {
  115. assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment())
  116. assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*Comment{{Content: "bla"}}}).CanComment())
  117. assert.True(t, (&DiffLine{Type: DiffLineAdd}).CanComment())
  118. assert.True(t, (&DiffLine{Type: DiffLineDel}).CanComment())
  119. assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment())
  120. }
  121. func TestDiffLine_GetCommentSide(t *testing.T) {
  122. assert.Equal(t, "previous", (&DiffLine{Comments: []*Comment{{Line: -3}}}).GetCommentSide())
  123. assert.Equal(t, "proposed", (&DiffLine{Comments: []*Comment{{Line: 3}}}).GetCommentSide())
  124. }