-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_preimports_json.py
More file actions
51 lines (42 loc) · 1.31 KB
/
test_preimports_json.py
File metadata and controls
51 lines (42 loc) · 1.31 KB
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
# -*- coding: UTF-8 -*-
"""Preimports string assets' tests.
"""
from tinyscript.preimports import json
from utils import *
FNAME = ".test.json"
TEST_JSON = """
# test long comment 1
# with another line
{
"test": ["a", "b", "c"],
"other": 1 # test comment 2
}
"""
class TestPreimportsJson(TestCase):
@classmethod
def tearDownClass(cls):
remove(FNAME)
def test_commented_json_dumping(self):
with open(FNAME, 'wt') as f:
f.write(TEST_JSON)
with open(FNAME) as f:
d = json.loadc(f)
d['another'] = True
with open(FNAME, 'wb') as f:
json.dumpc(d, f)
with open(FNAME) as f:
content = f.read()
self.assertIn(" # test long comment 1", content)
self.assertIn(" # with another line", content)
self.assertIn(" # test comment 2", content)
def test_commented_json_loading(self):
with open(FNAME, 'wt') as f:
f.write(TEST_JSON)
with open(FNAME) as f:
self.assertIsNotNone(json.loadc(f))
with open(FNAME, 'wb') as f:
f.write(TEST_JSON.encode())
with open(FNAME, 'rb') as f:
d = json.loadc(f)
self.assertIn('test', d)
self.assertIn('other', d)