Skip to content

Commit

Permalink
Added more supported versions and a test framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Richardson committed Dec 14, 2018
1 parent fd98468 commit f1afe1e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dumper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
simple depth and by some rules for how to handle contained instances.
Copyright (c) 2009 Python Software Foundation
Copyright (c) 2014 Joshua Richardson, Chegg Inc.
Copyright (c) 2014,2018 Joshua Richardson, Chegg Inc.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'
],

# What does your project relate to?
Expand Down
83 changes: 83 additions & 0 deletions test.py
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

0 comments on commit f1afe1e

Please sign in to comment.