ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_iomenu.py

Python code coverage for Lib/idlelib/idle_test/test_iomenu.py

#countcontent
1n/aimport unittest
2n/aimport io
3n/a
4n/afrom idlelib.run import PseudoInputFile, PseudoOutputFile
5n/a
6n/a
7n/aclass S(str):
8n/a def __str__(self):
9n/a return '%s:str' % type(self).__name__
10n/a def __unicode__(self):
11n/a return '%s:unicode' % type(self).__name__
12n/a def __len__(self):
13n/a return 3
14n/a def __iter__(self):
15n/a return iter('abc')
16n/a def __getitem__(self, *args):
17n/a return '%s:item' % type(self).__name__
18n/a def __getslice__(self, *args):
19n/a return '%s:slice' % type(self).__name__
20n/a
21n/aclass MockShell:
22n/a def __init__(self):
23n/a self.reset()
24n/a
25n/a def write(self, *args):
26n/a self.written.append(args)
27n/a
28n/a def readline(self):
29n/a return self.lines.pop()
30n/a
31n/a def close(self):
32n/a pass
33n/a
34n/a def reset(self):
35n/a self.written = []
36n/a
37n/a def push(self, lines):
38n/a self.lines = list(lines)[::-1]
39n/a
40n/a
41n/aclass PseudeOutputFilesTest(unittest.TestCase):
42n/a def test_misc(self):
43n/a shell = MockShell()
44n/a f = PseudoOutputFile(shell, 'stdout', 'utf-8')
45n/a self.assertIsInstance(f, io.TextIOBase)
46n/a self.assertEqual(f.encoding, 'utf-8')
47n/a self.assertIsNone(f.errors)
48n/a self.assertIsNone(f.newlines)
49n/a self.assertEqual(f.name, '<stdout>')
50n/a self.assertFalse(f.closed)
51n/a self.assertTrue(f.isatty())
52n/a self.assertFalse(f.readable())
53n/a self.assertTrue(f.writable())
54n/a self.assertFalse(f.seekable())
55n/a
56n/a def test_unsupported(self):
57n/a shell = MockShell()
58n/a f = PseudoOutputFile(shell, 'stdout', 'utf-8')
59n/a self.assertRaises(OSError, f.fileno)
60n/a self.assertRaises(OSError, f.tell)
61n/a self.assertRaises(OSError, f.seek, 0)
62n/a self.assertRaises(OSError, f.read, 0)
63n/a self.assertRaises(OSError, f.readline, 0)
64n/a
65n/a def test_write(self):
66n/a shell = MockShell()
67n/a f = PseudoOutputFile(shell, 'stdout', 'utf-8')
68n/a f.write('test')
69n/a self.assertEqual(shell.written, [('test', 'stdout')])
70n/a shell.reset()
71n/a f.write('t\xe8st')
72n/a self.assertEqual(shell.written, [('t\xe8st', 'stdout')])
73n/a shell.reset()
74n/a
75n/a f.write(S('t\xe8st'))
76n/a self.assertEqual(shell.written, [('t\xe8st', 'stdout')])
77n/a self.assertEqual(type(shell.written[0][0]), str)
78n/a shell.reset()
79n/a
80n/a self.assertRaises(TypeError, f.write)
81n/a self.assertEqual(shell.written, [])
82n/a self.assertRaises(TypeError, f.write, b'test')
83n/a self.assertRaises(TypeError, f.write, 123)
84n/a self.assertEqual(shell.written, [])
85n/a self.assertRaises(TypeError, f.write, 'test', 'spam')
86n/a self.assertEqual(shell.written, [])
87n/a
88n/a def test_writelines(self):
89n/a shell = MockShell()
90n/a f = PseudoOutputFile(shell, 'stdout', 'utf-8')
91n/a f.writelines([])
92n/a self.assertEqual(shell.written, [])
93n/a shell.reset()
94n/a f.writelines(['one\n', 'two'])
95n/a self.assertEqual(shell.written,
96n/a [('one\n', 'stdout'), ('two', 'stdout')])
97n/a shell.reset()
98n/a f.writelines(['on\xe8\n', 'tw\xf2'])
99n/a self.assertEqual(shell.written,
100n/a [('on\xe8\n', 'stdout'), ('tw\xf2', 'stdout')])
101n/a shell.reset()
102n/a
103n/a f.writelines([S('t\xe8st')])
104n/a self.assertEqual(shell.written, [('t\xe8st', 'stdout')])
105n/a self.assertEqual(type(shell.written[0][0]), str)
106n/a shell.reset()
107n/a
108n/a self.assertRaises(TypeError, f.writelines)
109n/a self.assertEqual(shell.written, [])
110n/a self.assertRaises(TypeError, f.writelines, 123)
111n/a self.assertEqual(shell.written, [])
112n/a self.assertRaises(TypeError, f.writelines, [b'test'])
113n/a self.assertRaises(TypeError, f.writelines, [123])
114n/a self.assertEqual(shell.written, [])
115n/a self.assertRaises(TypeError, f.writelines, [], [])
116n/a self.assertEqual(shell.written, [])
117n/a
118n/a def test_close(self):
119n/a shell = MockShell()
120n/a f = PseudoOutputFile(shell, 'stdout', 'utf-8')
121n/a self.assertFalse(f.closed)
122n/a f.write('test')
123n/a f.close()
124n/a self.assertTrue(f.closed)
125n/a self.assertRaises(ValueError, f.write, 'x')
126n/a self.assertEqual(shell.written, [('test', 'stdout')])
127n/a f.close()
128n/a self.assertRaises(TypeError, f.close, 1)
129n/a
130n/a
131n/aclass PseudeInputFilesTest(unittest.TestCase):
132n/a def test_misc(self):
133n/a shell = MockShell()
134n/a f = PseudoInputFile(shell, 'stdin', 'utf-8')
135n/a self.assertIsInstance(f, io.TextIOBase)
136n/a self.assertEqual(f.encoding, 'utf-8')
137n/a self.assertIsNone(f.errors)
138n/a self.assertIsNone(f.newlines)
139n/a self.assertEqual(f.name, '<stdin>')
140n/a self.assertFalse(f.closed)
141n/a self.assertTrue(f.isatty())
142n/a self.assertTrue(f.readable())
143n/a self.assertFalse(f.writable())
144n/a self.assertFalse(f.seekable())
145n/a
146n/a def test_unsupported(self):
147n/a shell = MockShell()
148n/a f = PseudoInputFile(shell, 'stdin', 'utf-8')
149n/a self.assertRaises(OSError, f.fileno)
150n/a self.assertRaises(OSError, f.tell)
151n/a self.assertRaises(OSError, f.seek, 0)
152n/a self.assertRaises(OSError, f.write, 'x')
153n/a self.assertRaises(OSError, f.writelines, ['x'])
154n/a
155n/a def test_read(self):
156n/a shell = MockShell()
157n/a f = PseudoInputFile(shell, 'stdin', 'utf-8')
158n/a shell.push(['one\n', 'two\n', ''])
159n/a self.assertEqual(f.read(), 'one\ntwo\n')
160n/a shell.push(['one\n', 'two\n', ''])
161n/a self.assertEqual(f.read(-1), 'one\ntwo\n')
162n/a shell.push(['one\n', 'two\n', ''])
163n/a self.assertEqual(f.read(None), 'one\ntwo\n')
164n/a shell.push(['one\n', 'two\n', 'three\n', ''])
165n/a self.assertEqual(f.read(2), 'on')
166n/a self.assertEqual(f.read(3), 'e\nt')
167n/a self.assertEqual(f.read(10), 'wo\nthree\n')
168n/a
169n/a shell.push(['one\n', 'two\n'])
170n/a self.assertEqual(f.read(0), '')
171n/a self.assertRaises(TypeError, f.read, 1.5)
172n/a self.assertRaises(TypeError, f.read, '1')
173n/a self.assertRaises(TypeError, f.read, 1, 1)
174n/a
175n/a def test_readline(self):
176n/a shell = MockShell()
177n/a f = PseudoInputFile(shell, 'stdin', 'utf-8')
178n/a shell.push(['one\n', 'two\n', 'three\n', 'four\n'])
179n/a self.assertEqual(f.readline(), 'one\n')
180n/a self.assertEqual(f.readline(-1), 'two\n')
181n/a self.assertEqual(f.readline(None), 'three\n')
182n/a shell.push(['one\ntwo\n'])
183n/a self.assertEqual(f.readline(), 'one\n')
184n/a self.assertEqual(f.readline(), 'two\n')
185n/a shell.push(['one', 'two', 'three'])
186n/a self.assertEqual(f.readline(), 'one')
187n/a self.assertEqual(f.readline(), 'two')
188n/a shell.push(['one\n', 'two\n', 'three\n'])
189n/a self.assertEqual(f.readline(2), 'on')
190n/a self.assertEqual(f.readline(1), 'e')
191n/a self.assertEqual(f.readline(1), '\n')
192n/a self.assertEqual(f.readline(10), 'two\n')
193n/a
194n/a shell.push(['one\n', 'two\n'])
195n/a self.assertEqual(f.readline(0), '')
196n/a self.assertRaises(TypeError, f.readlines, 1.5)
197n/a self.assertRaises(TypeError, f.readlines, '1')
198n/a self.assertRaises(TypeError, f.readlines, 1, 1)
199n/a
200n/a def test_readlines(self):
201n/a shell = MockShell()
202n/a f = PseudoInputFile(shell, 'stdin', 'utf-8')
203n/a shell.push(['one\n', 'two\n', ''])
204n/a self.assertEqual(f.readlines(), ['one\n', 'two\n'])
205n/a shell.push(['one\n', 'two\n', ''])
206n/a self.assertEqual(f.readlines(-1), ['one\n', 'two\n'])
207n/a shell.push(['one\n', 'two\n', ''])
208n/a self.assertEqual(f.readlines(None), ['one\n', 'two\n'])
209n/a shell.push(['one\n', 'two\n', ''])
210n/a self.assertEqual(f.readlines(0), ['one\n', 'two\n'])
211n/a shell.push(['one\n', 'two\n', ''])
212n/a self.assertEqual(f.readlines(3), ['one\n'])
213n/a shell.push(['one\n', 'two\n', ''])
214n/a self.assertEqual(f.readlines(4), ['one\n', 'two\n'])
215n/a
216n/a shell.push(['one\n', 'two\n', ''])
217n/a self.assertRaises(TypeError, f.readlines, 1.5)
218n/a self.assertRaises(TypeError, f.readlines, '1')
219n/a self.assertRaises(TypeError, f.readlines, 1, 1)
220n/a
221n/a def test_close(self):
222n/a shell = MockShell()
223n/a f = PseudoInputFile(shell, 'stdin', 'utf-8')
224n/a shell.push(['one\n', 'two\n', ''])
225n/a self.assertFalse(f.closed)
226n/a self.assertEqual(f.readline(), 'one\n')
227n/a f.close()
228n/a self.assertFalse(f.closed)
229n/a self.assertEqual(f.readline(), 'two\n')
230n/a self.assertRaises(TypeError, f.close, 1)
231n/a
232n/a
233n/aif __name__ == '__main__':
234n/a unittest.main(verbosity=2)