importing python metacircular evaluator into git
[boa.git] / tests.py
blob23fc55689e10d2999099d72b4679d268a123294c
1 import boa, os
2 from nose import with_setup
4 i = None
5 def setup():
6 global i
7 i = boa.Interpreter()
9 @with_setup(setup)
10 def test_assignment():
11 i.push('(define a "foo")')
12 v = i.push("(cons a a)")
13 a = boa.cons('foo', 'foo')
14 assert boa.eq_p(v, a), (v, a)
16 i.push('(define b 2)')
17 v = i.push('b')
18 assert boa.eq_p(v, 2), type(v)
20 @with_setup(setup)
21 def test_if():
22 v = i.push(r"""
23 (begin
24 (define a true)
25 (if a 3 4))
26 """)
27 assert boa.eq_p(v, 3), v
29 @with_setup(setup)
30 def test_procedures():
31 v = i.push(r"""
32 (begin
33 (define (foo x y)
34 (+ x
35 (+ y x)))
36 (foo 4 5))
37 """)
38 assert boa.eq_p(v, 13)
40 v = i.push(r"""
41 (begin
42 (define (bar x)
43 (+ x
44 ((lambda (a) (+ a a)) x)))
45 (bar 2))
46 """)
47 assert boa.eq_p(v, 6), v
49 @with_setup(setup)
50 def test_closure():
51 v = i.push(r"""
52 (begin
53 (define (counter n)
54 (define (inc)
55 (set! n (+ n 1))
57 inc)
58 (define a1 (counter 3))
59 (define a2 (counter 5))
60 (a1)
61 (a2)
62 (a1)
63 (a1)
64 (a2)
65 (cons (a1) (a2)))
66 """)
67 assert boa.eq_p(v, boa.cons(7, 8))
69 @with_setup(setup)
70 def test_import():
71 v = i.push(r"""
72 (define os (import "os"))
73 ((. os "getlogin"))
74 """)
75 assert boa.eq_p(v, os.getlogin()), v
77 def test_import_hook():
78 boa.install()
79 import foo
80 assert foo.bar == 3
82 @with_setup(setup)
83 def test_macro():
84 v = i.push(r"""
85 (define-syntax when
86 (syntax-rules ()
87 ((when c e) (if c e))))
89 (define a true)
90 (when a (+ 3 4))
91 """)
92 assert boa.eq_p(v, 7), v