Нет описания

DifferTest.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Diff
  4. *
  5. * Copyright (c) 2001-2014, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package Diff
  38. * @author Sebastian Bergmann <sebastian@phpunit.de>
  39. * @copyright 2001-2014 Sebastian Bergmann <sebastian@phpunit.de>
  40. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  41. * @link http://www.github.com/sebastianbergmann/diff
  42. */
  43. namespace SebastianBergmann\Diff;
  44. use PHPUnit_Framework_TestCase;
  45. use SebastianBergmann\Diff\LCS\MemoryEfficientImplementation;
  46. use SebastianBergmann\Diff\LCS\TimeEfficientImplementation;
  47. class DifferTest extends PHPUnit_Framework_TestCase
  48. {
  49. const REMOVED = 2;
  50. const ADDED = 1;
  51. const OLD = 0;
  52. /**
  53. * @var Differ
  54. */
  55. private $differ;
  56. protected function setUp()
  57. {
  58. $this->differ = new Differ;
  59. }
  60. /**
  61. * @param array $expected
  62. * @param string $from
  63. * @param string $to
  64. * @dataProvider arrayProvider
  65. * @covers SebastianBergmann\Diff\Differ::diffToArray
  66. * @covers SebastianBergmann\Diff\LCS\TimeEfficientImplementation
  67. */
  68. public function testArrayRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(array $expected, $from, $to)
  69. {
  70. $this->assertEquals($expected, $this->differ->diffToArray($from, $to, new TimeEfficientImplementation));
  71. }
  72. /**
  73. * @param string $expected
  74. * @param string $from
  75. * @param string $to
  76. * @dataProvider textProvider
  77. * @covers SebastianBergmann\Diff\Differ::diff
  78. * @covers SebastianBergmann\Diff\LCS\TimeEfficientImplementation
  79. */
  80. public function testTextRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation($expected, $from, $to)
  81. {
  82. $this->assertEquals($expected, $this->differ->diff($from, $to, new TimeEfficientImplementation));
  83. }
  84. /**
  85. * @param array $expected
  86. * @param string $from
  87. * @param string $to
  88. * @dataProvider arrayProvider
  89. * @covers SebastianBergmann\Diff\Differ::diffToArray
  90. * @covers SebastianBergmann\Diff\LCS\MemoryEfficientImplementation
  91. */
  92. public function testArrayRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation(array $expected, $from, $to)
  93. {
  94. $this->assertEquals($expected, $this->differ->diffToArray($from, $to, new MemoryEfficientImplementation));
  95. }
  96. /**
  97. * @param string $expected
  98. * @param string $from
  99. * @param string $to
  100. * @dataProvider textProvider
  101. * @covers SebastianBergmann\Diff\Differ::diff
  102. * @covers SebastianBergmann\Diff\LCS\MemoryEfficientImplementation
  103. */
  104. public function testTextRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcsImplementation($expected, $from, $to)
  105. {
  106. $this->assertEquals($expected, $this->differ->diff($from, $to, new MemoryEfficientImplementation));
  107. }
  108. /**
  109. * @covers SebastianBergmann\Diff\Differ::diff
  110. */
  111. public function testCustomHeaderCanBeUsed()
  112. {
  113. $differ = new Differ('CUSTOM HEADER');
  114. $this->assertEquals(
  115. "CUSTOM HEADER@@ @@\n-a\n+b\n",
  116. $differ->diff('a', 'b')
  117. );
  118. }
  119. public function arrayProvider()
  120. {
  121. return array(
  122. array(
  123. array(
  124. array('a', self::REMOVED),
  125. array('b', self::ADDED)
  126. ),
  127. 'a',
  128. 'b'
  129. ),
  130. array(
  131. array(
  132. array('ba', self::REMOVED),
  133. array('bc', self::ADDED)
  134. ),
  135. 'ba',
  136. 'bc'
  137. ),
  138. array(
  139. array(
  140. array('ab', self::REMOVED),
  141. array('cb', self::ADDED)
  142. ),
  143. 'ab',
  144. 'cb'
  145. ),
  146. array(
  147. array(
  148. array('abc', self::REMOVED),
  149. array('adc', self::ADDED)
  150. ),
  151. 'abc',
  152. 'adc'
  153. ),
  154. array(
  155. array(
  156. array('ab', self::REMOVED),
  157. array('abc', self::ADDED)
  158. ),
  159. 'ab',
  160. 'abc'
  161. ),
  162. array(
  163. array(
  164. array('bc', self::REMOVED),
  165. array('abc', self::ADDED)
  166. ),
  167. 'bc',
  168. 'abc'
  169. ),
  170. array(
  171. array(
  172. array('abc', self::REMOVED),
  173. array('abbc', self::ADDED)
  174. ),
  175. 'abc',
  176. 'abbc'
  177. ),
  178. array(
  179. array(
  180. array('abcdde', self::REMOVED),
  181. array('abcde', self::ADDED)
  182. ),
  183. 'abcdde',
  184. 'abcde'
  185. )
  186. );
  187. }
  188. public function textProvider()
  189. {
  190. return array(
  191. array(
  192. "--- Original\n+++ New\n@@ @@\n-a\n+b\n",
  193. 'a',
  194. 'b'
  195. ),
  196. array(
  197. "--- Original\n+++ New\n@@ @@\n-ba\n+bc\n",
  198. 'ba',
  199. 'bc'
  200. ),
  201. array(
  202. "--- Original\n+++ New\n@@ @@\n-ab\n+cb\n",
  203. 'ab',
  204. 'cb'
  205. ),
  206. array(
  207. "--- Original\n+++ New\n@@ @@\n-abc\n+adc\n",
  208. 'abc',
  209. 'adc'
  210. ),
  211. array(
  212. "--- Original\n+++ New\n@@ @@\n-ab\n+abc\n",
  213. 'ab',
  214. 'abc'
  215. ),
  216. array(
  217. "--- Original\n+++ New\n@@ @@\n-bc\n+abc\n",
  218. 'bc',
  219. 'abc'
  220. ),
  221. array(
  222. "--- Original\n+++ New\n@@ @@\n-abc\n+abbc\n",
  223. 'abc',
  224. 'abbc'
  225. ),
  226. array(
  227. "--- Original\n+++ New\n@@ @@\n-abcdde\n+abcde\n",
  228. 'abcdde',
  229. 'abcde'
  230. ),
  231. );
  232. }
  233. }