Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / simplejson / tests / test_tool.py @ 545

History | View | Annotate | Download (2.79 KB)

1
from __future__ import with_statement
2
import os
3
import sys
4
import textwrap
5
import unittest
6
import subprocess
7
import tempfile
8
try:
9
    # Python 3.x
10
    from test.support import strip_python_stderr
11
except ImportError:
12
    # Python 2.6+
13
    try:
14
        from test.test_support import strip_python_stderr
15
    except ImportError:
16
        # Python 2.5
17
        import re
18
        def strip_python_stderr(stderr):
19
            return re.sub(
20
                r"\[\d+ refs\]\r?\n?$".encode(),
21
                "".encode(),
22
                stderr).strip()
23

    
24
class TestTool(unittest.TestCase):
25
    data = """
26

27
        [["blorpie"],[ "whoops" ] , [
28
                                 ],\t"d-shtaeou",\r"d-nthiouh",
29
        "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field"
30
            :"yes"}  ]
31
           """
32

    
33
    expect = textwrap.dedent("""\
34
    [
35
        [
36
            "blorpie"
37
        ],
38
        [
39
            "whoops"
40
        ],
41
        [],
42
        "d-shtaeou",
43
        "d-nthiouh",
44
        "i-vhbjkhnth",
45
        {
46
            "nifty": 87
47
        },
48
        {
49
            "field": "yes",
50
            "morefield": false
51
        }
52
    ]
53
    """)
54

    
55
    def runTool(self, args=None, data=None):
56
        argv = [sys.executable, '-m', 'simplejson.tool']
57
        if args:
58
            argv.extend(args)
59
        proc = subprocess.Popen(argv,
60
                                stdin=subprocess.PIPE,
61
                                stderr=subprocess.PIPE,
62
                                stdout=subprocess.PIPE)
63
        out, err = proc.communicate(data)
64
        self.assertEqual(strip_python_stderr(err), ''.encode())
65
        self.assertEqual(proc.returncode, 0)
66
        return out
67

    
68
    def test_stdin_stdout(self):
69
        self.assertEqual(
70
            self.runTool(data=self.data.encode()),
71
            self.expect.encode())
72

    
73
    def test_infile_stdout(self):
74
        with tempfile.NamedTemporaryFile() as infile:
75
            infile.write(self.data.encode())
76
            infile.flush()
77
            self.assertEqual(
78
                self.runTool(args=[infile.name]),
79
                self.expect.encode())
80

    
81
    def test_infile_outfile(self):
82
        with tempfile.NamedTemporaryFile() as infile:
83
            infile.write(self.data.encode())
84
            infile.flush()
85
            # outfile will get overwritten by tool, so the delete
86
            # may not work on some platforms. Do it manually.
87
            outfile = tempfile.NamedTemporaryFile()
88
            try:
89
                self.assertEqual(
90
                    self.runTool(args=[infile.name, outfile.name]),
91
                    ''.encode())
92
                with open(outfile.name, 'rb') as f:
93
                    self.assertEqual(f.read(), self.expect.encode())
94
            finally:
95
                outfile.close()
96
                if os.path.exists(outfile.name):
97
                    os.unlink(outfile.name)