Mercurial > genshi > mirror
annotate markup/tests/eval.py @ 31:2ab5fa60575d trunk
* More test cases for expression evaluation.
* Fixes for the new AST-based Python 2.5 evaluation.
author | cmlenz |
---|---|
date | Wed, 28 Jun 2006 18:39:05 +0000 |
parents | bcdbb7e5e4e5 |
children | 2224a52256ca |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2006 Christopher Lenz | |
4 # All rights reserved. | |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
27 | 8 # are also available at http://markup.cmlenz.net/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
27 | 12 # history and logs, available at hhttp://markup.cmlenz.net/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 | |
27 | 17 from markup.eval import Expression |
1 | 18 |
30
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
19 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
20 class ExpressionTestCase(unittest.TestCase): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
21 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
22 def test_str_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
23 self.assertEqual('foo', Expression('"foo"').evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
24 self.assertEqual('foo', Expression('"""foo"""').evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
25 self.assertEqual('foo', Expression("'foo'").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
26 self.assertEqual('foo', Expression("'''foo'''").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
27 self.assertEqual('foo', Expression("u'foo'").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
28 self.assertEqual('foo', Expression("r'foo'").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
29 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
30 def test_num_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
31 self.assertEqual(42, Expression("42").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
32 self.assertEqual(42L, Expression("42L").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
33 self.assertEqual(.42, Expression(".42").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
34 self.assertEqual(07, Expression("07").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
35 self.assertEqual(0xF2, Expression("0xF2").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
36 self.assertEqual(0XF2, Expression("0XF2").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
37 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
38 def test_dict_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
39 self.assertEqual({}, Expression("{}").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
40 self.assertEqual({'key': True}, |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
41 Expression("{'key': value}").evaluate({'value': True})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
42 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
43 def test_list_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
44 self.assertEqual([], Expression("[]").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
45 self.assertEqual([1, 2, 3], Expression("[1, 2, 3]").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
46 self.assertEqual([True], |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
47 Expression("[value]").evaluate({'value': True})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
48 |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
49 def test_tuple_literal(self): |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
50 self.assertEqual((), Expression("()").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
51 self.assertEqual((1, 2, 3), Expression("(1, 2, 3)").evaluate({})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
52 self.assertEqual((True,), |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
53 Expression("(value,)").evaluate({'value': True})) |
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
54 |
31 | 55 def test_unaryop_pos(self): |
56 self.assertEqual(1, Expression("+1").evaluate({})) | |
57 self.assertEqual(1, Expression("+x").evaluate({'x': 1})) | |
58 | |
59 def test_unaryop_neg(self): | |
60 self.assertEqual(-1, Expression("-1").evaluate({})) | |
61 self.assertEqual(-1, Expression("-x").evaluate({'x': 1})) | |
62 | |
63 def test_unaryop_not(self): | |
64 self.assertEqual(False, Expression("not True").evaluate({})) | |
65 self.assertEqual(False, Expression("not x").evaluate({'x': True})) | |
66 | |
67 def test_binop_add(self): | |
68 self.assertEqual(3, Expression("2 + 1").evaluate({})) | |
69 self.assertEqual(3, Expression("x + y").evaluate({'x': 2, 'y': 1})) | |
70 | |
71 def test_binop_sub(self): | |
72 self.assertEqual(1, Expression("2 - 1").evaluate({})) | |
73 self.assertEqual(1, Expression("x - y").evaluate({'x': 1, 'y': 1})) | |
74 | |
75 def test_binop_sub(self): | |
76 self.assertEqual(1, Expression("2 - 1").evaluate({})) | |
77 self.assertEqual(1, Expression("x - y").evaluate({'x': 2, 'y': 1})) | |
78 | |
79 def test_binop_mul(self): | |
80 self.assertEqual(4, Expression("2 * 2").evaluate({})) | |
81 self.assertEqual(4, Expression("x * y").evaluate({'x': 2, 'y': 2})) | |
82 | |
83 def test_binop_pow(self): | |
84 self.assertEqual(4, Expression("2 ** 2").evaluate({})) | |
85 self.assertEqual(4, Expression("x ** y").evaluate({'x': 2, 'y': 2})) | |
86 | |
87 def test_binop_div(self): | |
88 self.assertEqual(2, Expression("4 / 2").evaluate({})) | |
89 self.assertEqual(2, Expression("x / y").evaluate({'x': 4, 'y': 2})) | |
90 | |
91 def test_binop_floordiv(self): | |
92 self.assertEqual(1, Expression("3 // 2").evaluate({})) | |
93 self.assertEqual(1, Expression("x // y").evaluate({'x': 3, 'y': 2})) | |
94 | |
95 def test_binop_mod(self): | |
96 self.assertEqual(1, Expression("3 % 2").evaluate({})) | |
97 self.assertEqual(1, Expression("x % y").evaluate({'x': 3, 'y': 2})) | |
98 | |
99 def test_binop_contains(self): | |
100 self.assertEqual(True, Expression("1 in (1, 2, 3)").evaluate({})) | |
101 self.assertEqual(True, Expression("x in y").evaluate({'x': 1, | |
102 'y': (1, 2, 3)})) | |
103 | |
104 def test_binop_not_contains(self): | |
105 self.assertEqual(True, Expression("4 not in (1, 2, 3)").evaluate({})) | |
106 self.assertEqual(True, Expression("x not in y").evaluate({'x': 4, | |
107 'y': (1, 2, 3)})) | |
108 | |
109 def test_boolop_and(self): | |
110 self.assertEqual(False, Expression("True and False").evaluate({})) | |
111 self.assertEqual(False, Expression("x and y").evaluate({'x': True, | |
112 'y': False})) | |
113 | |
114 def test_boolop_or(self): | |
115 self.assertEqual(True, Expression("True or False").evaluate({})) | |
116 self.assertEqual(True, Expression("x or y").evaluate({'x': True, | |
117 'y': False})) | |
118 | |
119 def test_compare_eq(self): | |
120 self.assertEqual(True, Expression("1 == 1").evaluate({})) | |
121 self.assertEqual(True, Expression("x == y").evaluate({'x': 1, 'y': 1})) | |
122 | |
123 def test_compare_ne(self): | |
124 self.assertEqual(False, Expression("1 != 1").evaluate({})) | |
125 self.assertEqual(False, Expression("x != y").evaluate({'x': 1, 'y': 1})) | |
126 | |
127 def test_compare_lt(self): | |
128 self.assertEqual(True, Expression("1 < 2").evaluate({})) | |
129 self.assertEqual(True, Expression("x < y").evaluate({'x': 1, 'y': 2})) | |
130 | |
131 def test_compare_le(self): | |
132 self.assertEqual(True, Expression("1 <= 1").evaluate({})) | |
133 self.assertEqual(True, Expression("x <= y").evaluate({'x': 1, 'y': 1})) | |
134 | |
135 def test_compare_gt(self): | |
136 self.assertEqual(True, Expression("2 > 1").evaluate({})) | |
137 self.assertEqual(True, Expression("x > y").evaluate({'x': 2, 'y': 1})) | |
138 | |
139 def test_compare_ge(self): | |
140 self.assertEqual(True, Expression("1 >= 1").evaluate({})) | |
141 self.assertEqual(True, Expression("x >= y").evaluate({'x': 1, 'y': 1})) | |
142 | |
143 def test_compare_multi(self): | |
144 self.assertEqual(True, Expression("1 != 3 == 3").evaluate({})) | |
145 self.assertEqual(True, Expression("x != y == y").evaluate({'x': 3, | |
146 'y': 3})) | |
147 | |
30
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
148 |
1 | 149 def suite(): |
150 suite = unittest.TestSuite() | |
30
bcdbb7e5e4e5
Experimental support for using the new native AST in Python 2.5 instead of the `compiler` package.
cmlenz
parents:
27
diff
changeset
|
151 suite.addTest(unittest.makeSuite(ExpressionTestCase, 'test')) |
27 | 152 suite.addTest(doctest.DocTestSuite(Expression.__module__)) |
1 | 153 return suite |
154 | |
155 if __name__ == '__main__': | |
156 unittest.main(defaultTest='suite') |