Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
linePairs = []
for line in lines:
line = lineAfterContext(line, prefix)
if not line:
linePairs.append([])
continue
pairStrs = line.split(TEST_PAIR_DELIMITER)
pairs = [tuple(s.split(':', 1)) for s in pairStrs]
# Indented line of a multiline value.
if len(pairs[0]) == 1 and line.startswith(' '):
arg, value = linePairs[-1][-1]
looksLikeAString = value[0] in ["'", '"']
prefix = (arg + ': ') + (' ' if looksLikeAString else '')
dedented = line[len(ic.prefix) + len(prefix):]
linePairs[-1][-1] = (arg, value + '\n' + dedented)
else:
items = [
(p[0].strip(), None) if len(p) == 1 # A value, like ic(3).
else (p[0].strip(), p[1].strip()) # A variable, like ic(a).
for p in pairs]
linePairs.append(items)
return linePairs
def testMultipleArguments(self):
with disableColoring(), captureStandardStreams() as (out, err):
ic(a, b)
pairs = parseOutputIntoPairs(out, err, 1)[0]
assert pairs == [('a', '1'), ('b', '2')]
def testMultilineValueWrapped(self):
# Multiline values are line wrapped.
multilineStr = 'line1\nline2'
with disableColoring(), captureStandardStreams() as (out, err):
ic(multilineStr)
pair = parseOutputIntoPairs(out, err, 2)[0][0]
assert pair == ('multilineStr', ic.argToStringFunction(multilineStr))
def testExpressionArguments(self):
class klass():
attr = 'yep'
d = {'d': {1: 'one'}, 'k': klass}
with disableColoring(), captureStandardStreams() as (out, err):
ic(d['d'][1])
pair = parseOutputIntoPairs(out, err, 1)[0][0]
assert pair == ("d['d'][1]", "'one'")
with disableColoring(), captureStandardStreams() as (out, err):
ic(d['k'].attr)
pair = parseOutputIntoPairs(out, err, 1)[0][0]
assert pair == ("d['k'].attr", "'yep'")
def testReturnValue(self):
with disableColoring(), captureStandardStreams() as (out, err):
assert ic() is None
assert ic(1) == 1
assert ic(1, 2, 3) == (1, 2, 3)
def testIncludeContextMultiLine(self):
multilineStr = 'line1\nline2'
with configureIcecreamOutput(includeContext=True):
with disableColoring(), captureStandardStreams() as (out, err):
ic(multilineStr)
firstLine = err.getvalue().splitlines()[0]
assert lineIsContext(firstLine)
pair = parseOutputIntoPairs(out, err, 3)[1][0]
assert pair == ('multilineStr', ic.argToStringFunction(multilineStr))
def testOutputFunction(self):
lst = []
def appendTo(s):
lst.append(s)
with configureIcecreamOutput(ic.prefix, appendTo):
with captureStandardStreams() as (out, err):
ic(a)
assert not out.getvalue() and not err.getvalue()
with configureIcecreamOutput(outputFunction=appendTo):
with captureStandardStreams() as (out, err):
ic(b)
assert not out.getvalue() and not err.getvalue()
pairs = parseOutputIntoPairs(out, '\n'.join(lst), 2)
assert pairs == [[('a', '1')], [('b', '2')]]
# A single long line with multiple variables is line wrapped.
val = '*' * int(ic.lineWrapWidth / 4)
valStr = ic.argToStringFunction(val)
v1 = v2 = v3 = v4 = val
with disableColoring(), captureStandardStreams() as (out, err):
ic(v1, v2, v3, v4)
pairs = parseOutputIntoPairs(out, err, 4)
assert pairs == [[(k, valStr)] for k in ['v1', 'v2', 'v3', 'v4']]
lines = err.getvalue().splitlines()
assert (
lines[0].startswith(ic.prefix) and
lines[1].startswith(' ' * len(ic.prefix)) and
lines[2].startswith(' ' * len(ic.prefix)) and
lines[3].startswith(' ' * len(ic.prefix)))
def testIncludeContextSingleLine(self):
i = 3
with configureIcecreamOutput(includeContext=True):
with disableColoring(), captureStandardStreams() as (out, err):
ic(i)
pair = parseOutputIntoPairs(out, err, 1)[0][0]
assert pair == ('i', '3')
def testPrefixConfiguration(self):
prefix = 'lolsup '
with configureIcecreamOutput(prefix, stderrPrint):
with disableColoring(), captureStandardStreams() as (out, err):
ic(a)
pair = parseOutputIntoPairs(out, err, 1, prefix=prefix)[0][0]
assert pair == ('a', '1')
def prefixFunction():
return 'lolsup '
with configureIcecreamOutput(prefix=prefixFunction):
with disableColoring(), captureStandardStreams() as (out, err):
ic(b)
pair = parseOutputIntoPairs(out, err, 1, prefix=prefixFunction())[0][0]
assert pair == ('b', '2')