Add tests for other scalar types

This commit is contained in:
nikic 2011-11-26 17:10:18 +01:00
parent 933bebb9b8
commit 03a0449b1a
6 changed files with 313 additions and 62 deletions

View File

@ -5,12 +5,16 @@ class PHPParser_Tests_codeTest extends PHPUnit_Framework_TestCase
/**
* @dataProvider provideTestCode
*/
public function testCode($name, $code, $xml) {
$parser = new PHPParser_Parser;
$serializer = new PHPParser_Serializer_XML;
public function testCode($name, $code, $dump) {
$parser = new PHPParser_Parser;
$dumper = new PHPParser_NodeDumper;
$stmts = $parser->parse(new PHPParser_Lexer($code));
$this->assertEquals($xml, trim($serializer->serialize($stmts)), $name);
$this->assertEquals(
$this->canonicalize($dump),
$this->canonicalize($dumper->dump($stmts)),
$name
);
}
public function provideTestCode() {
@ -23,9 +27,6 @@ class PHPParser_Tests_codeTest extends PHPUnit_Framework_TestCase
foreach ($it as $file) {
$fileContents = file_get_contents($file);
// normalize EOL to Unix
$fileContents = str_replace(array("\r\n", "\r"), "\n", $fileContents);
// evaluate @@{expr}@@ expressions
$fileContents = preg_replace('/@@\{(.*?)\}@@/e', '$1', $fileContents);
@ -34,4 +35,15 @@ class PHPParser_Tests_codeTest extends PHPUnit_Framework_TestCase
return $tests;
}
protected function canonicalize($str) {
// trim from both sides
$str = trim($str);
// normalize EOL to \n
$str = str_replace(array("\r\n", "\r"), "\n", $str);
// trim right side of all lines
return implode("\n", array_map('rtrim', explode("\n", $str)));
}
}

View File

@ -0,0 +1,52 @@
Constant string syntaxes
-----
<?php
'';
"";
b'';
b"";
'Hi';
b'Hi';
"Hi";
b"Hi";
'!\'!\\!\a!';
"!\"!\\!\$!\n!\r!\t!\f!\v!\a";
"!\xFF!\377!\400!\0!";
-----
array(
0: Scalar_String(
value:
)
1: Scalar_String(
value:
)
2: Scalar_String(
value:
)
3: Scalar_String(
value:
)
4: Scalar_String(
value: Hi
)
5: Scalar_String(
value: Hi
)
6: Scalar_String(
value: Hi
)
7: Scalar_String(
value: Hi
)
8: Scalar_String(
value: !'!\!\a!
)
9: Scalar_String(
value: !"!\!$!
!
! ! ! !\a
)
10: Scalar_String(
value: !@@{ chr(255) }@@!@@{ chr(255) }@@!@@{ chr(0) }@@!@@{ chr(0) }@@!
)
)

View File

@ -0,0 +1,134 @@
Encapsed strings
-----
<?php
"$A";
"$A->B";
"$A[B]";
"$A[0]";
"$A[0x0]";
"{$A}";
"{$A['B']}";
"${A}";
"${A['B']}";
"${$A}";
"A $B C";
b"$A";
-----
array(
0: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: A
)
)
)
1: Scalar_Encapsed(
parts: array(
0: Expr_PropertyFetch(
var: Expr_Variable(
name: A
)
name: B
)
)
)
2: Scalar_Encapsed(
parts: array(
0: Expr_ArrayDimFetch(
var: Expr_Variable(
name: A
)
dim: Scalar_String(
value: B
)
)
)
)
3: Scalar_Encapsed(
parts: array(
0: Expr_ArrayDimFetch(
var: Expr_Variable(
name: A
)
dim: Scalar_String(
value: 0
)
)
)
)
4: Scalar_Encapsed(
parts: array(
0: Expr_ArrayDimFetch(
var: Expr_Variable(
name: A
)
dim: Scalar_String(
value: 0x0
)
)
)
)
5: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: A
)
)
)
6: Scalar_Encapsed(
parts: array(
0: Expr_ArrayDimFetch(
var: Expr_Variable(
name: A
)
dim: Scalar_String(
value: B
)
)
)
)
7: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: A
)
)
)
8: Scalar_Encapsed(
parts: array(
0: Expr_ArrayDimFetch(
var: Expr_Variable(
name: A
)
dim: Scalar_String(
value: B
)
)
)
)
9: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: Expr_Variable(
name: A
)
)
)
)
10: Scalar_Encapsed(
parts: array(
0: A
1: Expr_Variable(
name: B
)
2: C
)
)
11: Scalar_Encapsed(
parts: array(
0: Expr_Variable(
name: A
)
)
)
)

