##// END OF EJS Templates
More frontend tests.
gvaroquaux -
Show More
@@ -1,124 +1,135 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Test process execution and IO redirection.
3 Test process execution and IO redirection.
4 """
4 """
5
5
6 __docformat__ = "restructuredtext en"
6 __docformat__ = "restructuredtext en"
7
7
8 #-------------------------------------------------------------------------------
8 #-------------------------------------------------------------------------------
9 # Copyright (C) 2008 The IPython Development Team
9 # Copyright (C) 2008 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is
11 # Distributed under the terms of the BSD License. The full license is
12 # in the file COPYING, distributed as part of this software.
12 # in the file COPYING, distributed as part of this software.
13 #-------------------------------------------------------------------------------
13 #-------------------------------------------------------------------------------
14
14
15 from IPython.frontend.prefilterfrontend import PrefilterFrontEnd
15 from IPython.frontend.prefilterfrontend import PrefilterFrontEnd
16 from cStringIO import StringIO
16 from cStringIO import StringIO
17 import string
17 import string
18
18
19 class TestPrefilterFrontEnd(PrefilterFrontEnd):
19 class TestPrefilterFrontEnd(PrefilterFrontEnd):
20
20
21 input_prompt_template = string.Template('')
21 input_prompt_template = string.Template('')
22 output_prompt_template = string.Template('')
22 output_prompt_template = string.Template('')
23
23
24 edit_buffer = ''
24 edit_buffer = ''
25
25
26
26
27 def __init__(self):
27 def __init__(self):
28 self.out = StringIO()
28 self.out = StringIO()
29 PrefilterFrontEnd.__init__(self)
29 PrefilterFrontEnd.__init__(self)
30
30
31 def get_current_edit_buffer(self):
31 def get_current_edit_buffer(self):
32 return self.edit_buffer
32 return self.edit_buffer
33
33
34 def add_to_edit_buffer(self, string):
34 def add_to_edit_buffer(self, string):
35 self.edit_buffer += string
35 self.edit_buffer += string
36
36
37 def write(self, string):
37 def write(self, string):
38 self.out.write(string)
38 self.out.write(string)
39
39
40 def _on_enter(self):
40 def _on_enter(self):
41 self.add_to_edit_buffer('\n')
41 self.add_to_edit_buffer('\n')
42 PrefilterFrontEnd._on_enter(self)
42 PrefilterFrontEnd._on_enter(self)
43
43
44 def new_prompt(self, prompt):
44 def new_prompt(self, prompt):
45 self.edit_buffer = ''
45 self.edit_buffer = ''
46 PrefilterFrontEnd.new_prompt(self, prompt)
46 PrefilterFrontEnd.new_prompt(self, prompt)
47
47
48
48
49 def test_execution():
49 def test_execution():
50 """ Test execution of a command.
50 """ Test execution of a command.
51 """
51 """
52 f = TestPrefilterFrontEnd()
52 f = TestPrefilterFrontEnd()
53 f.edit_buffer='print 1\n'
53 f.edit_buffer='print 1\n'
54 f._on_enter()
54 f._on_enter()
55 assert f.out.getvalue() == '1\n'
55 assert f.out.getvalue() == '1\n'
56
56
57
57
58 def test_multiline():
58 def test_multiline():
59 """ Test execution of a multiline command.
59 """ Test execution of a multiline command.
60 """
60 """
61 f = TestPrefilterFrontEnd()
61 f = TestPrefilterFrontEnd()
62 f.edit_buffer='if True:'
62 f.edit_buffer='if True:'
63 f._on_enter()
63 f._on_enter()
64 f.add_to_edit_buffer('print 1')
64 f.add_to_edit_buffer('print 1')
65 f._on_enter()
65 f._on_enter()
66 assert f.out.getvalue() == ''
66 assert f.out.getvalue() == ''
67 f._on_enter()
67 f._on_enter()
68 assert f.out.getvalue() == '1\n'
68 assert f.out.getvalue() == '1\n'
69 f = TestPrefilterFrontEnd()
69 f = TestPrefilterFrontEnd()
70 f.edit_buffer='(1 +'
70 f.edit_buffer='(1 +'
71 f._on_enter()
71 f._on_enter()
72 f.add_to_edit_buffer('0)')
72 f.add_to_edit_buffer('0)')
73 f._on_enter()
73 f._on_enter()
74 assert f.out.getvalue() == ''
74 assert f.out.getvalue() == ''
75 f._on_enter()
75 f._on_enter()
76 assert f.out.getvalue() == '1\n'
76 assert f.out.getvalue() == '1\n'
77
77
78
78
79 def test_capture():
79 def test_capture():
80 """ Test the capture of output in different channels.
80 """ Test the capture of output in different channels.
81 """
81 """
82 # Test on the OS-level stdout, stderr.
82 # Test on the OS-level stdout, stderr.
83 f = TestPrefilterFrontEnd()
83 f = TestPrefilterFrontEnd()
84 f.edit_buffer='import os; out=os.fdopen(1, "w"); out.write("1") ; out.flush()'
84 f.edit_buffer='import os; out=os.fdopen(1, "w"); out.write("1") ; out.flush()'
85 f._on_enter()
85 f._on_enter()
86 assert f.out.getvalue() == '1'
86 assert f.out.getvalue() == '1'
87 f = TestPrefilterFrontEnd()
87 f = TestPrefilterFrontEnd()
88 f.edit_buffer='import os; out=os.fdopen(2, "w"); out.write("1") ; out.flush()'
88 f.edit_buffer='import os; out=os.fdopen(2, "w"); out.write("1") ; out.flush()'
89 f._on_enter()
89 f._on_enter()
90 assert f.out.getvalue() == '1'
90 assert f.out.getvalue() == '1'
91
91
92
92
93 def test_magic():
93 def test_magic():
94 """ Test the magic expansion and history.
94 """ Test the magic expansion and history.
95
95
96 This test is fairly fragile and will break when magics change.
96 This test is fairly fragile and will break when magics change.
97 """
97 """
98 pass
99
100 if True:
101 f = TestPrefilterFrontEnd()
98 f = TestPrefilterFrontEnd()
102 f.edit_buffer='%hist'
103 f._on_enter()
104 assert f.out.getvalue() == '1: _ip.magic("hist ")\n'
105 f.out.reset()
106 f.out.truncate()
107 f.add_to_edit_buffer('%who\n')
99 f.add_to_edit_buffer('%who\n')
108 f._on_enter()
100 f._on_enter()
109 assert f.out.getvalue() == 'Interactive namespace is empty.\n'
101 assert f.out.getvalue() == 'Interactive namespace is empty.\n'
110 f.out.reset()
102
111 f.out.truncate()
103
112 f.add_to_edit_buffer('%hist')
104 def test_help():
105 """ Test object inspection.
106 """
107 f = TestPrefilterFrontEnd()
108 f.add_to_edit_buffer("def f():")
113 f._on_enter()
109 f._on_enter()
114 assert f.out.getvalue() == """1: _ip.magic("hist ")
110 f.add_to_edit_buffer("'foobar'")
115 2: _ip.magic("who ")
111 f._on_enter()
116 3: _ip.magic("hist ")
112 f.add_to_edit_buffer("pass")
117 """
113 f._on_enter()
118
114 f._on_enter()
119
115 f.add_to_edit_buffer("f?")
116 f._on_enter()
117 assert f.out.getvalue().split()[-1] == 'foobar'
118
119 def test_completion():
120 """ Test command-line completion.
121 """
122 f = TestPrefilterFrontEnd()
123 f.edit_buffer = 'zzza = 1'
124 f._on_enter()
125 f.edit_buffer = 'zzzb = 2'
126 f._on_enter()
127 f.edit_buffer = 'zz'
128
120
129
121 if __name__ == '__main__':
130 if __name__ == '__main__':
131 test_magic()
132 test_help()
122 test_execution()
133 test_execution()
123 test_multiline()
134 test_multiline()
124 test_capture()
135 test_capture()
General Comments 0
You need to be logged in to leave comments. Login now