| 1 |
import pytest |
| 2 |
import json |
| 3 |
from hypothesis import given, strategies |
| 4 |
|
| 5 |
def is_(x, y): |
| 6 |
return type(x) is type(y) and x == y |
| 7 |
|
| 8 |
def test_no_ensure_ascii(): |
| 9 |
assert is_(json.dumps(u"\u1234", ensure_ascii=False), u'"\u1234"') |
| 10 |
assert is_(json.dumps(u"\xc0", ensure_ascii=False), u'"\xc0"') |
| 11 |
with pytest.raises(TypeError): |
| 12 |
json.dumps((u"\u1234", b"x"), ensure_ascii=False) |
| 13 |
with pytest.raises(TypeError): |
| 14 |
json.dumps((b"x", u"\u1234"), ensure_ascii=False) |
| 15 |
|
| 16 |
def test_issue2191(): |
| 17 |
assert is_(json.dumps(u"xxx", ensure_ascii=False), u'"xxx"') |
| 18 |
|
| 19 |
jsondata = strategies.recursive( |
| 20 |
strategies.none() | |
| 21 |
strategies.booleans() | |
| 22 |
strategies.floats(allow_nan=False) | |
| 23 |
strategies.text(), |
| 24 |
lambda children: strategies.lists(children) | |
| 25 |
strategies.dictionaries(strategies.text(), children)) |
| 26 |
|
| 27 |
@given(jsondata) |
| 28 |
def test_roundtrip(d): |
| 29 |
assert json.loads(json.dumps(d)) == d |
| 30 |
|
| 31 |
def test_skipkeys(): |
| 32 |
assert json.dumps({Ellipsis: 42}, skipkeys=True) == '{}' |
| 33 |
assert json.dumps({Ellipsis: 42, 3: 4}, skipkeys=True) == '{"3": 4}' |
| 34 |
assert json.dumps({3: 4, Ellipsis: 42}, skipkeys=True) == '{"3": 4}' |
| 35 |
assert json.dumps({Ellipsis: 42, NotImplemented: 43}, skipkeys=True) \ |
| 36 |
== '{}' |
| 37 |
assert json.dumps({3: 4, Ellipsis: 42, NotImplemented: 43}, skipkeys=True)\ |
| 38 |
== '{"3": 4}' |
| 39 |
assert json.dumps({Ellipsis: 42, 3: 4, NotImplemented: 43}, skipkeys=True)\ |
| 40 |
== '{"3": 4}' |
| 41 |
assert json.dumps({Ellipsis: 42, NotImplemented: 43, 3: 4}, skipkeys=True)\ |
| 42 |
== '{"3": 4}' |
| 43 |
assert json.dumps({3: 4, 5: 6, Ellipsis: 42}, skipkeys=True) \ |
| 44 |
== '{"3": 4, "5": 6}' |
| 45 |
assert json.dumps({3: 4, Ellipsis: 42, 5: 6}, skipkeys=True) \ |
| 46 |
== '{"3": 4, "5": 6}' |
| 47 |
assert json.dumps({Ellipsis: 42, 3: 4, 5: 6}, skipkeys=True) \ |
| 48 |
== '{"3": 4, "5": 6}' |
| 49 |
|
| 50 |
def test_boolean_as_dict_key(): |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
assert json.dumps({True: 5}) == '{"true": 5}' |
| 57 |
assert json.dumps({False: 5}) == '{"false": 5}' |