-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_php.py
95 lines (79 loc) · 2.29 KB
/
test_php.py
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
91
92
93
94
95
#!/usr/bin/env python
from __future__ import with_statement
from php import PHP, PhpError
from time import time
import unittest
class TestPHP(unittest.TestCase):
TEST_STRING = 'foobar"\\\'\n'
def test_echo(self):
php = PHP()
php.echo(self.TEST_STRING)
self.assertEquals(str(php), self.TEST_STRING)
def test_error(self):
php = PHP()
php.nonexistant_method()
with self.assertRaises(PhpError):
str(php)
def test_new(self):
php = PHP()
dt = php.new('DateTime')
ts = dt.getTimestamp()
php.echo(ts)
output = str(php)
self.assertAlmostEquals(int(output), time(), delta=10)
def test_null(self):
php = PHP()
php.var_dump(None)
self.assertEquals(str(php), 'NULL\n')
def test_number(self):
php = PHP()
php.var_dump(5)
self.assertEquals(str(php), 'int(5)\n')
def test_compose(self):
php = PHP()
php.echo(php.strlen(self.TEST_STRING))
output = str(php)
self.assertEquals(output, str(len(self.TEST_STRING)))
def test_deep_compose(self):
php = PHP()
php.echo(php.strlen(php.strrev(self.TEST_STRING)))
output = str(php)
self.assertEquals(output, str(len(self.TEST_STRING)))
def test_repr(self):
php = PHP()
self.assertEquals(repr(php), '<PHP object with 0 statement(s)>')
TEST_RANGE = 6
def test_list(self):
php = PHP()
php.echo(php.count(range(self.TEST_RANGE)))
output = str(php)
self.assertEquals(output, str(self.TEST_RANGE))
TEST_DICT = {'foo': 'bar', 2: 'qux'}
def test_dict(self):
php = PHP()
values = php.array_values(self.TEST_DICT)
php.sort(values)
php.echo(php.implode(',', values))
expected = ','.join(sorted(self.TEST_DICT.itervalues()))
self.assertEquals(str(php), expected)
def test_getitem(self):
php = PHP()
array_params = range(self.TEST_RANGE)
array = php.array(*array_params)
php.echo(array[0])
self.assertEquals(str(php), str(array_params[0]))
def test_dyn_getitem(self):
php = PHP()
array_params = range(self.TEST_RANGE)
array = php.array(*array_params)
php.shuffle(array)
php.echo(array[array[0]])
self.assertIn(int(str(php)), array_params)
TEST_VALUE = 42
def test_add(self):
php = PHP()
array = php.range(1, self.TEST_RANGE)
php.echo(php.count(array) + self.TEST_VALUE)
self.assertEquals(str(php), str(self.TEST_VALUE + self.TEST_RANGE))
if __name__ == '__main__':
unittest.main()