def test_dotted_list(): """Check that dotted lists get tokenized correctly""" entry = tokenize("(a b c . (d . e))")[0] assert entry == HyCons( HySymbol("a"), HyCons(HySymbol("b"), HyCons(HySymbol("c"), HyCons(HySymbol("d"), HySymbol("e")))))
def test_cons_replacing(): """Check that assigning to a cons works as expected""" cons = HyCons("foo", "bar") cons[0] = "car" assert cons == HyCons("car", "bar") cons[1:] = "cdr" assert cons == HyCons("car", "cdr") try: cons[:] = "foo" assert True is False except IndexError: pass
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
def paren(p): cont = p[1] # Dotted lists are expressions of the form # (a b c . d) # that evaluate to nested cons cells of the form # (a . (b . (c . d))) if len(cont) >= 3 and isinstance(cont[-2], HySymbol) and cont[-2] == ".": reject_spurious_dots(cont[:-2], cont[-1:]) if len(cont) == 3: # Two-item dotted list: return the cons cell directly return HyCons(cont[0], cont[2]) else: # Return a nested cons cell return HyCons(cont[0], paren([p[0], cont[1:], p[2]])) # Warn preemptively on a malformed dotted list. # Only check for dots after the first item to allow for a potential # attribute accessor shorthand reject_spurious_dots(cont[1:]) return HyExpression(p[1])
def test_cons_slicing(): """Check that cons slicing works as expected""" cons = HyCons("car", "cdr") assert cons[0] == "car" assert cons[1:] == "cdr" try: cons[:] assert True is False except IndexError: pass try: cons[1] assert True is False except IndexError: pass
def test_simple_cons(): """Check that cons gets tokenized correctly""" entry = tokenize("(a . b)")[0] assert entry == HyCons(HySymbol("a"), HySymbol("b"))