StringTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node\Scalar;
  3. class StringTest extends \PHPUnit\Framework\TestCase
  4. {
  5. /**
  6. * @dataProvider provideTestParseEscapeSequences
  7. */
  8. public function testParseEscapeSequences($expected, $string, $quote) {
  9. $this->assertSame(
  10. $expected,
  11. String_::parseEscapeSequences($string, $quote)
  12. );
  13. }
  14. /**
  15. * @dataProvider provideTestParse
  16. */
  17. public function testCreate($expected, $string) {
  18. $this->assertSame(
  19. $expected,
  20. String_::parse($string)
  21. );
  22. }
  23. public function provideTestParseEscapeSequences() {
  24. return [
  25. ['"', '\\"', '"'],
  26. ['\\"', '\\"', '`'],
  27. ['\\"\\`', '\\"\\`', null],
  28. ["\\\$\n\r\t\f\v", '\\\\\$\n\r\t\f\v', null],
  29. ["\x1B", '\e', null],
  30. [chr(255), '\xFF', null],
  31. [chr(255), '\377', null],
  32. [chr(0), '\400', null],
  33. ["\0", '\0', null],
  34. ['\xFF', '\\\\xFF', null],
  35. ];
  36. }
  37. public function provideTestParse() {
  38. $tests = [
  39. ['A', '\'A\''],
  40. ['A', 'b\'A\''],
  41. ['A', '"A"'],
  42. ['A', 'b"A"'],
  43. ['\\', '\'\\\\\''],
  44. ['\'', '\'\\\'\''],
  45. ];
  46. foreach ($this->provideTestParseEscapeSequences() as $i => $test) {
  47. // skip second and third tests, they aren't for double quotes
  48. if ($i !== 1 && $i !== 2) {
  49. $tests[] = [$test[0], '"' . $test[1] . '"'];
  50. }
  51. }
  52. return $tests;
  53. }
  54. }