1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
<?php
namespace SqlParser\Tests;
require 'vendor/autoload.php';
use SqlParser\Lexer;
use SqlParser\Parser;
use SqlParser\Token;
/**
* Implements useful methods for testing.
*
* Each test consists of a string that represents the serialized Lexer or Parser
* instance. Because exceptions include information like file name, which may
* change due to environment's configuration, their information is extracted
* in an array which is serialized.
*
* For example, a parser test consists of an array with two keys, `parser`
* which holds the Parser instance, without errors and the `errors` key which
* holds the array that was previously extracted.
*/
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* Gets test's input and expected output.
*
* @param string $name The name of the test.
*
* @return array
*/
public function getData($name)
{
$input = file_get_contents('tests/data/' . $name . '.in');
$output = unserialize(file_get_contents('tests/data/' . $name . '.out'));
return array($input, $output);
}
/**
* Tests the `Lexer`.
*
* @param string $name The name of the test.
*
* @return Lexer
*/
public function runLexerTest($name)
{
list($input, $output) = $this->getData($name);
$lexer = new Lexer($input);
$errors = array();
foreach ($lexer->errors as $err) {
$errors[] = array($err->getMessage(), $err->ch, $err->pos, $err->getCode());
}
$lexer->errors = array();
$this->assertEquals($output['errors'], $errors);
$this->assertEquals($output['lexer'], $lexer);
return $lexer;
}
/**
* Tests the `Parser`.
*
* @param string $name The name of the test.
*
* @return Parser
*/
public function runParserTest($name)
{
list($input, $output) = $this->getData($name);
$lexer = new Lexer($input);
$parser = new Parser($lexer->tokens);
$errors = array();
foreach ($parser->errors as $err) {
$errors[] = array($err->getMessage(), $err->token, $err->getCode());
}
$parser->errors = array();
$this->assertEquals($output['errors'], $errors);
$this->assertEquals($output['parser'], $parser);
return $parser;
}
}
|