-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseparation.py
62 lines (48 loc) · 1.52 KB
/
separation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
def parse_json_file(path):
"""Return the contents of path as a JSON object."""
with open(path) as infile:
text = infile.read()
return json.loads(text)
def read_file(path):
"""Return the contents of path."""
with open(path) as infile:
return infile.read()
def parse_json(text):
"""Return text parsed as a JSON object."""
return json.loads(text)
# separation does not prevent you from having convenience functions
def parse_json_read_file(path):
"""Because I want the combined function behaviour badly."""
return parse_json(read_file(path))
sample_json = """
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
"""
def test_parse_json():
frame = parse_json(sample_json)
assert frame['menu']['popup']['menuitem'][0]['value'] == 'New'
def test_read_file():
# read_file(path) is just too boring to unit test
pass
#
# here is some of the extra junk needed without separation
#
import os
import tempfile
def test_parse_json_file():
json_file = tempfile.NamedTemporaryFile(delete=False)
json_file.write(sample_json)
# and this line raises an exception and I don't want to debug it
frame = parse_json_file(json_file.name)
os.unlink(json_file.name)
assert frame['menu']['popup']['menuitem'][0]['value'] == 'New'