-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more supported versions and a test framework.
- Loading branch information
Joshua Richardson
committed
Dec 14, 2018
1 parent
fd98468
commit f1afe1e
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from __future__ import print_function | ||
from dumper import dump, dumps, Dumper | ||
import dumper | ||
import io | ||
import sys | ||
|
||
buff = io.StringIO() | ||
dumper.default_dumper = Dumper(output=buff) | ||
|
||
# BEGIN TEST CASES | ||
|
||
def do_dump_scalars(): | ||
dump(1) | ||
dump('a') | ||
dump("foo") | ||
dump('''string | ||
with a newline''') | ||
return "1'a''foo''string\\nwith a newline'" | ||
|
||
def test_do_dump_scalars(): | ||
assert_output_as_expected(do_dump_scalars) | ||
|
||
# END TEST CASES | ||
|
||
def text_type(val): | ||
if sys.version < '3': | ||
return unicode(val) | ||
else: | ||
return str(val) | ||
|
||
def assertMatching(a, b): | ||
''' Asserts that the lines from string, a, match the lines in the string, b. | ||
a is the expected string / pattern | ||
b is the actual string | ||
We ignore leading/trailing whitespace | ||
''' | ||
a_lines = a.strip().split("\n") | ||
b_lines = b.strip().split("\n") | ||
if len(a_lines) != len(b_lines): | ||
raise AssertionError("a has " + text_type(len(a_lines)) + ", but b has " + text_type(len(b_lines)) + " lines: a={" + a + "}, b={" + b + "}") | ||
for i in range(0, len(a_lines)): | ||
assert a_lines[i] == b_lines[i] | ||
|
||
def assert_output_as_expected(func): | ||
# buffer stdout | ||
try: | ||
output = func() | ||
assertMatching(output, buff.getvalue()) | ||
finally: | ||
# reset the buffer for the next test | ||
buff.truncate(0) | ||
buff.seek(0) | ||
|
||
if __name__ == "__main__": | ||
|
||
l1 = [3, 5, 'hello'] | ||
t1 = ('uh', 'oh') | ||
l2 = ['foo', t1] | ||
d1 = {'k1': 'val1', | ||
'k2': l1, | ||
'k2': l2} | ||
|
||
print("a list: ", dumps (l1), "; a tuple: ", dumps (t1)) | ||
print("a complex list: ") | ||
dump (l2) | ||
dump (d1) | ||
print("same dict, printed from dumps(): ") | ||
print(dumps(d1)) | ||
dump (19) | ||
dump ("\nMy birth year!\n") | ||
|
||
dumper = Dumper (max_depth=1) | ||
l = ['foo', ['bar', 'baz', (1, 2, 3)]] | ||
dumper.dump (l) | ||
dumper.max_depth = 2 | ||
dumper.dump (l) | ||
l[1][2] = tuple (range (11)) | ||
dumper.dump (l) | ||
dumper.max_depth = None | ||
print(dumper.max_depth) | ||
|
||
class Foo: pass | ||
class Bar: pass |