Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e59ae5

Browse files
committed
Rewrite bunch of raise AssertionError and assert False tests
1 parent 5e3d218 commit 0e59ae5

6 files changed

Lines changed: 32 additions & 42 deletions

File tree

IPython/core/tests/test_compilerop.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ def test_compiler_check_cache():
6666
cp.cache('x=1', 99)
6767
# Ensure now that after clearing the cache, our entries survive
6868
linecache.checkcache()
69-
for k in linecache.cache:
70-
if k.startswith('<ipython-input-99'):
71-
break
72-
else:
73-
raise AssertionError('Entry for input-99 missing from linecache')
69+
assert any(
70+
k.startswith("<ipython-input-99") for k in linecache.cache
71+
), "Entry for input-99 missing from linecache"

IPython/core/tests/test_debugger.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,8 @@ def test_interruptible_core_debugger():
242242
"""
243243
def raising_input(msg="", called=[0]):
244244
called[0] += 1
245-
if called[0] == 1:
246-
raise KeyboardInterrupt()
247-
else:
248-
raise AssertionError("input() should only be called once!")
245+
assert called[0] == 1, "input() should only be called once!"
246+
raise KeyboardInterrupt()
249247

250248
tracer_orig = sys.gettrace()
251249
try:

IPython/core/tests/test_hooks.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Imports
77
#-----------------------------------------------------------------------------
88

9+
import pytest
910
from IPython.core.error import TryNext
1011
from IPython.core.hooks import CommandChainDispatcher
1112

@@ -41,12 +42,9 @@ def test_command_chain_dispatcher_ff():
4142
fail2 = Fail("fail2")
4243
dp = CommandChainDispatcher([(0, fail1), (10, fail2)])
4344

44-
try:
45+
with pytest.raises(TryNext) as e:
4546
dp()
46-
except TryNext as e:
47-
assert str(e) == "fail2"
48-
else:
49-
assert False, "Expected exception was not raised."
47+
assert str(e.value) == "fail2"
5048

5149
assert fail1.called is True
5250
assert fail2.called is True

IPython/core/tests/test_logger.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
from IPython.utils.tempdir import TemporaryDirectory
88

99
def test_logstart_inaccessible_file():
10-
try:
10+
with pytest.raises(IOError):
1111
_ip.logger.logstart(logfname="/") # Opening that filename will fail.
12-
except IOError:
13-
pass
14-
else:
15-
assert False # The try block should never pass.
1612

1713
try:
1814
_ip.run_cell("a=1") # Check it doesn't try to log this

IPython/core/tests/test_magic.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ def test_config_print_class():
132132
_ip.magic('config TerminalInteractiveShell')
133133

134134
stdout = captured.stdout
135-
if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
136-
print(stdout)
137-
raise AssertionError("1st line of stdout not like "
138-
"'TerminalInteractiveShell.* options'")
135+
assert re.match(
136+
"TerminalInteractiveShell.* options", stdout.splitlines()[0]
137+
), f"{stdout}\n\n1st line of stdout not like 'TerminalInteractiveShell.* options'"
138+
139139

140140
def test_rehashx():
141141
# clear up everything
@@ -1219,12 +1219,9 @@ def test_edit_interactive():
12191219
n = ip.execution_count
12201220
ip.run_cell("def foo(): return 1", store_history=True)
12211221

1222-
try:
1222+
with pytest.raises(code.InteractivelyDefined) as e:
12231223
_run_edit_test("foo")
1224-
except code.InteractivelyDefined as e:
1225-
assert e.index == n
1226-
else:
1227-
raise AssertionError("Should have raised InteractivelyDefined")
1224+
assert e.value.index == n
12281225

12291226

12301227
def test_edit_cell():

IPython/utils/tests/test_text.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ def test_columnize_random():
7070
out = text.columnize(items, row_first=row_first, displaywidth=displaywidth)
7171
longer_line = max([len(x) for x in out.split('\n')])
7272
longer_element = max(rand_len)
73-
if longer_line > displaywidth:
74-
print("Columnize displayed something lager than displaywidth : %s " % longer_line)
75-
print("longer element : %s " % longer_element)
76-
print("displaywidth : %s " % displaywidth)
77-
print("number of element : %s " % nitems)
78-
print("size of each element :\n %s" % rand_len)
79-
assert False, "row_first={0}".format(row_first)
73+
assert longer_line <= displaywidth, (
74+
f"Columnize displayed something lager than displaywidth : {longer_line}\n"
75+
f"longer element : {longer_element}\n"
76+
f"displaywidth : {displaywidth}\n"
77+
f"number of element : {nitems}\n"
78+
f"size of each element : {rand_len}\n"
79+
f"row_first={row_first}\n"
80+
)
8081

8182

8283
# TODO: pytest mark.parametrize once nose removed.
@@ -103,9 +104,9 @@ def eval_formatter_check(f):
103104
ns = dict(n=12, pi=math.pi, stuff='hello there', os=os, u=u"café", b="café")
104105
s = f.format("{n} {n//4} {stuff.split()[0]}", **ns)
105106
assert s == "12 3 hello"
106-
s = f.format(' '.join(['{n//%i}'%i for i in range(1,8)]), **ns)
107+
s = f.format(" ".join(["{n//%i}" % i for i in range(1, 8)]), **ns)
107108
assert s == "12 6 4 3 2 2 1"
108-
s = f.format('{[n//i for i in range(1,8)]}', **ns)
109+
s = f.format("{[n//i for i in range(1,8)]}", **ns)
109110
assert s == "[12, 6, 4, 3, 2, 2, 1]"
110111
s = f.format("{stuff!s}", **ns)
111112
assert s == ns["stuff"]
@@ -133,9 +134,9 @@ def eval_formatter_slicing_check(f):
133134
pytest.raises(SyntaxError, f.format, "{n:x}", **ns)
134135

135136
def eval_formatter_no_slicing_check(f):
136-
ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
137-
138-
s = f.format('{n:x} {pi**2:+f}', **ns)
137+
ns = dict(n=12, pi=math.pi, stuff="hello there", os=os)
138+
139+
s = f.format("{n:x} {pi**2:+f}", **ns)
139140
assert s == "c +9.869604"
140141

141142
s = f.format("{stuff[slice(1,4)]}", **ns)
@@ -187,17 +188,19 @@ def test_strip_email():
187188

188189

189190
def test_strip_email2():
190-
src = '> > > list()'
191-
cln = 'list()'
191+
src = "> > > list()"
192+
cln = "list()"
192193
assert text.strip_email_quotes(src) == cln
193194

195+
194196
def test_LSString():
195197
lss = text.LSString("abc\ndef")
196198
assert lss.l == ["abc", "def"]
197199
assert lss.s == "abc def"
198200
lss = text.LSString(os.getcwd())
199201
assert isinstance(lss.p[0], Path)
200202

203+
201204
def test_SList():
202205
sl = text.SList(["a 11", "b 1", "a 2"])
203206
assert sl.n == "a 11\nb 1\na 2"

0 commit comments

Comments
 (0)