blob: b1a2f7a302540fc8e2b4b1236e4fffaa997faaa9 [file] [log] [blame]
Steve Dower39294992016-08-30 21:22:36 -07001'''Tests for WindowsConsoleIO
Steve Dower39294992016-08-30 21:22:36 -07002'''
3
4import io
5import unittest
6import sys
7
8if sys.platform != 'win32':
9 raise unittest.SkipTest("test only relevant on win32")
10
Steve Dower312cef72016-10-03 09:04:58 -070011from _testconsole import write_input
12
Steve Dower39294992016-08-30 21:22:36 -070013ConIO = io._WindowsConsoleIO
14
15class WindowsConsoleIOTests(unittest.TestCase):
16 def test_abc(self):
17 self.assertTrue(issubclass(ConIO, io.RawIOBase))
18 self.assertFalse(issubclass(ConIO, io.BufferedIOBase))
19 self.assertFalse(issubclass(ConIO, io.TextIOBase))
20
21 def test_open_fd(self):
Steve Dower33df0c32016-09-08 14:36:18 -070022 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070023 f = ConIO(0)
Steve Dower33df0c32016-09-08 14:36:18 -070024 except ValueError:
25 # cannot open console because it's not a real console
26 pass
27 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070028 self.assertTrue(f.readable())
29 self.assertFalse(f.writable())
30 self.assertEqual(0, f.fileno())
31 f.close() # multiple close should not crash
32 f.close()
Steve Dower39294992016-08-30 21:22:36 -070033
Steve Dower33df0c32016-09-08 14:36:18 -070034 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070035 f = ConIO(1, 'w')
Steve Dower33df0c32016-09-08 14:36:18 -070036 except ValueError:
37 # cannot open console because it's not a real console
38 pass
39 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070040 self.assertFalse(f.readable())
41 self.assertTrue(f.writable())
42 self.assertEqual(1, f.fileno())
43 f.close()
44 f.close()
Steve Dower39294992016-08-30 21:22:36 -070045
Steve Dower33df0c32016-09-08 14:36:18 -070046 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070047 f = ConIO(2, 'w')
Steve Dower33df0c32016-09-08 14:36:18 -070048 except ValueError:
49 # cannot open console because it's not a real console
50 pass
51 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070052 self.assertFalse(f.readable())
53 self.assertTrue(f.writable())
54 self.assertEqual(2, f.fileno())
55 f.close()
56 f.close()
Steve Dower39294992016-08-30 21:22:36 -070057
58 def test_open_name(self):
59 f = ConIO("CON")
60 self.assertTrue(f.readable())
61 self.assertFalse(f.writable())
62 self.assertIsNotNone(f.fileno())
63 f.close() # multiple close should not crash
64 f.close()
65
66 f = ConIO('CONIN$')
67 self.assertTrue(f.readable())
68 self.assertFalse(f.writable())
69 self.assertIsNotNone(f.fileno())
70 f.close()
71 f.close()
72
73 f = ConIO('CONOUT$', 'w')
74 self.assertFalse(f.readable())
75 self.assertTrue(f.writable())
76 self.assertIsNotNone(f.fileno())
77 f.close()
78 f.close()
79
Steve Dower312cef72016-10-03 09:04:58 -070080 def assertStdinRoundTrip(self, text):
81 stdin = open('CONIN$', 'r')
82 old_stdin = sys.stdin
83 try:
84 sys.stdin = stdin
85 write_input(
86 stdin.buffer.raw,
87 (text + '\r\n').encode('utf-16-le', 'surrogatepass')
88 )
89 actual = input()
90 finally:
91 sys.stdin = old_stdin
92 self.assertEqual(actual, text)
93
94 def test_input(self):
95 # ASCII
96 self.assertStdinRoundTrip('abc123')
97 # Non-ASCII
98 self.assertStdinRoundTrip('ϼўТλФЙ')
99 # Combining characters
100 self.assertStdinRoundTrip('A͏B ﬖ̳AA̝')
101 # Non-BMP
102 self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd')
103
104 def test_partial_reads(self):
105 # Test that reading less than 1 full character works when stdin
106 # contains multibyte UTF-8 sequences
107 source = 'ϼўТλФЙ\r\n'.encode('utf-16-le')
108 expected = 'ϼўТλФЙ\r\n'.encode('utf-8')
109 for read_count in range(1, 16):
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700110 with open('CONIN$', 'rb', buffering=0) as stdin:
111 write_input(stdin, source)
Steve Dower312cef72016-10-03 09:04:58 -0700112
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700113 actual = b''
114 while not actual.endswith(b'\n'):
115 b = stdin.read(read_count)
116 actual += b
Steve Dower312cef72016-10-03 09:04:58 -0700117
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700118 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
Steve Dower312cef72016-10-03 09:04:58 -0700119
120 def test_partial_surrogate_reads(self):
121 # Test that reading less than 1 full character works when stdin
122 # contains surrogate pairs that cannot be decoded to UTF-8 without
123 # reading an extra character.
124 source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le')
125 expected = '\U00101FFF\U00101001\r\n'.encode('utf-8')
126 for read_count in range(1, 16):
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700127 with open('CONIN$', 'rb', buffering=0) as stdin:
128 write_input(stdin, source)
129
130 actual = b''
131 while not actual.endswith(b'\n'):
132 b = stdin.read(read_count)
133 actual += b
134
135 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
136
137 def test_ctrl_z(self):
138 with open('CONIN$', 'rb', buffering=0) as stdin:
139 source = '\xC4\x1A\r\n'.encode('utf-16-le')
140 expected = '\xC4'.encode('utf-8')
Steve Dower312cef72016-10-03 09:04:58 -0700141 write_input(stdin, source)
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700142 a, b = stdin.read(1), stdin.readall()
143 self.assertEqual(expected[0:1], a)
144 self.assertEqual(expected[1:], b)
Steve Dower312cef72016-10-03 09:04:58 -0700145
Steve Dower39294992016-08-30 21:22:36 -0700146if __name__ == "__main__":
147 unittest.main()