DetectionTest.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /* Copyright (c) 2013-2019 Geert Bergman (geert@scrivo.nl), highlight.php
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. * 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
  13. * contributors may be used to endorse or promote products derived from this
  14. * software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. use Highlight\Highlighter;
  29. use Symfony\Component\Finder\Finder;
  30. class DetectionTest extends PHPUnit_Framework_TestCase
  31. {
  32. private $allowedFailures;
  33. public function setUp()
  34. {
  35. // These languages fail auto-detection because their relevance scores are tied with or wrong because of another
  36. // language.
  37. $this->allowedFailures = array(
  38. 'http', // [1. routeros (15%); 2. groovy (15%)]
  39. 'java', // [1. angelscript (22%); 2. scala (22%)]
  40. 'shell', // [1. vhdl (9%); 2. elixir (9%)]
  41. 'plaintext', // [1. asciidoc (10%); 2. properties (4%)]
  42. 'coffeescript', // [1. livescript (26%); 2. coffeescript (26%)]
  43. 'handlebars', // [1. htmlbars (12%); 2. handlebars (12%)]
  44. 'n1ql', // [1. sql (26%); 2. n1ql (26%)]
  45. 'sml', // [1. sml (18%); 2. coq (18%)]
  46. 'purebasic', // [1. reasonml (29%); 2. purebasic (29%)]
  47. 'fortran', // [1. irpf90 (40%); 2. fortran (40%)]
  48. );
  49. }
  50. public static function detectableLanguagesProvider()
  51. {
  52. $testData = array();
  53. $languages = new Finder();
  54. $languages
  55. ->in(__DIR__ . '/detect/')
  56. ->sortByName()
  57. ->files()
  58. ;
  59. foreach ($languages as $language) {
  60. $testData[] = array($language->getRelativePath(), $language->getContents());
  61. }
  62. return $testData;
  63. }
  64. /**
  65. * @dataProvider detectableLanguagesProvider
  66. */
  67. public function testAutomaticDetection($language, $raw)
  68. {
  69. $hl = new Highlighter();
  70. $hl->setAutodetectLanguages($hl->listLanguages());
  71. $actual = $hl->highlightAuto($raw);
  72. $errMessage = sprintf(
  73. "Expected language: %s; [1. %s (%d%%); 2. %s (%d%%)]",
  74. $language,
  75. $actual->language,
  76. $actual->relevance,
  77. $actual->secondBest->language,
  78. $actual->secondBest->relevance
  79. );
  80. if (in_array($language, $this->allowedFailures)) {
  81. $this->markTestSkipped("The '$language' auto-detection test is known to fail: $errMessage");
  82. }
  83. $this->assertEquals($language, $actual->language, $errMessage);
  84. }
  85. }