git_diff_test.go 1.1 KB

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