git_diff_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package models
  2. import (
  3. dmp "github.com/sergi/go-diff/diffmatchpatch"
  4. "html/template"
  5. "testing"
  6. )
  7. func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
  8. if s1 != string(s2) {
  9. t.Errorf("%s should be equal %s", s2, s1)
  10. }
  11. }
  12. func assertLineEqual(t *testing.T, d1 *DiffLine, d2 *DiffLine) {
  13. if d1 != d2 {
  14. t.Errorf("%v should be equal %v", d1, d2)
  15. }
  16. }
  17. func TestDiffToHTML(t *testing.T) {
  18. assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  19. dmp.Diff{dmp.DiffEqual, "foo "},
  20. dmp.Diff{dmp.DiffInsert, "bar"},
  21. dmp.Diff{dmp.DiffDelete, " baz"},
  22. dmp.Diff{dmp.DiffEqual, " biz"},
  23. }, DIFF_LINE_ADD))
  24. assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
  25. dmp.Diff{dmp.DiffEqual, "foo "},
  26. dmp.Diff{dmp.DiffDelete, "bar"},
  27. dmp.Diff{dmp.DiffInsert, " baz"},
  28. dmp.Diff{dmp.DiffEqual, " biz"},
  29. }, DIFF_LINE_DEL))
  30. }
  31. // test if GetLine is return the correct lines
  32. func TestGetLine(t *testing.T) {
  33. ds := DiffSection{Lines: []*DiffLine{
  34. &DiffLine{LeftIdx: 28, RightIdx: 28, Type: DIFF_LINE_PLAIN},
  35. &DiffLine{LeftIdx: 29, RightIdx: 29, Type: DIFF_LINE_PLAIN},
  36. &DiffLine{LeftIdx: 30, RightIdx: 30, Type: DIFF_LINE_PLAIN},
  37. &DiffLine{LeftIdx: 31, RightIdx: 0, Type: DIFF_LINE_DEL},
  38. &DiffLine{LeftIdx: 0, RightIdx: 31, Type: DIFF_LINE_ADD},
  39. &DiffLine{LeftIdx: 0, RightIdx: 32, Type: DIFF_LINE_ADD},
  40. &DiffLine{LeftIdx: 32, RightIdx: 33, Type: DIFF_LINE_PLAIN},
  41. &DiffLine{LeftIdx: 33, RightIdx: 0, Type: DIFF_LINE_DEL},
  42. &DiffLine{LeftIdx: 34, RightIdx: 0, Type: DIFF_LINE_DEL},
  43. &DiffLine{LeftIdx: 35, RightIdx: 0, Type: DIFF_LINE_DEL},
  44. &DiffLine{LeftIdx: 36, RightIdx: 0, Type: DIFF_LINE_DEL},
  45. &DiffLine{LeftIdx: 0, RightIdx: 34, Type: DIFF_LINE_ADD},
  46. &DiffLine{LeftIdx: 0, RightIdx: 35, Type: DIFF_LINE_ADD},
  47. &DiffLine{LeftIdx: 0, RightIdx: 36, Type: DIFF_LINE_ADD},
  48. &DiffLine{LeftIdx: 0, RightIdx: 37, Type: DIFF_LINE_ADD},
  49. &DiffLine{LeftIdx: 37, RightIdx: 38, Type: DIFF_LINE_PLAIN},
  50. &DiffLine{LeftIdx: 38, RightIdx: 39, Type: DIFF_LINE_PLAIN},
  51. }}
  52. assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 31), ds.Lines[4])
  53. assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 31), ds.Lines[3])
  54. assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 33), ds.Lines[11])
  55. assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 34), ds.Lines[12])
  56. assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 35), ds.Lines[13])
  57. assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 36), ds.Lines[14])
  58. assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 34), ds.Lines[7])
  59. assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 35), ds.Lines[8])
  60. assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 36), ds.Lines[9])
  61. assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 37), ds.Lines[10])
  62. }