Skip to content

Commit 868c1a3

Browse files
committed
Many more tests
1 parent c8e4d67 commit 868c1a3

1 file changed

Lines changed: 69 additions & 6 deletions

File tree

tests/test_ast.py

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
T = el.Symbol('t')
88
NIL = el.Symbol('nil')
99

10-
SYMBOLS = [T, NIL] + list(map(el.Symbol, ['foo', ':foo', 'bar']))
10+
SYMBOLS = [T, NIL] + list(map(el.Symbol, ['foo', ':foo', '+']))
1111
LITERALS = list(map(el.Literal, [0, 1, -1, 0.0, 1.0, "foo", "bar", ""]))
1212
CONS = [el.Cons(el.Literal(0), el.Literal(1)), el.Cons(el.Symbol('foo'), el.Literal('bar'))]
1313
LISTS = list(map(el.List, [
@@ -16,6 +16,7 @@
1616
[el.Literal(s) for s in ['foo', 'bar', 'baz']],
1717
]))
1818
QUOTES = [el.Quote(n) for l in [SYMBOLS, LITERALS, CONS, LISTS] for n in l[:2]]
19+
RAW = [el.Raw('(+ 1 2)'), el.Raw('(message "hello")')]
1920

2021
NODES = SYMBOLS + LITERALS + CONS + LISTS
2122

@@ -66,14 +67,76 @@ def test_convert():
6667

6768
# Tuples to lists
6869
tup = ((3.14, "bar", NIL, (1, 2)))
69-
assert el.to_elisp(tup) == el.make_list(tup)
70+
assert el.to_elisp(tup) == el.List(list(map(el.to_elisp, tup)))
71+
72+
# Lists become quoted
73+
assert el.to_elisp(list(tup)) == el.Quote(el.to_elisp(tup))
74+
75+
# Mapping objects converted as alists
76+
d = {'a': 1, 'b': 'foo', 'c': True}
77+
assert el.to_elisp(d) == el.make_alist(d)
7078

7179
# Should leave existing nodes unchanged
7280
for n in NODES:
7381
assert el.to_elisp(n) == n
7482

7583

76-
def test_make_list():
77-
assert el.make_list((3.14, "bar", NIL, (1, 2))) == \
78-
el.List([el.Literal(3.14), el.Literal("bar"), NIL,
79-
el.List([el.Literal(1), el.Literal(2)])])
84+
def test_symbol_call():
85+
"""Test that calling a symbol creates a function call."""
86+
assert el.Symbol('+')(1, 2) == el.List([el.Symbol('+'), 1, 2])
87+
88+
89+
def test_str():
90+
"""Test conversion to str."""
91+
assert list(map(str, SYMBOLS)) == ['t', 'nil', 'foo', ':foo', '+']
92+
93+
assert list(map(str, LITERALS)) == ['0', '1', '-1', '0.0', '1.0', '"foo"', '"bar"', '""']
94+
95+
assert str(el.List([1, el.Symbol('a'), "b"])) == '(1 a "b")'
96+
97+
assert str(el.Cons(el.Symbol('a'), 1)) == '(cons a 1)'
98+
99+
assert str(el.Quote(el.Symbol('foo'))) == '\'foo'
100+
assert str(el.Quote(el.List([1, el.Symbol('a'), "b"]))) == '\'(1 a "b")'
101+
assert str(el.Quote(el.Cons(el.Symbol('a'), 1))) == '\'(a . 1)'
102+
103+
assert str(el.Raw('(+ 1 2)')) == '(+ 1 2)'
104+
105+
106+
def test_quote():
107+
"""Test quote() function."""
108+
sym = el.Symbol('a')
109+
assert el.quote(sym) == el.Quote(sym)
110+
assert el.quote('a') == el.Quote(sym)
111+
112+
l = el.List([1, 2, 3])
113+
assert el.quote(l) == el.Quote(l)
114+
115+
116+
def test_symbols():
117+
"""Test the symbols() function."""
118+
syms = el.symbols('a', 'b', 'c')
119+
assert syms == el.List(list(map(el.Symbol, 'abc')))
120+
assert el.symbols('a', 'b', 'c', quote=True) == el.Quote(syms)
121+
122+
123+
def test_make_alist():
124+
"""Test the make_alist() function."""
125+
d = {'a': 1, 'b': ('foo', el.Symbol('bar')), 42: True}
126+
assert el.make_alist(d) == el.List([
127+
el.Cons(el.Symbol('a'), el.Literal(1)),
128+
el.Cons(el.Symbol('b'), el.to_elisp(d['b'])),
129+
el.Cons(el.Literal(42), el.Symbol('t')),
130+
])
131+
assert el.make_alist(d, quote=True) == el.Quote(el.make_alist(d))
132+
133+
134+
def test_make_plist():
135+
"""Test the make_plist() function."""
136+
d = {'a': "foo", 'b': (1, 2, 3), 'c': True}
137+
assert el.make_plist(d) == el.List([
138+
el.Symbol('a'), el.Literal("foo"),
139+
el.Symbol('b'), el.to_elisp(d['b']),
140+
el.Symbol('c'), el.Symbol('t'),
141+
])
142+
assert el.make_plist(d, quote=True) == el.Quote(el.make_plist(d))

0 commit comments

Comments
 (0)