-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
77 lines (58 loc) · 1.4 KB
/
test.php
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
<?php
/**
* Automated test for Closure compiler.
*/
require_once('closure.php');
use Library\Closure\Compiler;
use Library\Closure\Level;
use Library\Closure\RemoteServerError;
use Library\Closure\InvalidResponseError;
$sample = <<<EOF
/**
* Sample header.
*/
/**
* Function declaration.
* @param string name
*/
function hello(name) {
var message = 'Hello, ' + name;
show_alert(message);
// log to console
if (console.log)
console.log(name);
}
hello('New user');
// export function
window['hello'] = hello;
EOF;
$externals = <<<EOF
function show_alert(text){};
EOF;
// create compiler
$compiler = new Compiler();
// compile code and test it
$levels = array(Level::WHITESPACE, Level::SIMPLE, Level::ADVANCED);
$compiler->set_code($sample);
$compiler->set_externals($externals);
foreach ($levels as $level) {
$compiler->set_level($level);
// show compile level
print "> Level: {$level}\n";
try {
$code = $compiler->compile();
} catch (InvalidResponseError $error) {
print "Server returned unfamiliar response.\n";
print $error->get_message();
} catch (RemoteServerError $error) {
print "There was a server side problem.\n";
print $error->get_message();
}
// show compilation outcome
$error_count = count($compiler->get_errors());
$warning_count = count($compiler->get_warnings());
print "\tErrors: {$error_count}\n";
print "\tWarnings: {$warning_count}\n\n";
print $code."\n\n";
}
?>