22import sys
33import unittest
44from itertools import islice
5+ import collections
56from mock import Mock , MagicMock
67try :
78 from unittest import skip
@@ -13,6 +14,7 @@ def skip(f):
1314
1415from bpython import config , repl , cli , autocomplete
1516
17+
1618def setup_config (conf ):
1719 config_struct = config .Struct ()
1820 config .loadini (config_struct , os .devnull )
@@ -253,6 +255,48 @@ def test_nonexistent_name(self):
253255 self .assertFalse (self .repl .get_args ())
254256
255257
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+
256300class TestRepl (unittest .TestCase ):
257301
258302 def setInputLine (self , line ):
0 commit comments