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