2
2
import sys
3
3
import unittest
4
4
from itertools import islice
5
+ import collections
5
6
from mock import Mock , MagicMock
6
7
try :
7
8
from unittest import skip
@@ -13,6 +14,7 @@ def skip(f):
13
14
14
15
from bpython import config , repl , cli , autocomplete
15
16
17
+
16
18
def setup_config (conf ):
17
19
config_struct = config .Struct ()
18
20
config .loadini (config_struct , os .devnull )
@@ -253,6 +255,48 @@ def test_nonexistent_name(self):
253
255
self .assertFalse (self .repl .get_args ())
254
256
255
257
258
+ class TestGetSource (unittest .TestCase ):
259
+ def setUp (self ):
260
+ self .repl = FakeRepl ()
261
+
262
+ def set_input_line (self , line ):
263
+ """Set current input line of the test REPL."""
264
+ self .repl .current_line = line
265
+ self .repl .cursor_offset = len (line )
266
+
267
+ def assert_get_source_error_for_current_function (self , func , msg ):
268
+ self .repl .current_func = func
269
+ self .assertRaises (repl .SourceNotFound , self .repl .get_source_of_current_name )
270
+ try :
271
+ self .repl .get_source_of_current_name ()
272
+ except repl .SourceNotFound as e :
273
+ self .assertEqual (e .args [0 ], msg )
274
+
275
+ def test_current_function (self ):
276
+ self .set_input_line ('INPUTLINE' )
277
+ self .repl .current_func = collections .MutableSet .add
278
+ self .assertTrue ("Add an element." in self .repl .get_source_of_current_name ())
279
+
280
+ self .assert_get_source_error_for_current_function (
281
+ collections .defaultdict .copy , "No source code found for INPUTLINE" )
282
+
283
+ self .assert_get_source_error_for_current_function (
284
+ collections .defaultdict , "could not find class definition" )
285
+
286
+ self .assert_get_source_error_for_current_function (
287
+ [], "No source code found for INPUTLINE" )
288
+
289
+ self .assert_get_source_error_for_current_function (
290
+ list .pop , "No source code found for INPUTLINE" )
291
+
292
+ def test_current_line (self ):
293
+ self .repl .interp .locals ['a' ] = collections .MutableSet
294
+ self .set_input_line ('a' )
295
+ self .assertTrue ('Add an element.' in self .repl .get_source_of_current_name ())
296
+
297
+ #TODO add tests for various failures without using current function
298
+
299
+
256
300
class TestRepl (unittest .TestCase ):
257
301
258
302
def setInputLine (self , line ):
0 commit comments