Skip to content

Commit a51efa5

Browse files
committed
improve coverage
1 parent ada11d1 commit a51efa5

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

unpythonic/fploop.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def loop(*args, **kwargs):
120120
try:
121121
if not arity_includes(body, 1):
122122
raise ValueError("Body arity mismatch. (Is 'loop' parameter declared? Do all extra parameters have their defaults set?)")
123-
except UnknownArity: # well, we tried!
123+
except UnknownArity: # well, we tried! # pragma: no cover
124124
pass
125125
tb = trampolined(body) # enable "return jump(...)"
126126
return tb(loop) # like @call, run the (now trampolined) body.
@@ -169,7 +169,7 @@ def loop(*args, **kwargs):
169169
try:
170170
if not arity_includes(body, 2):
171171
raise ValueError("Body arity mismatch. (Are (loop, brk) declared? Do all extra parameters have their defaults set?)")
172-
except UnknownArity: # well, we tried!
172+
except UnknownArity: # well, we tried! # pragma: no cover
173173
pass
174174
tb = trampolined(body)
175175
return tb(loop, brk)
@@ -261,7 +261,7 @@ def loop(*args, **kwargs):
261261
try:
262262
if not arity_includes(body, 3):
263263
raise ValueError("Body arity mismatch. (Are (loop, x, acc) declared? Do all extra parameters have their defaults set?)")
264-
except UnknownArity: # well, we tried!
264+
except UnknownArity: # well, we tried! # pragma: no cover
265265
pass
266266
try:
267267
x0 = next(it)
@@ -332,7 +332,7 @@ def loop(*args, **kwargs):
332332
try:
333333
if not arity_includes(body, 5):
334334
raise ValueError("Body arity mismatch. (Are (loop, x, acc, cnt, brk) declared? Do all extra parameters have their defaults set?)")
335-
except UnknownArity: # well, we tried!
335+
except UnknownArity: # well, we tried! # pragma: no cover
336336
pass
337337
try:
338338
x0 = next(it)

unpythonic/test/test_fploop.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ def result(loop, brk, acc=0, i=0):
327327
return loop(acc + i, i + 1) # provide the additional parameters
328328
test[result == 45]
329329

330+
with test_raises(ValueError):
331+
@breakably_looped
332+
def result(loop): # missing `brk` parameter
333+
pass # pragma: no cover
334+
330335
# break, continue
331336
@breakably_looped_over(range(100), acc=0)
332337
def s(loop, x, acc, cnt, brk):
@@ -346,6 +351,28 @@ def s(loop, x, acc, cnt, brk):
346351
return loop(acc + x)
347352
test[s == 15]
348353

354+
# termination by running out of input elements
355+
@breakably_looped_over(range(10), acc=0)
356+
def s(loop, x, acc, cnt, brk):
357+
return loop(acc + x)
358+
test[s == 45]
359+
360+
# empty input iterable
361+
@breakably_looped_over((), acc=0)
362+
def s(loop, x, acc, cnt, brk):
363+
return loop(acc + x)
364+
test[s == 0]
365+
366+
with test_raises(ValueError):
367+
@breakably_looped_over(range(10), acc=0)
368+
def s(loop, x, acc): # missing `cnt` and `brk` parameters
369+
return loop(acc + x)
370+
371+
with test_raises(ValueError):
372+
@breakably_looped_over(range(10), acc=0)
373+
def s(loop, x, acc, cnt): # missing `brk` parameter
374+
return loop(acc + x)
375+
349376
# TODO: need some kind of benchmarking tools to do this properly.
350377
with testset("performance benchmark"):
351378
n = 100000

unpythonic/test/test_let.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ def result(*, env):
141141
test[result is False]
142142

143143
with testset("error cases"):
144+
# TODO: Python 3.6+ (up to 3.5, can't guarantee argument evaluation order)
145+
# test_raises[AttributeError,
146+
# letrec(a=lambda e: e.b + 1, # error, e.b does not exist yet (simple value refers to binding below it)
147+
# b=42,
148+
# body=lambda e: e.a)]
149+
144150
test_raises[AttributeError,
145151
let(x=0,
146152
body=lambda e: e.set('y', 3)),
@@ -151,6 +157,12 @@ def result(*, env):
151157
def error1(*, env):
152158
env.y = 2 # error, cannot introduce new bindings into a let environment
153159

160+
test_raises[TypeError, let(body="not a callable")]
161+
test_raises[TypeError, let(body=lambda: None)] # body callable must be able to take in environment
162+
# Reassigning the same name is blocked by Python itself (SyntaxError), so no test for that.
163+
test_raises[TypeError, letrec(x=lambda: 1,
164+
body=lambda e: e.x)] # callable value must be able to take in environment
165+
154166
if __name__ == '__main__': # pragma: no cover
155167
with session(__file__):
156168
runtests()

0 commit comments

Comments
 (0)