-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_terminal.py
111 lines (93 loc) · 4.16 KB
/
test_terminal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import, unicode_literals
import os
import time
import six
from mog_commons.terminal import TerminalHandler
from mog_commons.unittest import TestCase, base_unittest, FakeBytesInput, FakeInput
class TestTerminal(TestCase):
def test_getch_from_file(self):
with open(os.path.join('tests', 'resources', 'test_terminal_input.txt')) as f:
t = TerminalHandler(stdin=f)
self.assertEqual(t.getch(), 'a')
self.assertRaises(EOFError, t.getch)
@base_unittest.skipUnless(os.name != 'nt', 'requires POSIX compatible')
def test_getch(self):
self.assertEqual(TerminalHandler(stdin=FakeBytesInput(b'')).getch(), '')
self.assertEqual(TerminalHandler(stdin=FakeBytesInput(b'\x03')).getch(), '\x03')
self.assertEqual(TerminalHandler(stdin=FakeBytesInput(b'abc')).getch(), 'a')
self.assertEqual(TerminalHandler(stdin=FakeBytesInput('あ'.encode('utf-8'))).getch(), '')
self.assertEqual(TerminalHandler(stdin=FakeBytesInput('あ'.encode('sjis'))).getch(), '')
def test_getch_disabled(self):
t = TerminalHandler(stdin=FakeInput('a\nb\ncd\ne\n'), keep_input_clean=False, getch_enabled=False)
self.assertEqual(t.getch(), 'a')
self.assertEqual(t.getch(), 'b')
self.assertEqual(t.getch(), 'c')
self.assertEqual(t.getch(), 'e')
self.assertRaises(EOFError, t.getch)
@base_unittest.skipUnless(os.name != 'nt', 'requires POSIX compatible')
def test_getch_key_repeat(self):
fin = FakeBytesInput(b'abcde')
def append_char(ch):
fin.write(ch)
fin.seek(-len(ch), 1)
t1 = TerminalHandler(stdin=fin)
self.assertEqual(t1.getch(), 'a')
append_char(b'x')
self.assertEqual(t1.getch(), 'x')
append_char(b'x')
self.assertEqual(t1.getch(), '')
append_char(b'x')
self.assertEqual(t1.getch(), '')
append_char(b'y')
self.assertEqual(t1.getch(), 'y')
append_char(b'y')
self.assertEqual(t1.getch(), '')
time.sleep(1)
append_char(b'y')
self.assertEqual(t1.getch(), 'y')
@base_unittest.skipUnless(os.name != 'nt', 'requires POSIX compatible')
def test_getch_key_repeat_disabled(self):
fin = FakeBytesInput(b'abcde')
def append_char(ch):
fin.write(ch)
fin.seek(-len(ch), 1)
t1 = TerminalHandler(stdin=fin, getch_repeat_threshold=0)
self.assertEqual(t1.getch(), 'a')
append_char(b'x')
self.assertEqual(t1.getch(), 'x')
append_char(b'x')
self.assertEqual(t1.getch(), 'x')
append_char(b'x')
self.assertEqual(t1.getch(), 'x')
append_char(b'y')
self.assertEqual(t1.getch(), 'y')
append_char(b'y')
self.assertEqual(t1.getch(), 'y')
time.sleep(1)
append_char(b'y')
self.assertEqual(t1.getch(), 'y')
@base_unittest.skipUnless(os.name != 'nt', 'requires POSIX compatible')
def test_getch_not_keep_input_clean(self):
fin = FakeBytesInput(b'abcde')
t1 = TerminalHandler(stdin=fin, keep_input_clean=False)
self.assertEqual(t1.getch(), 'a')
self.assertEqual(t1.getch(), 'b')
self.assertEqual(t1.getch(), 'c')
self.assertEqual(t1.getch(), 'd')
self.assertEqual(t1.getch(), 'e')
self.assertEqual(t1.getch(), '')
def test_resolve_encoding(self):
import io
import codecs
if six.PY2:
out = codecs.getwriter('sjis')
out.encoding = 'sjis'
else:
out = io.TextIOWrapper(six.StringIO(), 'sjis')
self.assertEqual(TerminalHandler._detect_encoding(out), 'sjis')
def test_init(self):
self.assertEqual(TerminalHandler(stdin=six.StringIO(), getch_enabled=False).getch_enabled, False)
self.assertEqual(TerminalHandler(stdin=six.StringIO(), getch_enabled=True).getch_enabled, False)
self.assertEqual(TerminalHandler(stdin=FakeInput(), getch_enabled=False).getch_enabled, False)
self.assertEqual(TerminalHandler(stdin=FakeInput(), getch_enabled=True).getch_enabled, True)