TextLibTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. App::uses('TextLib', 'Tools.Utility');
  3. /**
  4. * 2010-07-14 ms
  5. */
  6. class TextLibTest extends CakeTestCase {
  7. public function setUp() {
  8. $this->TextLib = new TextLib(null);
  9. }
  10. public function testScreamFont() {
  11. $strings = array(
  12. 'Some Äext' => false,
  13. 'SOME ÄEXT' => true,
  14. 'ST' => true,
  15. 'SOme TExt' => true,
  16. 'SOme Text' => false,
  17. );
  18. foreach ($strings as $string => $expected) {
  19. $this->TextLib = new TextLib($string);
  20. $is = $this->TextLib->isScreamFont();
  21. //pr($is);
  22. $this->assertSame($expected, $is);
  23. }
  24. }
  25. public function testConvertToOrd() {
  26. $this->TextLib = new TextLib('h H');
  27. $is = $this->TextLib->convertToOrd();
  28. //pr($is);
  29. $this->assertEquals($is, '0-104-32-72-0');
  30. $is = $this->TextLib->convertToOrd('x'.NL.'x'.LF.'x'.PHP_EOL.'x'.CR.'x'.TB.'x');
  31. //pr($is);
  32. }
  33. public function testConvertToOrdTable() {
  34. $is = $this->TextLib->convertToOrdTable('x'.NL.'x'.LF.'x'.PHP_EOL.'x'.CR.'x'.TB.'x');
  35. //pr($is);
  36. }
  37. public function testMaxWords() {
  38. $this->assertEquals('Taylor...', TextLib::maxWords('Taylor Otwell', 1));
  39. $this->assertEquals('Taylor___', TextLib::maxWords('Taylor Otwell', 1, array('ellipsis'=>'___')));
  40. $this->assertEquals('Taylor Otwell', TextLib::maxWords('Taylor Otwell', 3));
  41. }
  42. public function testWords() {
  43. $this->TextLib = new TextLib('Hochhaus, Unter dem Bau von ae Äußeren Einflüssen - und von Autos.');
  44. $is = $this->TextLib->words(array('min_char'=>3));
  45. //pr($is);
  46. $this->assertTrue(!empty($is) && is_array($is) && count($is) === 9);
  47. }
  48. public function testWordCount() {
  49. $this->TextLib = new TextLib('Hochhaus, Unter dem Bau von ae Äußeren Einflüssen - und von Autos.');
  50. $is = $this->TextLib->wordCount(array('min_char'=>3));
  51. //pr($is);
  52. $this->assertTrue(!empty($is) && is_array($is) && count($is) === 9);
  53. $this->TextLib = new TextLib('Hochhaus, Unter dem Bau von ae Äußeren Einflüssen - und von Autos.');
  54. $is = $this->TextLib->wordCount(array('min_char'=>3, 'sort'=>'DESC', 'limit'=>5));
  55. //pr($is);
  56. $this->assertTrue(!empty($is) && is_array($is) && count($is) === 5);
  57. }
  58. /** Start **/
  59. public function testOccurances() {
  60. $this->TextLib = new TextLib('Hochhaus');
  61. $is = $this->TextLib->occurrences();
  62. //pr($is);
  63. $this->assertTrue(!empty($is) && is_array($is) && count($is) === 6);
  64. $is = $this->TextLib->occurrences(null, true);
  65. //pr($is);
  66. $this->assertTrue(!empty($is) && is_array($is) && count($is) === 7);
  67. $is = $this->TextLib->occurrences('h');
  68. //pr($is);
  69. $expected = 3;
  70. $this->assertEquals($is, $expected);
  71. $is = $this->TextLib->occurrences('h', true);
  72. //pr($is);
  73. $expected = 2;
  74. $this->assertEquals($is, $expected);
  75. }
  76. public function testMaxOccurances() {
  77. $is = $this->TextLib->maxOccurrences();
  78. //pr($is);
  79. $this->assertTrue(!empty($is) && is_array($is) && count($is) === 1);
  80. $this->TextLib = new TextLib('Radfahren');
  81. $is = $this->TextLib->maxOccurrences();
  82. //pr($is);
  83. $this->assertTrue(!empty($is) && is_array($is) && count($is) === 2);
  84. }
  85. //TODO: rest of the test functions
  86. /*
  87. //give it the text
  88. $text = new TextParse(
  89. "PHP:Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages... .. .. For this purpose,PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document.
  90. As a general-purpose programming language, PHP code(PHP CODE)is processed by an interpreter application in command-line mode performing desired operating system operations and producing program output on its standard output channel.It may also function as a graphical application...... PHP is available as a processor for most modern web servers and as standalone interpreter on most operating systems and computing platforms."
  91. );
  92. echo "Lenght:". $text->getLenght() ."\n"; //the Lenght
  93. echo "Character:". $text->getCharacter() ."\n"; //Character count
  94. echo "Letter:". $text->getLetter() ."\n"; //Letter count
  95. echo "Space:". $text->getSpace() ."\n"; //Space count
  96. echo "Symbol:". $text->getSymbol() ."\n"; //Symbol count(non letter / space / \n / \r)
  97. echo "Word:". $text->getWord() ."\n"; //Word count
  98. echo "The Words:";
  99. print_r($text->getWord(1)); //the Words
  100. echo "\n";
  101. echo "Sentence:". $text->getSentence() ."\n"; //Sentence count
  102. echo "The Sentences:";
  103. print_r($text->getSentence(1)); //the Sentences
  104. echo "\n";
  105. echo "Paragraph:". $text->getParagraph() ."\n"; //Paragraph count
  106. echo "The Paragraphs:";
  107. print_r($text->getParagraph(1)); //the Paragraphs
  108. echo "\n";
  109. echo "Beautified Text:\n". $text->beautify(80); //beautify the text, wordwrap=80
  110. */
  111. }