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

Python code coverage for Lib/test/test_tools/test_fixcid.py

#countcontent
1n/a'''Test Tools/scripts/fixcid.py.'''
2n/a
3n/afrom io import StringIO
4n/aimport os, os.path
5n/aimport runpy
6n/aimport sys
7n/afrom test import support
8n/afrom test.test_tools import skip_if_missing, scriptsdir
9n/aimport unittest
10n/a
11n/askip_if_missing()
12n/a
13n/aclass Test(unittest.TestCase):
14n/a def test_parse_strings(self):
15n/a old1 = 'int xx = "xx\\"xx"[xx];\n'
16n/a old2 = "int xx = 'x\\'xx' + xx;\n"
17n/a output = self.run_script(old1 + old2)
18n/a new1 = 'int yy = "xx\\"xx"[yy];\n'
19n/a new2 = "int yy = 'x\\'xx' + yy;\n"
20n/a self.assertMultiLineEqual(output,
21n/a "1\n"
22n/a "< {old1}"
23n/a "> {new1}"
24n/a "{new1}"
25n/a "2\n"
26n/a "< {old2}"
27n/a "> {new2}"
28n/a "{new2}".format(old1=old1, old2=old2, new1=new1, new2=new2)
29n/a )
30n/a
31n/a def test_alter_comments(self):
32n/a output = self.run_script(
33n/a substfile=
34n/a "xx yy\n"
35n/a "*aa bb\n",
36n/a args=("-c", "-",),
37n/a input=
38n/a "/* xx altered */\n"
39n/a "int xx;\n"
40n/a "/* aa unaltered */\n"
41n/a "int aa;\n",
42n/a )
43n/a self.assertMultiLineEqual(output,
44n/a "1\n"
45n/a "< /* xx altered */\n"
46n/a "> /* yy altered */\n"
47n/a "/* yy altered */\n"
48n/a "2\n"
49n/a "< int xx;\n"
50n/a "> int yy;\n"
51n/a "int yy;\n"
52n/a "/* aa unaltered */\n"
53n/a "4\n"
54n/a "< int aa;\n"
55n/a "> int bb;\n"
56n/a "int bb;\n"
57n/a )
58n/a
59n/a def test_directory(self):
60n/a os.mkdir(support.TESTFN)
61n/a self.addCleanup(support.rmtree, support.TESTFN)
62n/a c_filename = os.path.join(support.TESTFN, "file.c")
63n/a with open(c_filename, "w") as file:
64n/a file.write("int xx;\n")
65n/a with open(os.path.join(support.TESTFN, "file.py"), "w") as file:
66n/a file.write("xx = 'unaltered'\n")
67n/a script = os.path.join(scriptsdir, "fixcid.py")
68n/a output = self.run_script(args=(support.TESTFN,))
69n/a self.assertMultiLineEqual(output,
70n/a "{}:\n"
71n/a "1\n"
72n/a '< int xx;\n'
73n/a '> int yy;\n'.format(c_filename)
74n/a )
75n/a
76n/a def run_script(self, input="", *, args=("-",), substfile="xx yy\n"):
77n/a substfilename = support.TESTFN + ".subst"
78n/a with open(substfilename, "w") as file:
79n/a file.write(substfile)
80n/a self.addCleanup(support.unlink, substfilename)
81n/a
82n/a argv = ["fixcid.py", "-s", substfilename] + list(args)
83n/a script = os.path.join(scriptsdir, "fixcid.py")
84n/a with support.swap_attr(sys, "argv", argv), \
85n/a support.swap_attr(sys, "stdin", StringIO(input)), \
86n/a support.captured_stdout() as output, \
87n/a support.captured_stderr():
88n/a try:
89n/a runpy.run_path(script, run_name="__main__")
90n/a except SystemExit as exit:
91n/a self.assertEqual(exit.code, 0)
92n/a return output.getvalue()