git_diff_test.go 1005 B

1234567891011121314151617181920212223242526272829303132333435
  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 db
  5. import (
  6. "html/template"
  7. "testing"
  8. "github.com/gogs/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 Test_diffToHTML(t *testing.T) {
  17. assertEqual(t, "+foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  18. {Type: dmp.DiffEqual, Text: "foo "},
  19. {Type: dmp.DiffInsert, Text: "bar"},
  20. {Type: dmp.DiffDelete, Text: " baz"},
  21. {Type: dmp.DiffEqual, Text: " biz"},
  22. }, git.DiffLineAdd))
  23. assertEqual(t, "-foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  24. {Type: dmp.DiffEqual, Text: "foo "},
  25. {Type: dmp.DiffDelete, Text: "bar"},
  26. {Type: dmp.DiffInsert, Text: " baz"},
  27. {Type: dmp.DiffEqual, Text: " biz"},
  28. }, git.DiffLineDel))
  29. }