test hy model reprs

This commit is contained in:
gilch 2017-09-06 17:28:41 -06:00 committed by Kodi Arfer
parent 2d8a318e0c
commit b23f6e5b59

View File

@ -3,9 +3,12 @@
# license. See the LICENSE.
import copy
import hy
from clint.textui.colored import clean
from hy._compat import long_type, str_type
from hy.models import (wrap_value, replace_hy_obj, HyString, HyInteger, HyList,
HyDict, HySet, HyExpression, HyCons, HyComplex, HyFloat)
HyDict, HySet, HyExpression, HyCons, HyComplex, HyFloat,
pretty)
def test_wrap_long_type():
@ -140,3 +143,117 @@ def test_number_model_copy():
c = HyComplex(42j)
assert (c == copy.copy(c))
assert (c == copy.deepcopy(c))
PRETTY_STRINGS = {
k % ('[1.0] {1.0} (1.0) #{1.0} (0.0 1.0 . 2.0)',):
v.format("""
HyList([
HyFloat(1.0)]),
HyDict([
HyFloat(1.0) # odd
]),
HyExpression([
HyFloat(1.0)]),
HySet([
HyFloat(1.0)]),
<HyCons (
HyFloat(0.0)
HyFloat(1.0)
. HyFloat(2.0))>""")
for k, v in {'[%s]': 'HyList([{}])',
'#{%s}': 'HySet([{}])'}.items()}
PRETTY_STRINGS.update({
'{[1.0] {1.0} (1.0) #{1.0} (0.0 1.0 . 2.0)}':
"""HyDict([
HyList([
HyFloat(1.0)]),
HyDict([
HyFloat(1.0) # odd
])
,
HyExpression([
HyFloat(1.0)]),
HySet([
HyFloat(1.0)])
,
<HyCons (
HyFloat(0.0)
HyFloat(1.0)
. HyFloat(2.0))> # odd
])"""
,
'([1.0] {1.0} (1.0) #{1.0} (0.0 1.0 . 2.0) . 3.0)':
"""<HyCons (
HyList([
HyFloat(1.0)])
HyDict([
HyFloat(1.0) # odd
])
HyExpression([
HyFloat(1.0)])
HySet([
HyFloat(1.0)])
<HyCons (
HyFloat(0.0)
HyFloat(1.0)
. HyFloat(2.0))>
. HyFloat(3.0))>"""
,
'[1.0 1j [] {} () #{}]':
"""HyList([
HyFloat(1.0),
HyComplex(1j),
HyList(),
HyDict(),
HyExpression(),
HySet()])"""
,
'{{1j 2j} {1j 2j [][1j]} {[1j][] 1j 2j} {[1j][1j]}}':
"""HyDict([
HyDict([
HyComplex(1j), HyComplex(2j),]),
HyDict([
HyComplex(1j), HyComplex(2j),
HyList(),
HyList([
HyComplex(1j)])
,])
,
HyDict([
HyList([
HyComplex(1j)]),
HyList()
,
HyComplex(1j), HyComplex(2j),]),
HyDict([
HyList([
HyComplex(1j)]),
HyList([
HyComplex(1j)])
,])
,])"""})
def test_compound_model_repr():
HY_LIST_MODELS = (HyExpression, HyDict, HySet, HyList)
with pretty(False):
assert eval(repr(HyCons(1, 2))).__class__ is HyCons
assert eval(repr(HyCons(1, 2))) == HyCons(1, 2)
for model in HY_LIST_MODELS:
assert eval(repr(model())).__class__ is model
assert eval(repr(model([1, 2]))) == model([1, 2])
assert eval(repr(model([1, 2, 3]))) == model([1, 2, 3])
for k, v in PRETTY_STRINGS.items():
# `str` should be pretty, even under `pretty(False)`.
assert clean(str(hy.read_str(k))) == v
for k in PRETTY_STRINGS.keys():
assert eval(repr(hy.read_str(k))) == hy.read_str(k)
with pretty(True):
for model in HY_LIST_MODELS:
assert eval(clean(repr(model()))).__class__ is model
assert eval(clean(repr(model([1, 2])))) == model([1, 2])
assert eval(clean(repr(model([1, 2, 3])))) == model([1, 2, 3])
for k, v in PRETTY_STRINGS.items():
assert clean(repr(hy.read_str(k))) == v