document_rst_parser_test.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * ezcDocumentRstParserTests
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Document
  23. * @version //autogen//
  24. * @subpackage Tests
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Test suite for class.
  29. *
  30. * @package Document
  31. * @subpackage Tests
  32. */
  33. class ezcDocumentRstParserTests extends ezcTestCase
  34. {
  35. protected static $testDocuments = null;
  36. public static function suite()
  37. {
  38. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  39. }
  40. public function testParserOptionsXhtmlVisitor()
  41. {
  42. $options = new ezcDocumentRstOptions();
  43. $options->xhtmlVisitor = 'foo';
  44. try
  45. {
  46. $options->errorReporting = 0;
  47. $this->fail( 'Expected ezcBaseValueException.' );
  48. }
  49. catch ( ezcBaseValueException $e )
  50. { /* Expected */ }
  51. }
  52. public function testParserOptionsXhtmlVisitorOptions()
  53. {
  54. $options = new ezcDocumentRstOptions();
  55. $options->xhtmlVisitorOptions = new ezcDocumentHtmlConverterOptions();
  56. try
  57. {
  58. $options->errorReporting = 0;
  59. $this->fail( 'Expected ezcBaseValueException.' );
  60. }
  61. catch ( ezcBaseValueException $e )
  62. { /* Expected */ }
  63. }
  64. public function testParserOptionsUnknownOption()
  65. {
  66. $options = new ezcDocumentRstOptions();
  67. try
  68. {
  69. $options->notExistingOption = 0;
  70. $this->fail( 'Expected ezcBasePropertyNotFoundException.' );
  71. }
  72. catch ( ezcBasePropertyNotFoundException $e )
  73. { /* Expected */ }
  74. }
  75. public static function getTestDocuments()
  76. {
  77. if ( self::$testDocuments === null )
  78. {
  79. // Get a list of all test files from the respektive folder
  80. $testFiles = glob( dirname( __FILE__ ) . '/files/rst/parser/s_*.txt' );
  81. // Create array with the test file and the expected result file
  82. foreach ( $testFiles as $file )
  83. {
  84. self::$testDocuments[] = array(
  85. $file,
  86. substr( $file, 0, -3 ) . 'rst'
  87. );
  88. }
  89. }
  90. return self::$testDocuments;
  91. return array_slice( self::$testDocuments, -1, 1 );
  92. }
  93. /**
  94. * @dataProvider getTestDocuments
  95. */
  96. public function testParseRstFile( $from, $to )
  97. {
  98. if ( !is_file( $to ) )
  99. {
  100. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  101. }
  102. $tokenizer = new ezcDocumentRstTokenizer();
  103. $parser = new ezcDocumentRstParser();
  104. $document = $parser->parse( $tokenizer->tokenizeFile( $from ) );
  105. $this->assertTrue(
  106. $document instanceof ezcDocumentRstDocumentNode
  107. );
  108. $expected = include $to;
  109. // Store test file, to have something to compare on failure
  110. $tempDir = $this->createTempDir( 'rst_parser_' ) . '/';
  111. file_put_contents( $tempDir . basename( $to ), "<?php\n\nreturn " . var_export( $document, true ) . ";\n\n" );
  112. $this->assertEquals(
  113. $expected,
  114. $document,
  115. 'Parsed document does not match expected document.',
  116. 0, 20
  117. );
  118. // Remove tempdir, when nothing failed.
  119. $this->removeTempDir();
  120. }
  121. public static function getErroneousTestDocuments()
  122. {
  123. // return array();
  124. return array(
  125. array(
  126. dirname( __FILE__ ) . '/files/rst/parser/e_001_non_aligned_text.txt',
  127. 'Parse error: Fatal error: \'Unexpected indentation change from level 4 to 0.\' in line 4 at position 38.',
  128. ),
  129. array(
  130. dirname( __FILE__ ) . '/files/rst/parser/e_002_titles_mismatch.txt',
  131. 'Parse error: Notice: \'Title underline length (12) is shorter then text length (13).\' in line 3 at position 1.',
  132. ),
  133. array(
  134. dirname( __FILE__ ) . '/files/rst/parser/e_003_titles_depth.txt',
  135. 'Parse error: Fatal error: \'Title depth inconsitency.\' in line 13 at position 1.',
  136. ),
  137. );
  138. }
  139. /**
  140. * @dataProvider getErroneousTestDocuments
  141. */
  142. public function testParseErroneousRstFile( $file, $message )
  143. {
  144. $tokenizer = new ezcDocumentRstTokenizer();
  145. $parser = new ezcDocumentRstParser();
  146. try
  147. {
  148. $document = $parser->parse( $tokenizer->tokenizeFile( $file ) );
  149. $this->fail( 'Expected ezcDocumentRstParserException.' );
  150. }
  151. catch ( ezcDocumentParserException $e )
  152. {
  153. $this->assertSame(
  154. $message,
  155. $e->getMessage(),
  156. 'Different parse error expected.'
  157. );
  158. }
  159. }
  160. }
  161. ?>