View File

@ -0,0 +1,46 @@
Different float syntaxes
-----
<?php
0.0;
0.;
.0;
0e0;
0E0;
0e+0;
0e-0;
30.20e10;
300.200e100;
1e10000;
-----
array(
0: Scalar_DNumber(
value: 0
)
1: Scalar_DNumber(
value: 0
)
2: Scalar_DNumber(
value: 0
)
3: Scalar_DNumber(
value: 0
)
4: Scalar_DNumber(
value: 0
)
5: Scalar_DNumber(
value: 0
)
6: Scalar_DNumber(
value: 0
)
7: Scalar_DNumber(
value: 302000000000
)
8: Scalar_DNumber(
value: 3.002E+102
)
9: Scalar_DNumber(
value: INF
)
)

View File

@ -12,58 +12,35 @@ Different integer syntaxes
0787;
0b111000111000;
-----
<?xml version="1.0" encoding="UTF-8"?>
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
<scalar:array>
<node:Scalar_LNumber line="2">
<subNode:value>
<scalar:int>0</scalar:int>
</subNode:value>
</node:Scalar_LNumber>
<node:Scalar_LNumber line="3">
<subNode:value>
<scalar:int>1</scalar:int>
</subNode:value>
</node:Scalar_LNumber>
<node:Scalar_LNumber line="4">
<subNode:value>
<scalar:int>2147483647</scalar:int>
</subNode:value>
</node:Scalar_LNumber>
<node:Scalar_DNumber line="5">
<subNode:value>
<scalar:float>2147483648</scalar:float>
</subNode:value>
</node:Scalar_DNumber>
<node:Scalar_LNumber line="6">
<subNode:value>
<scalar:int>4095</scalar:int>
</subNode:value>
</node:Scalar_LNumber>
<node:Scalar_LNumber line="7">
<subNode:value>
<scalar:int>4095</scalar:int>
</subNode:value>
</node:Scalar_LNumber>
<node:Scalar_LNumber line="8">
<subNode:value>
<scalar:int>4095</scalar:int>
</subNode:value>
</node:Scalar_LNumber>
<node:Scalar_LNumber line="9">
<subNode:value>
<scalar:int>511</scalar:int>
</subNode:value>
</node:Scalar_LNumber>
<node:Scalar_LNumber line="10">
<subNode:value>
<scalar:int>7</scalar:int>
</subNode:value>
</node:Scalar_LNumber>
<node:Scalar_LNumber line="11">
<subNode:value>
<scalar:int>3640</scalar:int>
</subNode:value>
</node:Scalar_LNumber>
</scalar:array>
</AST>
array(
0: Scalar_LNumber(
value: 0
)
1: Scalar_LNumber(
value: 1
)
2: Scalar_LNumber(
value: 2147483647
)
3: Scalar_DNumber(
value: 2147483648
)
4: Scalar_LNumber(
value: 4095
)
5: Scalar_LNumber(
value: 4095
)
6: Scalar_LNumber(
value: 4095
)
7: Scalar_LNumber(
value: 511
)
8: Scalar_LNumber(
value: 7
)
9: Scalar_LNumber(
value: 3640
)
)

View File

@ -0,0 +1,30 @@
Magic constants
-----
<?php
__CLASS__;
__DIR__;
__FILE__;
__FUNCTION__;
__LINE__;
__METHOD__;
__NAMESPACE__;
__TRAIT__;
-----
array(
0: Scalar_ClassConst(
)
1: Scalar_DirConst(
)
2: Scalar_FileConst(
)
3: Scalar_FuncConst(
)
4: Scalar_LineConst(
)
5: Scalar_MethodConst(
)
6: Scalar_NSConst(
)
7: Scalar_TraitConst(
)
)