summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2013-02-21 15:13:39 -0800
committerBob Ippolito <bob@redivi.com>2013-02-21 15:13:39 -0800
commitdb53d9a525ea7fec93a00fc17a1e094e821961ad (patch)
tree1bd80ccbcfc4b581cdea292db5e00a9c5ccf066a
parent104b40fcf6aa39d9ba7b240c3c528d1f85e86ef2 (diff)
downloadsimplejson-db53d9a525ea7fec93a00fc17a1e094e821961ad.tar.gz
simplejson.tool tests and bugfix for Python 3.x (#60)
-rw-r--r--CHANGES.txt2
-rw-r--r--simplejson/tests/__init__.py1
-rw-r--r--simplejson/tests/test_tool.py80
-rw-r--r--simplejson/tool.py24
4 files changed, 96 insertions, 11 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index aeb131e..04e22c9 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,5 +1,7 @@
Version 3.1.0 released XXXX-XX-XX
+* simplejson.tool tests and bugfix for Python 3.x
+ http://bugs.python.org/issue16549
* Improve error messages for certain kinds of truncated input
http://bugs.python.org/issue16009
* Moved JSONDecodeError to json.scanner (still available for import
diff --git a/simplejson/tests/__init__.py b/simplejson/tests/__init__.py
index da21365..61540d5 100644
--- a/simplejson/tests/__init__.py
+++ b/simplejson/tests/__init__.py
@@ -59,6 +59,7 @@ def all_tests_suite():
'simplejson.tests.test_decimal',
'simplejson.tests.test_tuple',
'simplejson.tests.test_namedtuple',
+ 'simplejson.tests.test_tool',
])
suite = additional_tests(suite)
return OptionalExtensionTestSuite([suite])
diff --git a/simplejson/tests/test_tool.py b/simplejson/tests/test_tool.py
new file mode 100644
index 0000000..f39f0ee
--- /dev/null
+++ b/simplejson/tests/test_tool.py
@@ -0,0 +1,80 @@
+import os
+import sys
+import textwrap
+import unittest
+import subprocess
+import tempfile
+
+class TestTool(unittest.TestCase):
+ data = """
+
+ [["blorpie"],[ "whoops" ] , [
+ ],\t"d-shtaeou",\r"d-nthiouh",
+ "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field"
+ :"yes"} ]
+ """
+
+ expect = textwrap.dedent("""\
+ [
+ [
+ "blorpie"
+ ],
+ [
+ "whoops"
+ ],
+ [],
+ "d-shtaeou",
+ "d-nthiouh",
+ "i-vhbjkhnth",
+ {
+ "nifty": 87
+ },
+ {
+ "field": "yes",
+ "morefield": false
+ }
+ ]
+ """)
+
+ def runTool(self, args=None, data=None):
+ argv = [sys.executable, '-m', 'simplejson.tool']
+ if args:
+ argv.extend(args)
+ proc = subprocess.Popen(argv,
+ stdin=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ stdout=subprocess.PIPE)
+ out, err = proc.communicate(data)
+ self.assertEqual(len(err), 0)
+ self.assertEqual(proc.returncode, 0)
+ return out
+
+ def test_stdin_stdout(self):
+ self.assertEqual(
+ self.runTool(data=self.data.encode()),
+ self.expect.encode())
+
+ def test_infile_stdout(self):
+ with tempfile.NamedTemporaryFile() as infile:
+ infile.write(self.data.encode())
+ infile.flush()
+ self.assertEqual(
+ self.runTool(args=[infile.name]),
+ self.expect.encode())
+
+ def test_infile_outfile(self):
+ with tempfile.NamedTemporaryFile() as infile:
+ infile.write(self.data.encode())
+ infile.flush()
+ # outfile will get overwritten by tool, so the delete
+ # may not work on some platforms. Do it manually.
+ outfile = tempfile.NamedTemporaryFile(delete=0)
+ try:
+ self.assertEqual(
+ self.runTool(args=[infile.name, outfile.name]),
+ ''.encode())
+ with open(outfile.name, 'rb') as f:
+ self.assertEqual(f.read(), self.expect.encode())
+ finally:
+ outfile.close()
+ os.unlink(outfile.name)
diff --git a/simplejson/tool.py b/simplejson/tool.py
index f00e592..35627db 100644
--- a/simplejson/tool.py
+++ b/simplejson/tool.py
@@ -18,21 +18,23 @@ def main():
infile = sys.stdin
outfile = sys.stdout
elif len(sys.argv) == 2:
- infile = open(sys.argv[1], 'rb')
+ infile = open(sys.argv[1], 'r')
outfile = sys.stdout
elif len(sys.argv) == 3:
- infile = open(sys.argv[1], 'rb')
- outfile = open(sys.argv[2], 'wb')
+ infile = open(sys.argv[1], 'r')
+ outfile = open(sys.argv[2], 'w')
else:
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
- try:
- obj = json.load(infile,
- object_pairs_hook=json.OrderedDict,
- use_decimal=True)
- except ValueError:
- raise SystemExit(sys.exc_info()[1])
- json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True)
- outfile.write('\n')
+ with infile:
+ try:
+ obj = json.load(infile,
+ object_pairs_hook=json.OrderedDict,
+ use_decimal=True)
+ except ValueError:
+ raise SystemExit(sys.exc_info()[1])
+ with outfile:
+ json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True)
+ outfile.write('\n')
if __name__ == '__main__':