LibeditTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  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 Psy\Test\Readline;
  11. use Psy\Readline\Libedit;
  12. class LibeditTest extends \Psy\Test\TestCase
  13. {
  14. private $historyFile;
  15. /**
  16. * @before
  17. */
  18. public function getReady()
  19. {
  20. if (!Libedit::isSupported()) {
  21. $this->markTestSkipped('Libedit not enabled');
  22. }
  23. $this->historyFile = \tempnam(\sys_get_temp_dir(), 'psysh_test_history');
  24. if (false === \file_put_contents($this->historyFile, "_HiStOrY_V2_\n")) {
  25. $this->fail('Unable to write history file: '.$this->historyFile);
  26. }
  27. \readline_clear_history();
  28. }
  29. /**
  30. * @after
  31. */
  32. public function removeHistoryFile()
  33. {
  34. if (\is_file($this->historyFile)) {
  35. \unlink($this->historyFile);
  36. }
  37. }
  38. public function testReadlineName()
  39. {
  40. $readline = new Libedit($this->historyFile);
  41. $this->assertSame(\readline_info('readline_name'), 'psysh');
  42. }
  43. public function testHistory()
  44. {
  45. $readline = new Libedit($this->historyFile);
  46. $this->assertEmpty($readline->listHistory());
  47. $readline->addHistory('foo');
  48. $this->assertSame(['foo'], $readline->listHistory());
  49. $readline->addHistory('bar');
  50. $this->assertSame(['foo', 'bar'], $readline->listHistory());
  51. $readline->addHistory('baz');
  52. $this->assertSame(['foo', 'bar', 'baz'], $readline->listHistory());
  53. $readline->clearHistory();
  54. $this->assertEmpty($readline->listHistory());
  55. }
  56. /**
  57. * @depends testHistory
  58. */
  59. public function testHistorySize()
  60. {
  61. $readline = new Libedit($this->historyFile, 2);
  62. $this->assertEmpty($readline->listHistory());
  63. $readline->addHistory('foo');
  64. $readline->addHistory('bar');
  65. $this->assertSame(['foo', 'bar'], $readline->listHistory());
  66. $readline->addHistory('baz');
  67. $this->assertSame(['bar', 'baz'], $readline->listHistory());
  68. $readline->addHistory('w00t');
  69. $this->assertSame(['baz', 'w00t'], $readline->listHistory());
  70. $readline->clearHistory();
  71. $this->assertEmpty($readline->listHistory());
  72. }
  73. /**
  74. * @depends testHistory
  75. */
  76. public function testHistoryEraseDups()
  77. {
  78. $readline = new Libedit($this->historyFile, 0, true);
  79. $this->assertEmpty($readline->listHistory());
  80. $readline->addHistory('foo');
  81. $readline->addHistory('bar');
  82. $readline->addHistory('foo');
  83. $this->assertSame(['bar', 'foo'], $readline->listHistory());
  84. $readline->addHistory('baz');
  85. $readline->addHistory('w00t');
  86. $readline->addHistory('baz');
  87. $this->assertSame(['bar', 'foo', 'w00t', 'baz'], $readline->listHistory());
  88. $readline->clearHistory();
  89. $this->assertEmpty($readline->listHistory());
  90. }
  91. public function testListHistory()
  92. {
  93. $readline = new Libedit($this->historyFile);
  94. \file_put_contents(
  95. $this->historyFile,
  96. "This is an entry\n\0This is a comment\nThis is an entry\0With a comment\n",
  97. \FILE_APPEND
  98. );
  99. $this->assertSame([
  100. 'This is an entry',
  101. 'This is an entry',
  102. ], $readline->listHistory());
  103. $readline->clearHistory();
  104. }
  105. /**
  106. * Libedit being a BSD library,
  107. * it doesn't support non-unix line separators.
  108. */
  109. public function testLinebreaksSupport()
  110. {
  111. $readline = new Libedit($this->historyFile);
  112. \file_put_contents(
  113. $this->historyFile,
  114. "foo\rbar\nbaz\r\nw00t",
  115. \FILE_APPEND
  116. );
  117. $this->assertSame([
  118. "foo\rbar",
  119. "baz\r",
  120. 'w00t',
  121. ], $readline->listHistory());
  122. $readline->clearHistory();
  123. }
  124. }