git_diff_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package models
  7. import (
  8. "html/template"
  9. "testing"
  10. dmp "github.com/sergi/go-diff/diffmatchpatch"
  11. "gitlab.com/gitote/git-module"
  12. )
  13. func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
  14. if s1 != string(s2) {
  15. t.Errorf("%s should be equal %s", s2, s1)
  16. }
  17. }
  18. func assertLineEqual(t *testing.T, d1 *git.DiffLine, d2 *git.DiffLine) {
  19. if d1 != d2 {
  20. t.Errorf("%v should be equal %v", d1, d2)
  21. }
  22. }
  23. func Test_diffToHTML(t *testing.T) {
  24. assertEqual(t, "+foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  25. dmp.Diff{dmp.DiffEqual, "foo "},
  26. dmp.Diff{dmp.DiffInsert, "bar"},
  27. dmp.Diff{dmp.DiffDelete, " baz"},
  28. dmp.Diff{dmp.DiffEqual, " biz"},
  29. }, git.DIFF_LINE_ADD))
  30. assertEqual(t, "-foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  31. dmp.Diff{dmp.DiffEqual, "foo "},
  32. dmp.Diff{dmp.DiffDelete, "bar"},
  33. dmp.Diff{dmp.DiffInsert, " baz"},
  34. dmp.Diff{dmp.DiffEqual, " biz"},
  35. }, git.DIFF_LINE_DEL))
  36. }