1
0

diff_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package gitutil
  2. import (
  3. "html/template"
  4. "testing"
  5. dmp "github.com/sergi/go-diff/diffmatchpatch"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/gogs/git-module"
  8. )
  9. func Test_diffsToHTML(t *testing.T) {
  10. tests := []struct {
  11. diffs []dmp.Diff
  12. lineType git.DiffLineType
  13. expHTML template.HTML
  14. }{
  15. {
  16. diffs: []dmp.Diff{
  17. {Type: dmp.DiffEqual, Text: "foo "},
  18. {Type: dmp.DiffInsert, Text: "bar"},
  19. {Type: dmp.DiffDelete, Text: " baz"},
  20. {Type: dmp.DiffEqual, Text: " biz"},
  21. },
  22. lineType: git.DiffLineAdd,
  23. expHTML: template.HTML(`+foo <span class="added-code">bar</span> biz`),
  24. },
  25. {
  26. diffs: []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. },
  32. lineType: git.DiffLineDelete,
  33. expHTML: template.HTML(`-foo <span class="removed-code">bar</span> biz`),
  34. },
  35. }
  36. for _, test := range tests {
  37. t.Run("", func(t *testing.T) {
  38. assert.Equal(t, test.expHTML, diffsToHTML(test.diffs, test.lineType))
  39. })
  40. }
  41. }