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 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"))