blob: ec26f068fcb89ceb83723ab29bde2cdec5149584 [file] [log] [blame]
Steve Dower39294992016-08-30 21:22:36 -07001'''Tests for WindowsConsoleIO
2
3Unfortunately, most testing requires interactive use, since we have no
4API to read back from a real console, and this class is only for use
5with real consoles.
6
7Instead, we validate that basic functionality such as opening, closing
8and in particular fileno() work, but are forced to leave real testing
9to real people with real keyborads.
10'''
11
12import io
13import unittest
14import sys
15
16if sys.platform != 'win32':
17 raise unittest.SkipTest("test only relevant on win32")
18
19ConIO = io._WindowsConsoleIO
20
21class WindowsConsoleIOTests(unittest.TestCase):
22 def test_abc(self):
23 self.assertTrue(issubclass(ConIO, io.RawIOBase))
24 self.assertFalse(issubclass(ConIO, io.BufferedIOBase))
25 self.assertFalse(issubclass(ConIO, io.TextIOBase))
26
27 def test_open_fd(self):
Steve Dower33df0c32016-09-08 14:36:18 -070028 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070029 f = ConIO(0)
Steve Dower33df0c32016-09-08 14:36:18 -070030 except ValueError:
31 # cannot open console because it's not a real console
32 pass
33 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070034 self.assertTrue(f.readable())
35 self.assertFalse(f.writable())
36 self.assertEqual(0, f.fileno())
37 f.close() # multiple close should not crash
38 f.close()
Steve Dower39294992016-08-30 21:22:36 -070039
Steve Dower33df0c32016-09-08 14:36:18 -070040 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070041 f = ConIO(1, 'w')
Steve Dower33df0c32016-09-08 14:36:18 -070042 except ValueError:
43 # cannot open console because it's not a real console
44 pass
45 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070046 self.assertFalse(f.readable())
47 self.assertTrue(f.writable())
48 self.assertEqual(1, f.fileno())
49 f.close()
50 f.close()
Steve Dower39294992016-08-30 21:22:36 -070051
Steve Dower33df0c32016-09-08 14:36:18 -070052 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070053 f = ConIO(2, 'w')
Steve Dower33df0c32016-09-08 14:36:18 -070054 except ValueError:
55 # cannot open console because it's not a real console
56 pass
57 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070058 self.assertFalse(f.readable())
59 self.assertTrue(f.writable())
60 self.assertEqual(2, f.fileno())
61 f.close()
62 f.close()
Steve Dower39294992016-08-30 21:22:36 -070063
64 def test_open_name(self):
65 f = ConIO("CON")
66 self.assertTrue(f.readable())
67 self.assertFalse(f.writable())
68 self.assertIsNotNone(f.fileno())
69 f.close() # multiple close should not crash
70 f.close()
71
72 f = ConIO('CONIN$')
73 self.assertTrue(f.readable())
74 self.assertFalse(f.writable())
75 self.assertIsNotNone(f.fileno())
76 f.close()
77 f.close()
78
79 f = ConIO('CONOUT$', 'w')
80 self.assertFalse(f.readable())
81 self.assertTrue(f.writable())
82 self.assertIsNotNone(f.fileno())
83 f.close()
84 f.close()
85
86if __name__ == "__main__":
87 unittest.main()