AbstractChunkOutputBuilderTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/diff.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Diff\Output;
  11. use PHPUnit\Framework\TestCase;
  12. use SebastianBergmann\Diff\Differ;
  13. /**
  14. * @covers SebastianBergmann\Diff\Output\AbstractChunkOutputBuilder
  15. *
  16. * @uses SebastianBergmann\Diff\Differ
  17. * @uses SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder
  18. * @uses SebastianBergmann\Diff\TimeEfficientLongestCommonSubsequenceCalculator
  19. */
  20. final class AbstractChunkOutputBuilderTest extends TestCase
  21. {
  22. /**
  23. * @param array $expected
  24. * @param string $from
  25. * @param string $to
  26. * @param int $lineThreshold
  27. *
  28. * @dataProvider provideGetCommonChunks
  29. */
  30. public function testGetCommonChunks(array $expected, string $from, string $to, int $lineThreshold = 5): void
  31. {
  32. $output = new class extends AbstractChunkOutputBuilder {
  33. public function getDiff(array $diff): string
  34. {
  35. return '';
  36. }
  37. public function getChunks(array $diff, $lineThreshold)
  38. {
  39. return $this->getCommonChunks($diff, $lineThreshold);
  40. }
  41. };
  42. $this->assertSame(
  43. $expected,
  44. $output->getChunks((new Differ)->diffToArray($from, $to), $lineThreshold)
  45. );
  46. }
  47. public function provideGetCommonChunks(): array
  48. {
  49. return[
  50. 'same (with default threshold)' => [
  51. [],
  52. 'A',
  53. 'A',
  54. ],
  55. 'same (threshold 0)' => [
  56. [0 => 0],
  57. 'A',
  58. 'A',
  59. 0,
  60. ],
  61. 'empty' => [
  62. [],
  63. '',
  64. '',
  65. ],
  66. 'single line diff' => [
  67. [],
  68. 'A',
  69. 'B',
  70. ],
  71. 'below threshold I' => [
  72. [],
  73. "A\nX\nC",
  74. "A\nB\nC",
  75. ],
  76. 'below threshold II' => [
  77. [],
  78. "A\n\n\n\nX\nC",
  79. "A\n\n\n\nB\nC",
  80. ],
  81. 'below threshold III' => [
  82. [0 => 5],
  83. "A\n\n\n\n\n\nB",
  84. "A\n\n\n\n\n\nA",
  85. ],
  86. 'same start' => [
  87. [0 => 5],
  88. "A\n\n\n\n\n\nX\nC",
  89. "A\n\n\n\n\n\nB\nC",
  90. ],
  91. 'same start long' => [
  92. [0 => 13],
  93. "\n\n\n\n\n\n\n\n\n\n\n\n\n\nA",
  94. "\n\n\n\n\n\n\n\n\n\n\n\n\n\nB",
  95. ],
  96. 'same part in between' => [
  97. [2 => 8],
  98. "A\n\n\n\n\n\n\nX\nY\nZ\n\n",
  99. "B\n\n\n\n\n\n\nX\nA\nZ\n\n",
  100. ],
  101. 'same trailing' => [
  102. [2 => 14],
  103. "A\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
  104. "B\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
  105. ],
  106. 'same part in between, same trailing' => [
  107. [2 => 7, 10 => 15],
  108. "A\n\n\n\n\n\n\nA\n\n\n\n\n\n\n",
  109. "B\n\n\n\n\n\n\nB\n\n\n\n\n\n\n",
  110. ],
  111. 'below custom threshold I' => [
  112. [],
  113. "A\n\nB",
  114. "A\n\nD",
  115. 2,
  116. ],
  117. 'custom threshold I' => [
  118. [0 => 1],
  119. "A\n\nB",
  120. "A\n\nD",
  121. 1,
  122. ],
  123. 'custom threshold II' => [
  124. [],
  125. "A\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
  126. "A\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
  127. 19,
  128. ],
  129. [
  130. [3 => 9],
  131. "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk",
  132. "a\np\nc\nd\ne\nf\ng\nh\ni\nw\nk",
  133. ],
  134. [
  135. [0 => 5, 8 => 13],
  136. "A\nA\nA\nA\nA\nA\nX\nC\nC\nC\nC\nC\nC",
  137. "A\nA\nA\nA\nA\nA\nB\nC\nC\nC\nC\nC\nC",
  138. ],
  139. [
  140. [0 => 5, 8 => 13],
  141. "A\nA\nA\nA\nA\nA\nX\nC\nC\nC\nC\nC\nC\nX",
  142. "A\nA\nA\nA\nA\nA\nB\nC\nC\nC\nC\nC\nC\nY",
  143. ],
  144. ];
  145. }
  146. }