Nessuna descrizione

UnifiedDiffAssertTraitTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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\Utils;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * @covers SebastianBergmann\Diff\Utils\UnifiedDiffAssertTrait
  14. */
  15. final class UnifiedDiffAssertTraitTest extends TestCase
  16. {
  17. use UnifiedDiffAssertTrait;
  18. /**
  19. * @param string $diff
  20. *
  21. * @dataProvider provideValidCases
  22. */
  23. public function testValidCases(string $diff): void
  24. {
  25. $this->assertValidUnifiedDiffFormat($diff);
  26. }
  27. public function provideValidCases(): array
  28. {
  29. return [
  30. [
  31. '--- Original
  32. +++ New
  33. @@ -8 +8 @@
  34. -Z
  35. +U
  36. ',
  37. ],
  38. [
  39. '--- Original
  40. +++ New
  41. @@ -8 +8 @@
  42. -Z
  43. +U
  44. @@ -15 +15 @@
  45. -X
  46. +V
  47. ',
  48. ],
  49. 'empty diff. is valid' => [
  50. '',
  51. ],
  52. ];
  53. }
  54. public function testNoLinebreakEnd(): void
  55. {
  56. $this->expectException(\UnexpectedValueException::class);
  57. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Expected diff to end with a line break, got "C".', '#')));
  58. $this->assertValidUnifiedDiffFormat("A\nB\nC");
  59. }
  60. public function testInvalidStartWithoutHeader(): void
  61. {
  62. $this->expectException(\UnexpectedValueException::class);
  63. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote("Expected line to start with '@', '-' or '+', got \"A\n\". Line 1.", '#')));
  64. $this->assertValidUnifiedDiffFormat("A\n");
  65. }
  66. public function testInvalidStartHeader1(): void
  67. {
  68. $this->expectException(\UnexpectedValueException::class);
  69. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote("Line 1 indicates a header, so line 2 must start with \"+++\".\nLine 1: \"--- A\n\"\nLine 2: \"+ 1\n\".", '#')));
  70. $this->assertValidUnifiedDiffFormat("--- A\n+ 1\n");
  71. }
  72. public function testInvalidStartHeader2(): void
  73. {
  74. $this->expectException(\UnexpectedValueException::class);
  75. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote("Header line does not match expected pattern, got \"+++ file X\n\". Line 2.", '#')));
  76. $this->assertValidUnifiedDiffFormat("--- A\n+++ file\tX\n");
  77. }
  78. public function testInvalidStartHeader3(): void
  79. {
  80. $this->expectException(\UnexpectedValueException::class);
  81. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Date of header line does not match expected pattern, got "[invalid date]". Line 1.', '#')));
  82. $this->assertValidUnifiedDiffFormat(
  83. "--- Original\t[invalid date]
  84. +++ New
  85. @@ -1,2 +1,2 @@
  86. -A
  87. +B
  88. " . '
  89. '
  90. );
  91. }
  92. public function testInvalidStartHeader4(): void
  93. {
  94. $this->expectException(\UnexpectedValueException::class);
  95. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote("Expected header line to start with \"+++ \", got \"+++INVALID\n\". Line 2.", '#')));
  96. $this->assertValidUnifiedDiffFormat(
  97. '--- Original
  98. +++INVALID
  99. @@ -1,2 +1,2 @@
  100. -A
  101. +B
  102. ' . '
  103. '
  104. );
  105. }
  106. public function testInvalidLine1(): void
  107. {
  108. $this->expectException(\UnexpectedValueException::class);
  109. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote("Expected line to start with '@', '-' or '+', got \"1\n\". Line 5.", '#')));
  110. $this->assertValidUnifiedDiffFormat(
  111. '--- Original
  112. +++ New
  113. @@ -8 +8 @@
  114. -Z
  115. 1
  116. +U
  117. '
  118. );
  119. }
  120. public function testInvalidLine2(): void
  121. {
  122. $this->expectException(\UnexpectedValueException::class);
  123. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Expected string length of minimal 2, got 1. Line 4.', '#')));
  124. $this->assertValidUnifiedDiffFormat(
  125. '--- Original
  126. +++ New
  127. @@ -8 +8 @@
  128. '
  129. );
  130. }
  131. public function testHunkInvalidFormat(): void
  132. {
  133. $this->expectException(\UnexpectedValueException::class);
  134. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote("Hunk header line does not match expected pattern, got \"@@ INVALID -1,1 +1,1 @@\n\". Line 3.", '#')));
  135. $this->assertValidUnifiedDiffFormat(
  136. '--- Original
  137. +++ New
  138. @@ INVALID -1,1 +1,1 @@
  139. -Z
  140. +U
  141. '
  142. );
  143. }
  144. public function testHunkOverlapFrom(): void
  145. {
  146. $this->expectException(\UnexpectedValueException::class);
  147. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected new hunk; "from" (\'-\') start overlaps previous hunk. Line 6.', '#')));
  148. $this->assertValidUnifiedDiffFormat(
  149. '--- Original
  150. +++ New
  151. @@ -8,1 +8,1 @@
  152. -Z
  153. +U
  154. @@ -7,1 +9,1 @@
  155. -Z
  156. +U
  157. '
  158. );
  159. }
  160. public function testHunkOverlapTo(): void
  161. {
  162. $this->expectException(\UnexpectedValueException::class);
  163. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected new hunk; "to" (\'+\') start overlaps previous hunk. Line 6.', '#')));
  164. $this->assertValidUnifiedDiffFormat(
  165. '--- Original
  166. +++ New
  167. @@ -8,1 +8,1 @@
  168. -Z
  169. +U
  170. @@ -17,1 +7,1 @@
  171. -Z
  172. +U
  173. '
  174. );
  175. }
  176. public function testExpectHunk1(): void
  177. {
  178. $this->expectException(\UnexpectedValueException::class);
  179. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Expected hunk start (\'@\'), got "+". Line 6.', '#')));
  180. $this->assertValidUnifiedDiffFormat(
  181. '--- Original
  182. +++ New
  183. @@ -8 +8 @@
  184. -Z
  185. +U
  186. +O
  187. '
  188. );
  189. }
  190. public function testExpectHunk2(): void
  191. {
  192. $this->expectException(\UnexpectedValueException::class);
  193. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected hunk start (\'@\'). Line 6.', '#')));
  194. $this->assertValidUnifiedDiffFormat(
  195. '--- Original
  196. +++ New
  197. @@ -8,12 +8,12 @@
  198. ' . '
  199. ' . '
  200. @@ -38,12 +48,12 @@
  201. '
  202. );
  203. }
  204. public function testMisplacedLineAfterComments1(): void
  205. {
  206. $this->expectException(\UnexpectedValueException::class);
  207. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected line as 2 "No newline" markers have found, ". Line 8.', '#')));
  208. $this->assertValidUnifiedDiffFormat(
  209. '--- Original
  210. +++ New
  211. @@ -8 +8 @@
  212. -Z
  213. \ No newline at end of file
  214. +U
  215. \ No newline at end of file
  216. +A
  217. '
  218. );
  219. }
  220. public function testMisplacedLineAfterComments2(): void
  221. {
  222. $this->expectException(\UnexpectedValueException::class);
  223. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected line as 2 "No newline" markers have found, ". Line 7.', '#')));
  224. $this->assertValidUnifiedDiffFormat(
  225. '--- Original
  226. +++ New
  227. @@ -8 +8 @@
  228. +U
  229. \ No newline at end of file
  230. \ No newline at end of file
  231. \ No newline at end of file
  232. '
  233. );
  234. }
  235. public function testMisplacedLineAfterComments3(): void
  236. {
  237. $this->expectException(\UnexpectedValueException::class);
  238. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected line as 2 "No newline" markers have found, ". Line 7.', '#')));
  239. $this->assertValidUnifiedDiffFormat(
  240. '--- Original
  241. +++ New
  242. @@ -8 +8 @@
  243. +U
  244. \ No newline at end of file
  245. \ No newline at end of file
  246. +A
  247. '
  248. );
  249. }
  250. public function testMisplacedComment(): void
  251. {
  252. $this->expectException(\UnexpectedValueException::class);
  253. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected "\ No newline at end of file", it must be preceded by \'+\' or \'-\' line. Line 1.', '#')));
  254. $this->assertValidUnifiedDiffFormat(
  255. '\ No newline at end of file
  256. '
  257. );
  258. }
  259. public function testUnexpectedDuplicateNoNewLineEOF(): void
  260. {
  261. $this->expectException(\UnexpectedValueException::class);
  262. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected "\\ No newline at end of file", "\\" was already closed. Line 8.', '#')));
  263. $this->assertValidUnifiedDiffFormat(
  264. '--- Original
  265. +++ New
  266. @@ -8,12 +8,12 @@
  267. ' . '
  268. ' . '
  269. \ No newline at end of file
  270. ' . '
  271. \ No newline at end of file
  272. '
  273. );
  274. }
  275. public function testFromAfterClose(): void
  276. {
  277. $this->expectException(\UnexpectedValueException::class);
  278. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Not expected from (\'-\'), already closed by "\ No newline at end of file". Line 6.', '#')));
  279. $this->assertValidUnifiedDiffFormat(
  280. '--- Original
  281. +++ New
  282. @@ -8,12 +8,12 @@
  283. -A
  284. \ No newline at end of file
  285. -A
  286. \ No newline at end of file
  287. '
  288. );
  289. }
  290. public function testSameAfterFromClose(): void
  291. {
  292. $this->expectException(\UnexpectedValueException::class);
  293. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Not expected same (\' \'), \'-\' already closed by "\ No newline at end of file". Line 6.', '#')));
  294. $this->assertValidUnifiedDiffFormat(
  295. '--- Original
  296. +++ New
  297. @@ -8,12 +8,12 @@
  298. -A
  299. \ No newline at end of file
  300. A
  301. \ No newline at end of file
  302. '
  303. );
  304. }
  305. public function testToAfterClose(): void
  306. {
  307. $this->expectException(\UnexpectedValueException::class);
  308. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Not expected to (\'+\'), already closed by "\ No newline at end of file". Line 6.', '#')));
  309. $this->assertValidUnifiedDiffFormat(
  310. '--- Original
  311. +++ New
  312. @@ -8,12 +8,12 @@
  313. +A
  314. \ No newline at end of file
  315. +A
  316. \ No newline at end of file
  317. '
  318. );
  319. }
  320. public function testSameAfterToClose(): void
  321. {
  322. $this->expectException(\UnexpectedValueException::class);
  323. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Not expected same (\' \'), \'+\' already closed by "\ No newline at end of file". Line 6.', '#')));
  324. $this->assertValidUnifiedDiffFormat(
  325. '--- Original
  326. +++ New
  327. @@ -8,12 +8,12 @@
  328. +A
  329. \ No newline at end of file
  330. A
  331. \ No newline at end of file
  332. '
  333. );
  334. }
  335. public function testUnexpectedEOFFromMissingLines(): void
  336. {
  337. $this->expectException(\UnexpectedValueException::class);
  338. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected EOF, number of lines in hunk "from" (\'-\')) mismatched. Line 7.', '#')));
  339. $this->assertValidUnifiedDiffFormat(
  340. '--- Original
  341. +++ New
  342. @@ -8,19 +7,2 @@
  343. -A
  344. +B
  345. ' . '
  346. '
  347. );
  348. }
  349. public function testUnexpectedEOFToMissingLines(): void
  350. {
  351. $this->expectException(\UnexpectedValueException::class);
  352. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected EOF, number of lines in hunk "to" (\'+\')) mismatched. Line 7.', '#')));
  353. $this->assertValidUnifiedDiffFormat(
  354. '--- Original
  355. +++ New
  356. @@ -8,2 +7,3 @@
  357. -A
  358. +B
  359. ' . '
  360. '
  361. );
  362. }
  363. public function testUnexpectedEOFBothFromAndToMissingLines(): void
  364. {
  365. $this->expectException(\UnexpectedValueException::class);
  366. $this->expectExceptionMessageRegExp(\sprintf('#^%s$#', \preg_quote('Unexpected EOF, number of lines in hunk "from" (\'-\')) and "to" (\'+\') mismatched. Line 7.', '#')));
  367. $this->assertValidUnifiedDiffFormat(
  368. '--- Original
  369. +++ New
  370. @@ -1,12 +1,14 @@
  371. -A
  372. +B
  373. ' . '
  374. '
  375. );
  376. }
  377. }