GNUReadlineTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\GNUReadline;
  12. class GNUReadlineTest extends \Psy\Test\TestCase
  13. {
  14. private $historyFile;
  15. /**
  16. * @before
  17. */
  18. public function getReady()
  19. {
  20. if (!GNUReadline::isSupported()) {
  21. $this->markTestSkipped('GNUReadline not enabled');
  22. }
  23. $this->historyFile = \tempnam(\sys_get_temp_dir(), 'psysh_test_history');
  24. \file_put_contents($this->historyFile, "_HiStOrY_V2_\n");
  25. }
  26. public function testReadlineName()
  27. {
  28. $readline = new GNUReadline($this->historyFile);
  29. $this->assertSame(\readline_info('readline_name'), 'psysh');
  30. }
  31. public function testHistory()
  32. {
  33. $readline = new GNUReadline($this->historyFile);
  34. $this->assertEmpty($readline->listHistory());
  35. $readline->addHistory('foo');
  36. $this->assertSame(['foo'], $readline->listHistory());
  37. $readline->addHistory('bar');
  38. $this->assertSame(['foo', 'bar'], $readline->listHistory());
  39. $readline->addHistory('baz');
  40. $this->assertSame(['foo', 'bar', 'baz'], $readline->listHistory());
  41. $readline->clearHistory();
  42. $this->assertEmpty($readline->listHistory());
  43. }
  44. /**
  45. * @depends testHistory
  46. */
  47. public function testHistorySize()
  48. {
  49. $readline = new GNUReadline($this->historyFile, 2);
  50. $this->assertEmpty($readline->listHistory());
  51. $readline->addHistory('foo');
  52. $readline->addHistory('bar');
  53. $this->assertSame(['foo', 'bar'], $readline->listHistory());
  54. $readline->addHistory('baz');
  55. $this->assertSame(['bar', 'baz'], $readline->listHistory());
  56. $readline->addHistory('w00t');
  57. $this->assertSame(['baz', 'w00t'], $readline->listHistory());
  58. $readline->clearHistory();
  59. $this->assertEmpty($readline->listHistory());
  60. }
  61. /**
  62. * @depends testHistory
  63. */
  64. public function testHistoryEraseDups()
  65. {
  66. $readline = new GNUReadline($this->historyFile, 0, true);
  67. $this->assertEmpty($readline->listHistory());
  68. $readline->addHistory('foo');
  69. $readline->addHistory('bar');
  70. $readline->addHistory('foo');
  71. $this->assertSame(['bar', 'foo'], $readline->listHistory());
  72. $readline->addHistory('baz');
  73. $readline->addHistory('w00t');
  74. $readline->addHistory('baz');
  75. $this->assertSame(['bar', 'foo', 'w00t', 'baz'], $readline->listHistory());
  76. $readline->clearHistory();
  77. $this->assertEmpty($readline->listHistory());
  78. }
  79. }