PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/classes/Parser/Constant.php

https://github.com/Nycto/phpVocab
PHP | 183 lines | 140 code | 13 blank | 30 comment | 0 complexity | 4823e4c4369512cf9a6899d1555537dc MD5 | raw file
  1. <?php
  2. /**
  3. * @license Artistic License 2.0
  4. *
  5. * This file is part of phpVocab.
  6. *
  7. * phpVocab is free software: you can redistribute it and/or modify
  8. * it under the terms of the Artistic License as published by
  9. * the Open Source Initiative, either version 2.0 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * phpVocab is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * Artistic License for more details.
  16. *
  17. * You should have received a copy of the Artistic License
  18. * along with phpVocab. If not, see <http://www.phpVocab.com/license.php>
  19. * or <http://www.opensource.org/licenses/artistic-license-2.0.php>.
  20. *
  21. * @author James Frasca <James@RoundEights.com>
  22. * @copyright Copyright 2009, James Frasca, All Rights Reserved
  23. */
  24. require_once rtrim( __DIR__, "/" ) ."/../../setup.php";
  25. use \vc\Tokens\Token as Token;
  26. /**
  27. * Unit tests
  28. */
  29. class test_classes_Parser_Constant extends \vc\Test\TestCase
  30. {
  31. /**
  32. * Returns a constant parser
  33. *
  34. * @return \vc\Parser\Constant
  35. */
  36. public function getConstantParser ()
  37. {
  38. return new \vc\Parser\Constant(
  39. new \vc\Parser\Value(
  40. new \vc\Parser\Brackets,
  41. new \vc\Parser\Path
  42. )
  43. );
  44. }
  45. public function testParseConstant ()
  46. {
  47. $access = \vc\Tokens\Access::buildAccess(
  48. $this->oneTokenReader()
  49. ->thenAConst->thenSomeSpace
  50. ->thenAName('CONSTANT')->thenSomeSpace
  51. ->thenAnEquals->thenSomeSpace
  52. ->thenAnInteger(123)->thenASemicolon
  53. ->thenAClass
  54. );
  55. $parser = $this->getConstantParser();
  56. $this->assertEquals(
  57. r8(new \vc\Data\Constant('CONSTANT'))
  58. ->setValue(new \vc\Data\Value('123', 'int')),
  59. $parser->parseConstant( $access )
  60. );
  61. $this->assertHasToken( Token::T_CLASS, $access );
  62. }
  63. public function testParseConstant_FloatValue ()
  64. {
  65. $access = \vc\Tokens\Access::buildAccess(
  66. $this->oneTokenReader()
  67. ->thenAConst->thenSomeSpace
  68. ->thenAName('NAME')->thenSomeSpace
  69. ->thenAnEquals->thenSomeSpace
  70. ->thenAFloat(12.3)->thenASemicolon
  71. ->thenAClass
  72. );
  73. $parser = $this->getConstantParser();
  74. $this->assertEquals(
  75. r8(new \vc\Data\Constant('NAME'))
  76. ->setValue(new \vc\Data\Value('12.3', 'float')),
  77. $parser->parseConstant( $access )
  78. );
  79. $this->assertHasToken( Token::T_CLASS, $access );
  80. }
  81. public function testParseConstant_StringWithSingleQuotes ()
  82. {
  83. $access = \vc\Tokens\Access::buildAccess(
  84. $this->oneTokenReader()
  85. ->thenAConst->thenSomeSpace
  86. ->thenAName('NAME')->thenSomeSpace
  87. ->thenAnEquals->thenSomeSpace
  88. ->thenAString('string', "'")->thenASemicolon
  89. ->thenAClass
  90. );
  91. $parser = $this->getConstantParser();
  92. $this->assertEquals(
  93. r8(new \vc\Data\Constant('NAME'))
  94. ->setValue(new \vc\Data\Value('string', 'string')),
  95. $parser->parseConstant( $access )
  96. );
  97. $this->assertHasToken( Token::T_CLASS, $access );
  98. }
  99. public function testParseConstant_StringWithDoubleQuotes ()
  100. {
  101. $access = \vc\Tokens\Access::buildAccess(
  102. $this->oneTokenReader()
  103. ->thenAConst->thenSomeSpace
  104. ->thenAName('NAME')->thenSomeSpace
  105. ->thenAnEquals->thenSomeSpace
  106. ->thenAString('string', '"')->thenASemicolon
  107. ->thenAClass
  108. );
  109. $parser = $this->getConstantParser();
  110. $this->assertEquals(
  111. r8(new \vc\Data\Constant('NAME'))
  112. ->setValue(new \vc\Data\Value('string', 'string')),
  113. $parser->parseConstant( $access )
  114. );
  115. $this->assertHasToken( Token::T_CLASS, $access );
  116. }
  117. public function testParseConstant_Null ()
  118. {
  119. $access = \vc\Tokens\Access::buildAccess(
  120. $this->oneTokenReader()
  121. ->thenAConst->thenSomeSpace
  122. ->thenAName('NAME')->thenSomeSpace
  123. ->thenAnEquals->thenSomeSpace
  124. ->thenAName('NULL')->thenASemicolon
  125. ->thenAClass
  126. );
  127. $parser = $this->getConstantParser();
  128. $this->assertEquals(
  129. r8(new \vc\Data\Constant('NAME'))
  130. ->setValue(new \vc\Data\Value('null', 'null')),
  131. $parser->parseConstant( $access )
  132. );
  133. $this->assertHasToken( Token::T_CLASS, $access );
  134. }
  135. public function testParseConstant_Heredoc ()
  136. {
  137. $access = \vc\Tokens\Access::buildAccess(
  138. $this->oneTokenReader()
  139. ->thenAConst->thenSomeSpace
  140. ->thenAName('NAME')->thenSomeSpace
  141. ->thenAnEquals->thenSomeSpace
  142. ->thenAHereDoc('contents')->thenASemicolon
  143. ->thenAClass
  144. );
  145. $parser = $this->getConstantParser();
  146. $this->assertEquals(
  147. r8(new \vc\Data\Constant('NAME'))
  148. ->setValue(new \vc\Data\Value('contents', 'string')),
  149. $parser->parseConstant( $access )
  150. );
  151. $this->assertHasToken( Token::T_CLASS, $access );
  152. }
  153. }