DiffHelperTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. App::uses('DiffHelper', 'Tools.View/Helper');
  3. App::uses('HtmlHelper', 'View/Helper');
  4. App::uses('View', 'View');
  5. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  6. /**
  7. * render: unified/inline
  8. * engine: native/shell (shell only on linux!)
  9. */
  10. class DiffHelperTest extends MyCakeTestCase {
  11. /**
  12. * SetUp method
  13. */
  14. public function setUp() {
  15. parent::setUp();
  16. $this->Diff = new DiffHelper(new View(null));
  17. $this->Diff->Html = new HtmlHelper(new View(null));
  18. $style = <<<CSS
  19. <style type="text/css">
  20. del {
  21. color: red;
  22. }
  23. ins {
  24. color: green;
  25. }
  26. </style>
  27. CSS;
  28. $this->out($style);
  29. }
  30. public function testAutoEngine() {
  31. $engine = extension_loaded('xdiff') ? 'Xdiff' : 'Native';
  32. $this->out('auto engine: ' . $engine);
  33. }
  34. /**
  35. * String renderer
  36. * source: 'context', 'unified', or 'autodetect'
  37. * engine:
  38. * - auto
  39. * - context from unified
  40. * - unified from context
  41. */
  42. public function testReverse() {
  43. $this->out('String - autodetect', false);
  44. $text = <<<TEXT
  45. ***************
  46. *** 1 ****
  47. ! 99999999777
  48. --- 1 ----
  49. ! 9999944449977
  50. TEXT;
  51. $res = $this->Diff->reverse($text);
  52. $this->out($res);
  53. $this->out('String - Context - render as Unified', false);
  54. $text = <<<TEXT
  55. ***************
  56. *** 1 ****
  57. ! 99999999777
  58. --- 1 ----
  59. ! 9999944449977
  60. TEXT;
  61. $this->Diff->renderType('unified');
  62. $res = $this->Diff->reverse($text, array('mode' => 'context'));
  63. $this->out($res);
  64. }
  65. /**
  66. * @expectedException HORDE_TEXT_DIFF_EXCEPTION
  67. */
  68. public function testReverseUnifiedDiffNotDetectable() {
  69. $this->out('Unified - String', false);
  70. $text = <<<TEXT
  71. @@ -1,3 +1,3 @@
  72. 1dfdf
  73. -jtzth6h6h6th6
  74. +jtzh6h6th6
  75. xcsdfdf
  76. TEXT;
  77. $this->Diff->reverse($text);
  78. }
  79. /**
  80. * Auto engine + inline Render
  81. * Fastest way
  82. *
  83. */
  84. public function testDiffDefault() {
  85. $t1 = array(
  86. 'errgrshrth',
  87. 'srhrthrt777 ssshsrjtz jrjtjtjt',
  88. '1dfdf' . PHP_EOL . 'jtzth6h6h6th6' . PHP_EOL . 'xcsdfdf',
  89. '99999999777'
  90. );
  91. $t2 = array(
  92. 'errgrsh3333rth',
  93. 'srhrthrt777 hsrthsrjt888 jrjtjtjt',
  94. '1dfdf' . PHP_EOL . 'jtzh6h6th6' . PHP_EOL . 'xcsdfdf',
  95. '9999944449977'
  96. );
  97. $this->out('Inline - auto', false);
  98. for ($i = 0; $i < 4; $i++) {
  99. $res = $this->Diff->compare($t1[$i], $t2[$i]);
  100. $this->out($res);
  101. }
  102. }
  103. /**
  104. * Inline render
  105. * engine:
  106. * - native
  107. * - shell
  108. * - xdiff (skip if not available)
  109. */
  110. public function testDiffInline() {
  111. $t1 = array(
  112. 'errgrshrth',
  113. 'srhrthrt777 ssshsrjtz jrjtjtjt',
  114. '1dfdf' . PHP_EOL . 'jtzth6h6h6th6' . PHP_EOL . 'xcsdfdf',
  115. '99999999777'
  116. );
  117. $t2 = array(
  118. 'errgrsh3333rth',
  119. 'srhrthrt777 hsrthsrjt888 jrjtjtjt',
  120. '1dfdf' . PHP_EOL . 'jtzh6h6th6' . PHP_EOL . 'xcsdfdf',
  121. '9999944449977'
  122. );
  123. $this->out('Inline - Native', false);
  124. for ($i = 0; $i < 4; $i++) {
  125. $this->assertTrue($this->Diff->renderType('inline'));
  126. $this->assertTrue($this->Diff->engineType('native'));
  127. $res = $this->Diff->compare($t1[$i], $t2[$i]);
  128. $this->out($res);
  129. }
  130. $this->out('Inline - Shell', false);
  131. for ($i = 0; $i < 4; $i++) {
  132. $this->assertTrue($this->Diff->renderType('inline'));
  133. $this->assertTrue($this->Diff->engineType('shell'));
  134. $res = $this->Diff->compare($t1[$i], $t2[$i]);
  135. $this->out($res);
  136. }
  137. $this->skipIf(!extension_loaded('xdiff'), 'xdiff not available');
  138. $this->out('Inline - Xdiff', false);
  139. for ($i = 0; $i < 4; $i++) {
  140. $this->assertTrue($this->Diff->renderType('inline'));
  141. $this->assertTrue($this->Diff->engineType('xdiff'));
  142. $res = $this->Diff->compare($t1[$i], $t2[$i]);
  143. $this->out($res);
  144. }
  145. }
  146. /**
  147. * Unified renderer
  148. */
  149. public function testDiffUnified() {
  150. $t1 = array(
  151. 'errgrshrth',
  152. 'srhrthrt777 ssshsrjtz jrjtjtjt',
  153. '1dfdf' . PHP_EOL . 'jtzth6h6h6th6' . PHP_EOL . 'xcsdfdf',
  154. '99999999777'
  155. );
  156. $t2 = array(
  157. 'errgrsh3333rth',
  158. 'srhrthrt777 hsrthsrjt888 jrjtjtjt',
  159. '1dfdf' . PHP_EOL . 'jtzh6h6th6' . PHP_EOL . 'xcsdfdf',
  160. '9999944449977'
  161. );
  162. $max = 4;
  163. $this->out('Unified - Native', false);
  164. for ($i = 0; $i < $max; $i++) {
  165. $this->assertTrue($this->Diff->renderType('unified'));
  166. $this->assertTrue($this->Diff->engineType('native'));
  167. $res = $this->Diff->compare($t1[$i], $t2[$i]);
  168. $this->out($res);
  169. }
  170. $this->out('Unified - Shell', false);
  171. for ($i = 0; $i < 4; $i++) {
  172. $this->assertTrue($this->Diff->renderType('unified'));
  173. $this->assertTrue($this->Diff->engineType('shell'));
  174. $res = $this->Diff->compare($t1[$i], $t2[$i]);
  175. $this->out($res);
  176. }
  177. $this->skipIf(!extension_loaded('xdiff'), 'xdiff not available');
  178. $this->out('Unified - Xdiff', false);
  179. for ($i = 0; $i < $max; $i++) {
  180. $this->assertTrue($this->Diff->renderType('unified'));
  181. $this->assertTrue($this->Diff->engineType('xdiff'));
  182. $res = $this->Diff->compare($t1[$i], $t2[$i]);
  183. $this->out($res);
  184. }
  185. }
  186. /**
  187. * Context renderer
  188. */
  189. public function testDiffContext() {
  190. $t1 = array(
  191. 'errgrshrth',
  192. 'srhrthrt777 ssshsrjtz jrjtjtjt',
  193. '1dfdf' . PHP_EOL . 'jtzth6h6h6th6' . PHP_EOL . 'xcsdfdf',
  194. '99999999777'
  195. );
  196. $t2 = array(
  197. 'errgrsh3333rth',
  198. 'srhrthrt777 hsrthsrjt888 jrjtjtjt',
  199. '1dfdf' . PHP_EOL . 'jtzh6h6th6' . PHP_EOL . 'xcsdfdf',
  200. '9999944449977'
  201. );
  202. $this->out('Context - Native', false);
  203. for ($i = 0; $i < 4; $i++) {
  204. $this->assertTrue($this->Diff->renderType('context'));
  205. $this->assertTrue($this->Diff->engineType('native'));
  206. $res = $this->Diff->compare($t1[$i], $t2[$i]);
  207. $this->out($res);
  208. }
  209. $this->out('Context - Shell', false);
  210. for ($i = 0; $i < 4; $i++) {
  211. $this->assertTrue($this->Diff->renderType('context'));
  212. $this->assertTrue($this->Diff->engineType('shell'));
  213. $res = $this->Diff->compare($t1[$i], $t2[$i]);
  214. $this->out($res);
  215. }
  216. }
  217. }