Skip to content

Commit 7cb9d8b

Browse files
some tests for getting source
1 parent 7c274fe commit 7cb9d8b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

bpython/test/test_repl.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import unittest
44
from itertools import islice
5+
import collections
56
from mock import Mock, MagicMock
67
try:
78
from unittest import skip
@@ -13,6 +14,7 @@ def skip(f):
1314

1415
from bpython import config, repl, cli, autocomplete
1516

17+
1618
def 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+
256300
class TestRepl(unittest.TestCase):
257301

258302
def setInputLine(self, line):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy