DiffHelperTest.php 5.6 KB

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