git_diff_test.go 959 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package models
  2. import (
  3. "html/template"
  4. "testing"
  5. dmp "github.com/sergi/go-diff/diffmatchpatch"
  6. "gitlab.com/gitote/git-module"
  7. )
  8. func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
  9. if s1 != string(s2) {
  10. t.Errorf("%s should be equal %s", s2, s1)
  11. }
  12. }
  13. func assertLineEqual(t *testing.T, d1 *git.DiffLine, d2 *git.DiffLine) {
  14. if d1 != d2 {
  15. t.Errorf("%v should be equal %v", d1, d2)
  16. }
  17. }
  18. func Test_diffToHTML(t *testing.T) {
  19. assertEqual(t, "+foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  20. dmp.Diff{dmp.DiffEqual, "foo "},
  21. dmp.Diff{dmp.DiffInsert, "bar"},
  22. dmp.Diff{dmp.DiffDelete, " baz"},
  23. dmp.Diff{dmp.DiffEqual, " biz"},
  24. }, git.DIFF_LINE_ADD))
  25. assertEqual(t, "-foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  26. dmp.Diff{dmp.DiffEqual, "foo "},
  27. dmp.Diff{dmp.DiffDelete, "bar"},
  28. dmp.Diff{dmp.DiffInsert, " baz"},
  29. dmp.Diff{dmp.DiffEqual, " biz"},
  30. }, git.DIFF_LINE_DEL))
  31. }