ยปCore Development>Code coverage>Lib/test/test_codeop.py

Python code coverage for Lib/test/test_codeop.py

#countcontent
1n/a"""
2n/a Test cases for codeop.py
3n/a Nick Mathewson
4n/a"""
5n/aimport unittest
6n/afrom test.support import is_jython
7n/a
8n/afrom codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
9n/aimport io
10n/a
11n/aif is_jython:
12n/a import sys
13n/a
14n/a def unify_callables(d):
15n/a for n,v in d.items():
16n/a if hasattr(v, '__call__'):
17n/a d[n] = True
18n/a return d
19n/a
20n/aclass CodeopTests(unittest.TestCase):
21n/a
22n/a def assertValid(self, str, symbol='single'):
23n/a '''succeed iff str is a valid piece of code'''
24n/a if is_jython:
25n/a code = compile_command(str, "<input>", symbol)
26n/a self.assertTrue(code)
27n/a if symbol == "single":
28n/a d,r = {},{}
29n/a saved_stdout = sys.stdout
30n/a sys.stdout = io.StringIO()
31n/a try:
32n/a exec(code, d)
33n/a exec(compile(str,"<input>","single"), r)
34n/a finally:
35n/a sys.stdout = saved_stdout
36n/a elif symbol == 'eval':
37n/a ctx = {'a': 2}
38n/a d = { 'value': eval(code,ctx) }
39n/a r = { 'value': eval(str,ctx) }
40n/a self.assertEqual(unify_callables(r),unify_callables(d))
41n/a else:
42n/a expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
43n/a self.assertEqual(compile_command(str, "<input>", symbol), expected)
44n/a
45n/a def assertIncomplete(self, str, symbol='single'):
46n/a '''succeed iff str is the start of a valid piece of code'''
47n/a self.assertEqual(compile_command(str, symbol=symbol), None)
48n/a
49n/a def assertInvalid(self, str, symbol='single', is_syntax=1):
50n/a '''succeed iff str is the start of an invalid piece of code'''
51n/a try:
52n/a compile_command(str,symbol=symbol)
53n/a self.fail("No exception raised for invalid code")
54n/a except SyntaxError:
55n/a self.assertTrue(is_syntax)
56n/a except OverflowError:
57n/a self.assertTrue(not is_syntax)
58n/a
59n/a def test_valid(self):
60n/a av = self.assertValid
61n/a
62n/a # special case
63n/a if not is_jython:
64n/a self.assertEqual(compile_command(""),
65n/a compile("pass", "<input>", 'single',
66n/a PyCF_DONT_IMPLY_DEDENT))
67n/a self.assertEqual(compile_command("\n"),
68n/a compile("pass", "<input>", 'single',
69n/a PyCF_DONT_IMPLY_DEDENT))
70n/a else:
71n/a av("")
72n/a av("\n")
73n/a
74n/a av("a = 1")
75n/a av("\na = 1")
76n/a av("a = 1\n")
77n/a av("a = 1\n\n")
78n/a av("\n\na = 1\n\n")
79n/a
80n/a av("def x():\n pass\n")
81n/a av("if 1:\n pass\n")
82n/a
83n/a av("\n\nif 1: pass\n")
84n/a av("\n\nif 1: pass\n\n")
85n/a
86n/a av("def x():\n\n pass\n")
87n/a av("def x():\n pass\n \n")
88n/a av("def x():\n pass\n \n")
89n/a
90n/a av("pass\n")
91n/a av("3**3\n")
92n/a
93n/a av("if 9==3:\n pass\nelse:\n pass\n")
94n/a av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
95n/a
96n/a av("#a\n#b\na = 3\n")
97n/a av("#a\n\n \na=3\n")
98n/a av("a=3\n\n")
99n/a av("a = 9+ \\\n3")
100n/a
101n/a av("3**3","eval")
102n/a av("(lambda z: \n z**3)","eval")
103n/a
104n/a av("9+ \\\n3","eval")
105n/a av("9+ \\\n3\n","eval")
106n/a
107n/a av("\n\na**3","eval")
108n/a av("\n \na**3","eval")
109n/a av("#a\n#b\na**3","eval")
110n/a
111n/a av("\n\na = 1\n\n")
112n/a av("\n\nif 1: a=1\n\n")
113n/a
114n/a av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
115n/a av("#a\n\n \na=3\n\n")
116n/a
117n/a av("\n\na**3","eval")
118n/a av("\n \na**3","eval")
119n/a av("#a\n#b\na**3","eval")
120n/a
121n/a av("def f():\n try: pass\n finally: [x for x in (1,2)]\n")
122n/a av("def f():\n pass\n#foo\n")
123n/a av("@a.b.c\ndef f():\n pass\n")
124n/a
125n/a def test_incomplete(self):
126n/a ai = self.assertIncomplete
127n/a
128n/a ai("(a **")
129n/a ai("(a,b,")
130n/a ai("(a,b,(")
131n/a ai("(a,b,(")
132n/a ai("a = (")
133n/a ai("a = {")
134n/a ai("b + {")
135n/a
136n/a ai("if 9==3:\n pass\nelse:")
137n/a ai("if 9==3:\n pass\nelse:\n")
138n/a ai("if 9==3:\n pass\nelse:\n pass")
139n/a ai("if 1:")
140n/a ai("if 1:\n")
141n/a ai("if 1:\n pass\n if 1:\n pass\n else:")
142n/a ai("if 1:\n pass\n if 1:\n pass\n else:\n")
143n/a ai("if 1:\n pass\n if 1:\n pass\n else:\n pass")
144n/a
145n/a ai("def x():")
146n/a ai("def x():\n")
147n/a ai("def x():\n\n")
148n/a
149n/a ai("def x():\n pass")
150n/a ai("def x():\n pass\n ")
151n/a ai("def x():\n pass\n ")
152n/a ai("\n\ndef x():\n pass")
153n/a
154n/a ai("a = 9+ \\")
155n/a ai("a = 'a\\")
156n/a ai("a = '''xy")
157n/a
158n/a ai("","eval")
159n/a ai("\n","eval")
160n/a ai("(","eval")
161n/a ai("(\n\n\n","eval")
162n/a ai("(9+","eval")
163n/a ai("9+ \\","eval")
164n/a ai("lambda z: \\","eval")
165n/a
166n/a ai("if True:\n if True:\n if True: \n")
167n/a
168n/a ai("@a(")
169n/a ai("@a(b")
170n/a ai("@a(b,")
171n/a ai("@a(b,c")
172n/a ai("@a(b,c,")
173n/a
174n/a ai("from a import (")
175n/a ai("from a import (b")
176n/a ai("from a import (b,")
177n/a ai("from a import (b,c")
178n/a ai("from a import (b,c,")
179n/a
180n/a ai("[");
181n/a ai("[a");
182n/a ai("[a,");
183n/a ai("[a,b");
184n/a ai("[a,b,");
185n/a
186n/a ai("{");
187n/a ai("{a");
188n/a ai("{a:");
189n/a ai("{a:b");
190n/a ai("{a:b,");
191n/a ai("{a:b,c");
192n/a ai("{a:b,c:");
193n/a ai("{a:b,c:d");
194n/a ai("{a:b,c:d,");
195n/a
196n/a ai("a(")
197n/a ai("a(b")
198n/a ai("a(b,")
199n/a ai("a(b,c")
200n/a ai("a(b,c,")
201n/a
202n/a ai("a[")
203n/a ai("a[b")
204n/a ai("a[b,")
205n/a ai("a[b:")
206n/a ai("a[b:c")
207n/a ai("a[b:c:")
208n/a ai("a[b:c:d")
209n/a
210n/a ai("def a(")
211n/a ai("def a(b")
212n/a ai("def a(b,")
213n/a ai("def a(b,c")
214n/a ai("def a(b,c,")
215n/a
216n/a ai("(")
217n/a ai("(a")
218n/a ai("(a,")
219n/a ai("(a,b")
220n/a ai("(a,b,")
221n/a
222n/a ai("if a:\n pass\nelif b:")
223n/a ai("if a:\n pass\nelif b:\n pass\nelse:")
224n/a
225n/a ai("while a:")
226n/a ai("while a:\n pass\nelse:")
227n/a
228n/a ai("for a in b:")
229n/a ai("for a in b:\n pass\nelse:")
230n/a
231n/a ai("try:")
232n/a ai("try:\n pass\nexcept:")
233n/a ai("try:\n pass\nfinally:")
234n/a ai("try:\n pass\nexcept:\n pass\nfinally:")
235n/a
236n/a ai("with a:")
237n/a ai("with a as b:")
238n/a
239n/a ai("class a:")
240n/a ai("class a(")
241n/a ai("class a(b")
242n/a ai("class a(b,")
243n/a ai("class a():")
244n/a
245n/a ai("[x for")
246n/a ai("[x for x in")
247n/a ai("[x for x in (")
248n/a
249n/a ai("(x for")
250n/a ai("(x for x in")
251n/a ai("(x for x in (")
252n/a
253n/a def test_invalid(self):
254n/a ai = self.assertInvalid
255n/a ai("a b")
256n/a
257n/a ai("a @")
258n/a ai("a b @")
259n/a ai("a ** @")
260n/a
261n/a ai("a = ")
262n/a ai("a = 9 +")
263n/a
264n/a ai("def x():\n\npass\n")
265n/a
266n/a ai("\n\n if 1: pass\n\npass")
267n/a
268n/a ai("a = 9+ \\\n")
269n/a ai("a = 'a\\ ")
270n/a ai("a = 'a\\\n")
271n/a
272n/a ai("a = 1","eval")
273n/a ai("a = (","eval")
274n/a ai("]","eval")
275n/a ai("())","eval")
276n/a ai("[}","eval")
277n/a ai("9+","eval")
278n/a ai("lambda z:","eval")
279n/a ai("a b","eval")
280n/a
281n/a ai("return 2.3")
282n/a ai("if (a == 1 and b = 2): pass")
283n/a
284n/a ai("del 1")
285n/a ai("del (1,)")
286n/a ai("del [1]")
287n/a ai("del '1'")
288n/a
289n/a ai("[i for i in range(10)] = (1, 2, 3)")
290n/a
291n/a def test_filename(self):
292n/a self.assertEqual(compile_command("a = 1\n", "abc").co_filename,
293n/a compile("a = 1\n", "abc", 'single').co_filename)
294n/a self.assertNotEqual(compile_command("a = 1\n", "abc").co_filename,
295n/a compile("a = 1\n", "def", 'single').co_filename)
296n/a
297n/a
298n/aif __name__ == "__main__":
299n/a unittest.main()