test_yaml_diff.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. yaml = pytest.importorskip('yaml')
  4. import datetime
  5. import ioex.datetimeex
  6. import ioex.debug
  7. from ioex.shell import TextColor
  8. @pytest.mark.parametrize(('a', 'b', 'expected_diff_lines'), [
  9. [
  10. [1, 2, 3],
  11. [1, 2, 3],
  12. [
  13. u' - 1',
  14. u' - 2',
  15. u' - 3',
  16. ],
  17. ],
  18. [
  19. [1, 2, 3],
  20. [1, 3],
  21. [
  22. u' - 1',
  23. u'- - 2',
  24. u' - 3',
  25. ],
  26. ],
  27. [
  28. [1, 3],
  29. [1, 2, 3],
  30. [
  31. u' - 1',
  32. u'+ - 2',
  33. u' - 3',
  34. ],
  35. ],
  36. [
  37. u'abcdef',
  38. u'abdf',
  39. [
  40. u"- abcdef",
  41. u"? - -",
  42. u"+ abdf",
  43. u" ...",
  44. ],
  45. ],
  46. [
  47. u'äbcdef',
  48. u'äbcdÜf',
  49. [
  50. u"- äbcdef",
  51. u"? ^",
  52. u"+ äbcdÜf",
  53. u"? ^",
  54. u" ...",
  55. ],
  56. ],
  57. [
  58. ioex.datetimeex.Duration(years = 2),
  59. ioex.datetimeex.Duration(years = 2),
  60. [
  61. u' !duration',
  62. u' years: 2',
  63. ],
  64. ],
  65. [
  66. ioex.datetimeex.Duration(years = 1),
  67. ioex.datetimeex.Duration(years = 2),
  68. [
  69. u' !duration',
  70. u'- years: 1',
  71. u'? ^',
  72. u'+ years: 2',
  73. u'? ^',
  74. ],
  75. ],
  76. [
  77. ioex.datetimeex.Period(
  78. start = datetime.datetime(2016, 7, 29, 21, 59, 13),
  79. end = datetime.datetime(2017, 8, 30, 22, 32, 12),
  80. ),
  81. ioex.datetimeex.Period(
  82. start = datetime.datetime(2016, 7, 29, 21, 59, 13),
  83. end = datetime.datetime(2017, 8, 30, 23, 32, 12),
  84. ),
  85. [
  86. u' !period',
  87. u'- end: 2017-08-30 22:32:12',
  88. u'? ^',
  89. u'+ end: 2017-08-30 23:32:12',
  90. u'? ^',
  91. u' start: 2016-07-29 21:59:13',
  92. ],
  93. ],
  94. ])
  95. def test_yaml_diff(a, b, expected_diff_lines):
  96. class TestDumper(yaml.SafeDumper):
  97. pass
  98. ioex.datetimeex.Duration.register_yaml_representer(TestDumper)
  99. ioex.datetimeex.Period.register_yaml_representer(TestDumper)
  100. expected_diff = u'\n'.join(expected_diff_lines) + u'\n'
  101. generated_diff = ioex.yaml_diff(a, b, dumper = TestDumper)
  102. assert expected_diff == generated_diff, \
  103. '\ngenerated: %r\nexpected: %r' % (generated_diff, expected_diff)
  104. @pytest.mark.parametrize(('a', 'b', 'expected_diff_lines'), [
  105. [
  106. [1, 2, 3],
  107. [1, 3, 4],
  108. [
  109. TextColor.default + u' - 1',
  110. TextColor.red + u'- - 2',
  111. TextColor.default + u' - 3',
  112. TextColor.green + u'+ - 4',
  113. ],
  114. ],
  115. [
  116. 'abcef',
  117. 'abdef',
  118. [
  119. TextColor.red + u'- abcef',
  120. TextColor.yellow + u'? ^',
  121. TextColor.green + u'+ abdef',
  122. TextColor.yellow + u'? ^',
  123. TextColor.default + u' ...',
  124. ],
  125. ],
  126. [
  127. {'a': True, 'b': 123, 'c': 'string', 'd': 1.23},
  128. {'a': True, 'b': 123, 'c': 'string', 'd': 1.23},
  129. [
  130. TextColor.default + u' a: true',
  131. TextColor.default + u' b: 123',
  132. TextColor.default + u' c: string',
  133. TextColor.default + u' d: 1.23',
  134. ],
  135. ],
  136. [
  137. {'a': True, 'b': 123, 'c': 'str', 'd': 1.23},
  138. {'a': False, 'b': 13, 'c': 'string', 'd': 12.3},
  139. [
  140. TextColor.red + u'- a: true',
  141. TextColor.green + u'+ a: false',
  142. TextColor.red + u'- b: 123',
  143. TextColor.yellow + u'? -',
  144. TextColor.green + u'+ b: 13',
  145. TextColor.red + u'- c: str',
  146. TextColor.green + u'+ c: string',
  147. TextColor.yellow + u'? +++',
  148. TextColor.red + u'- d: 1.23',
  149. TextColor.yellow + u'? -',
  150. TextColor.green + u'+ d: 12.3',
  151. TextColor.yellow + u'? +',
  152. ],
  153. ],
  154. ])
  155. def test_yaml_diff(a, b, expected_diff_lines):
  156. expected_diff = (u'\n' + TextColor.default).join(expected_diff_lines) + u'\n' + TextColor.default
  157. generated_diff = ioex.yaml_diff(a, b, colors = True)
  158. assert expected_diff == generated_diff, \
  159. '\ngenerated: %r\nexpected: %r' % (generated_diff, expected_diff)