1
+ import collections
2
+ from itertools import islice
1
3
import os
4
+ import socket
2
5
import sys
3
6
import unittest
4
- from itertools import islice
7
+
5
8
from mock import Mock , MagicMock
9
+
6
10
try :
7
11
from unittest import skip
8
12
except ImportError :
@@ -13,6 +17,7 @@ def skip(f):
13
17
14
18
from bpython import config , repl , cli , autocomplete
15
19
20
+
16
21
def setup_config (conf ):
17
22
config_struct = config .Struct ()
18
23
config .loadini (config_struct , os .devnull )
@@ -253,6 +258,48 @@ def test_nonexistent_name(self):
253
258
self .assertFalse (self .repl .get_args ())
254
259
255
260
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
+
256
303
class TestRepl (unittest .TestCase ):
257
304
258
305
def setInputLine (self , line ):
0 commit comments