test_yaml_diff.py 4.5 KB

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