-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
executable file
·65 lines (43 loc) · 1.31 KB
/
test.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
63
64
65
#!/usr/bin/env pytest
# SPDX-License-Identifier: WTFPL
import datetime
from decimal import Decimal
import json
import os
import subprocess
import types
import pytest
os.chdir(os.path.dirname(__file__))
def run(input):
return json.loads(subprocess.check_output(
["./pyliteral-to-json"],
encoding="utf8",
input=input
))
def test_basic():
assert run("""{0o1: u'2', 0x3: None,}""") == {"1": "2", "3": None}
def test_error():
with pytest.raises(subprocess.CalledProcessError):
run("[1+1]")
with pytest.raises(subprocess.CalledProcessError):
run("[id(1)]")
with pytest.raises(subprocess.CalledProcessError):
run("[datetime(1+1)]")
def test_datetime():
assert run("""[datetime(2013, 4, 5)]""") == ["2013-04-05 00:00:00"]
def test_decimal():
assert run("""[Decimal('1')]""") == ["1"]
def test_set():
assert run(repr(set())) == []
assert run(repr(frozenset())) == []
assert run(repr(frozenset({1}))) == [1]
assert run(repr({1})) == [1]
assert run(repr({datetime.datetime(2013, 4, 5)})) == ["2013-04-05 00:00:00"]
assert run(repr({Decimal("1")})) == ["1"]
def test_tuple():
assert run(repr(())) == []
assert run(repr((1,))) == [1]
assert run(repr((1, 2))) == [1, 2]
assert run(repr(((),))) == [[]]
def test_namespace():
assert run(repr(types.SimpleNamespace(a=1, b=["2"]))) == {"a": 1, "b": ["2"]}