選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

diff_test.go 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. const exampleDiff = `diff --git a/README.md b/README.md
  11. --- a/README.md
  12. +++ b/README.md
  13. @@ -1,3 +1,6 @@
  14. # gitea-github-migrator
  15. +
  16. + Build Status
  17. - Latest Release
  18. Docker Pulls
  19. + cut off
  20. + cut off`
  21. func TestCutDiffAroundLine(t *testing.T) {
  22. result := CutDiffAroundLine(strings.NewReader(exampleDiff), 4, false, 3)
  23. resultByLine := strings.Split(result, "\n")
  24. assert.Len(t, resultByLine, 7)
  25. // Check if headers got transferred
  26. assert.Equal(t, "diff --git a/README.md b/README.md", resultByLine[0])
  27. assert.Equal(t, "--- a/README.md", resultByLine[1])
  28. assert.Equal(t, "+++ b/README.md", resultByLine[2])
  29. // Check if hunk header is calculated correctly
  30. assert.Equal(t, "@@ -2,2 +3,2 @@", resultByLine[3])
  31. // Check if line got transferred
  32. assert.Equal(t, "+ Build Status", resultByLine[4])
  33. // Must be same result as before since old line 3 == new line 5
  34. newResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
  35. assert.Equal(t, result, newResult, "Must be same result as before since old line 3 == new line 5")
  36. newResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 300)
  37. assert.Equal(t, exampleDiff, newResult)
  38. emptyResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 0)
  39. assert.Empty(t, emptyResult)
  40. // Line is out of scope
  41. emptyResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 434, false, 0)
  42. assert.Empty(t, emptyResult)
  43. }
  44. func BenchmarkCutDiffAroundLine(b *testing.B) {
  45. for n := 0; n < b.N; n++ {
  46. CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
  47. }
  48. }
  49. func ExampleCutDiffAroundLine() {
  50. const diff = `diff --git a/README.md b/README.md
  51. --- a/README.md
  52. +++ b/README.md
  53. @@ -1,3 +1,6 @@
  54. # gitea-github-migrator
  55. +
  56. + Build Status
  57. - Latest Release
  58. Docker Pulls
  59. + cut off
  60. + cut off`
  61. result := CutDiffAroundLine(strings.NewReader(diff), 4, false, 3)
  62. println(result)
  63. }
  64. func TestParseDiffHunkString(t *testing.T) {
  65. leftLine, leftHunk, rightLine, rightHunk := ParseDiffHunkString("@@ -19,3 +19,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER")
  66. assert.EqualValues(t, 19, leftLine)
  67. assert.EqualValues(t, 3, leftHunk)
  68. assert.EqualValues(t, 19, rightLine)
  69. assert.EqualValues(t, 5, rightHunk)
  70. }