test_cdiff.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """Unit test for cdiff"""
  4. import sys
  5. if sys.hexversion < 0x02050000:
  6. raise SystemExit("*** Requires python >= 2.5.0")
  7. import unittest
  8. sys.path.insert(0, '')
  9. import cdiff
  10. class Sequential(object):
  11. """A non-seekable iterator, mock of file object"""
  12. def __init__(self, items):
  13. self._items = items
  14. self._index = 0
  15. def __iter__(self):
  16. while True:
  17. try:
  18. item = self._items[self._index]
  19. except IndexError:
  20. raise StopIteration
  21. yield item
  22. self._index += 1
  23. def readline(self):
  24. try:
  25. item = self._items[self._index]
  26. except IndexError:
  27. return ''
  28. self._index += 1
  29. return item
  30. class TestPatchStream(unittest.TestCase):
  31. def test_is_empty(self):
  32. stream = cdiff.PatchStream(Sequential([]))
  33. self.assertTrue(stream.is_empty())
  34. stream = cdiff.PatchStream(Sequential(['hello', 'world']))
  35. self.assertFalse(stream.is_empty())
  36. def test_read_stream_header(self):
  37. stream = cdiff.PatchStream(Sequential([]))
  38. self.assertEqual(
  39. stream.read_stream_header(1),
  40. [])
  41. items = ['hello', 'world', 'again']
  42. stream = cdiff.PatchStream(Sequential(items))
  43. self.assertEqual(stream.read_stream_header(2), items[:2])
  44. stream = cdiff.PatchStream(Sequential(items))
  45. self.assertEqual(stream.read_stream_header(4), items[:])
  46. def test_iter_after_read_stream_header(self):
  47. items = ['hello', 'world', 'again', 'and', 'again']
  48. stream = cdiff.PatchStream(Sequential(items))
  49. out = []
  50. _ = stream.read_stream_header(2)
  51. for item in stream:
  52. out.append(item)
  53. self.assertEqual(out, items)
  54. class TestHunk(unittest.TestCase):
  55. def test_mdiff(self):
  56. pass
  57. def test_get_old_text(self):
  58. pass
  59. def test_get_new_text(self):
  60. pass
  61. class TestDiff(unittest.TestCase):
  62. def test_markup_mix(self):
  63. pass
  64. def test_markup_traditional(self):
  65. pass
  66. def test_markup_side_by_side(self):
  67. pass
  68. class TestUdiff(unittest.TestCase):
  69. diff = cdiff.Udiff(None, None, None, None)
  70. def test_is_hunk_meta_normal(self):
  71. self.assertTrue(self.diff.is_hunk_meta('@@ -1 +1 @@'))
  72. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@'))
  73. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@ class Foo'))
  74. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@ class Foo\n'))
  75. self.assertTrue(self.diff.is_hunk_meta('@@ -3,7 +3,6 @@ class Foo\r\n'))
  76. def test_is_hunk_meta_svn_prop(self):
  77. self.assertTrue(self.diff.is_hunk_meta('## -0,0 +1 ##'))
  78. self.assertTrue(self.diff.is_hunk_meta('## -0,0 +1 ##\n'))
  79. self.assertTrue(self.diff.is_hunk_meta('## -0,0 +1 ##\r\n'))
  80. def test_is_hunk_meta_neg(self):
  81. self.assertFalse(self.diff.is_hunk_meta('@@ -1 + @@'))
  82. self.assertFalse(self.diff.is_hunk_meta('@@ -this is not a hunk meta'))
  83. self.assertFalse(self.diff.is_hunk_meta('## -this is not either'))
  84. def test_parse_hunk_meta_normal(self):
  85. self.assertEqual(
  86. self.diff.parse_hunk_meta('@@ -3,7 +3,6 @@'),
  87. ((3, 7), (3, 6)))
  88. def test_parse_hunk_meta_missing(self):
  89. self.assertEqual(
  90. self.diff.parse_hunk_meta('@@ -3 +3,6 @@'),
  91. ((3, 0), (3, 6)))
  92. self.assertEqual(
  93. self.diff.parse_hunk_meta('@@ -3,7 +3 @@'),
  94. ((3, 7), (3, 0)))
  95. self.assertEqual(
  96. self.diff.parse_hunk_meta('@@ -3 +3 @@'),
  97. ((3, 0), (3, 0)))
  98. def test_parse_hunk_meta_svn_prop(self):
  99. self.assertEqual(
  100. self.diff.parse_hunk_meta('## -0,0 +1 ##'),
  101. ((0, 0), (1, 0)))
  102. def test_is_old(self):
  103. self.assertTrue(self.diff.is_old('-hello world'))
  104. self.assertTrue(self.diff.is_old('----')) # yaml
  105. def test_is_old_neg(self):
  106. self.assertFalse(self.diff.is_old('--- considered as old path'))
  107. self.assertFalse(self.diff.is_old('-------------')) # svn log --diff
  108. def test_is_new(self):
  109. self.assertTrue(self.diff.is_new('+hello world'))
  110. self.assertTrue(self.diff.is_new('++++hello world'))
  111. def test_is_new_neg(self):
  112. self.assertFalse(self.diff.is_new('+++ considered as new path'))
  113. class TestDiffParser(unittest.TestCase):
  114. def test_type_detect(self):
  115. items = ['spam\n', '--- README\n', '+++ README\n', '@@ -3,7 +3,6 @@\n']
  116. stream = cdiff.PatchStream(Sequential(items))
  117. parser = cdiff.DiffParser(stream)
  118. self.assertEqual(parser._type, 'udiff')
  119. def test_type_detect_neg(self):
  120. items = ['spam\n', '--- README\n', '+++ README\n', 'spam\n']
  121. stream = cdiff.PatchStream(Sequential(items))
  122. try:
  123. parser = cdiff.DiffParser(stream)
  124. except:
  125. e = sys.exc_info()[1]
  126. self.assertTrue(isinstance(e, RuntimeError))
  127. def test_parser(self):
  128. pass
  129. if __name__ == '__main__':
  130. unittest.main()
  131. # vim:set et sts=4 sw=4 tw=80: