Skip to content

Commit 67c3f46

Browse files
committed
ch18: added std_env fixture to lis.py tests
1 parent 0f9c297 commit 67c3f46

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

18-context-mngr/lispy/py3.10/lis_test.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Optional
22

3-
from pytest import mark
3+
from pytest import mark, fixture
44

55
from lis import parse, evaluate, Expression, Environment, standard_env
66

@@ -39,12 +39,15 @@
3939
("((repeat riff-shuffle) (list 1 2 3 4 5 6 7 8))", [1, 3, 5, 7, 2, 4, 6, 8]),
4040
("(riff-shuffle (riff-shuffle (riff-shuffle (list 1 2 3 4 5 6 7 8))))", [1,2,3,4,5,6,7,8]),
4141
])
42-
@mark.skip
4342
def test_evaluate(source: str, expected: Optional[Expression]) -> None:
4443
got = evaluate(parse(source))
4544
assert got == expected
4645

4746

47+
@fixture
48+
def std_env() -> Environment:
49+
return standard_env()
50+
4851
# tests for each of the cases in evaluate
4952

5053
def test_evaluate_variable() -> None:
@@ -83,65 +86,58 @@ def test_evaluate_if_false() -> None:
8386
assert got == expected
8487

8588

86-
def test_define() -> None:
87-
env: Environment = standard_env()
89+
def test_define(std_env: Environment) -> None:
8890
source = '(define answer (* 6 7))'
89-
got = evaluate(parse(source), env)
91+
got = evaluate(parse(source), std_env)
9092
assert got is None
91-
assert env['answer'] == 42
93+
assert std_env['answer'] == 42
9294

9395

94-
def test_lambda() -> None:
95-
env: Environment = standard_env()
96+
def test_lambda(std_env: Environment) -> None:
9697
source = '(lambda (a b) (if (>= a b) a b))'
97-
func = evaluate(parse(source), env)
98+
func = evaluate(parse(source), std_env)
9899
assert func.parms == ['a', 'b']
99100
assert func.body == ['if', ['>=', 'a', 'b'], 'a', 'b']
100-
assert func.env is env
101+
assert func.env is std_env
101102
assert func(1, 2) == 2
102103
assert func(3, 2) == 3
103104

104105

105-
def test_begin() -> None:
106-
env: Environment = standard_env()
106+
def test_begin(std_env: Environment) -> None:
107107
source = """
108108
(begin
109109
(define x (* 2 3))
110110
(* x 7)
111111
)
112112
"""
113-
got = evaluate(parse(source), env)
113+
got = evaluate(parse(source), std_env)
114114
assert got == 42
115115

116116

117-
def test_invocation_builtin_car() -> None:
118-
env: Environment = standard_env()
117+
def test_invocation_builtin_car(std_env: Environment) -> None:
119118
source = '(car (quote (11 22 33)))'
120-
got = evaluate(parse(source), env)
119+
got = evaluate(parse(source), std_env)
121120
assert got == 11
122121

123122

124-
def test_invocation_builtin_append() -> None:
125-
env: Environment = standard_env()
123+
def test_invocation_builtin_append(std_env: Environment) -> None:
126124
source = '(append (quote (a b)) (quote (c d)))'
127-
got = evaluate(parse(source), env)
125+
got = evaluate(parse(source), std_env)
128126
assert got == ['a', 'b', 'c', 'd']
129127

130128

131-
def test_invocation_builtin_map() -> None:
132-
env: Environment = standard_env()
129+
def test_invocation_builtin_map(std_env: Environment) -> None:
133130
source = '(map (lambda (x) (* x 2)) (quote (1 2 3))))'
134-
got = evaluate(parse(source), env)
131+
got = evaluate(parse(source), std_env)
135132
assert got == [2, 4, 6]
136133

137134

138-
def test_invocation_user_procedure() -> None:
139-
env: Environment = standard_env()
135+
def test_invocation_user_procedure(std_env: Environment) -> None:
140136
source = """
141137
(begin
142138
(define max (lambda (a b) (if (>= a b) a b)))
143139
(max 22 11)
144140
)
145141
"""
146-
got = evaluate(parse(source), env)
142+
got = evaluate(parse(source), std_env)
147143
assert got == 22

18-context-mngr/lispy/py3.9/lis_test.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Optional
22

3-
from pytest import mark
3+
from pytest import mark, fixture
44

55
from lis import parse, evaluate, Expression, Environment, standard_env
66

@@ -39,12 +39,15 @@
3939
("((repeat riff-shuffle) (list 1 2 3 4 5 6 7 8))", [1, 3, 5, 7, 2, 4, 6, 8]),
4040
("(riff-shuffle (riff-shuffle (riff-shuffle (list 1 2 3 4 5 6 7 8))))", [1,2,3,4,5,6,7,8]),
4141
])
42-
@mark.skip
4342
def test_evaluate(source: str, expected: Optional[Expression]) -> None:
4443
got = evaluate(parse(source))
4544
assert got == expected
4645

4746

47+
@fixture
48+
def std_env() -> Environment:
49+
return standard_env()
50+
4851
# tests for each of the cases in evaluate
4952

5053
def test_evaluate_variable() -> None:
@@ -83,65 +86,58 @@ def test_evaluate_if_false() -> None:
8386
assert got == expected
8487

8588

86-
def test_define() -> None:
87-
env: Environment = standard_env()
89+
def test_define(std_env: Environment) -> None:
8890
source = '(define answer (* 6 7))'
89-
got = evaluate(parse(source), env)
91+
got = evaluate(parse(source), std_env)
9092
assert got is None
91-
assert env['answer'] == 42
93+
assert std_env['answer'] == 42
9294

9395

94-
def test_lambda() -> None:
95-
env: Environment = standard_env()
96+
def test_lambda(std_env: Environment) -> None:
9697
source = '(lambda (a b) (if (>= a b) a b))'
97-
func = evaluate(parse(source), env)
98+
func = evaluate(parse(source), std_env)
9899
assert func.parms == ['a', 'b']
99100
assert func.body == ['if', ['>=', 'a', 'b'], 'a', 'b']
100-
assert func.env is env
101+
assert func.env is std_env
101102
assert func(1, 2) == 2
102103
assert func(3, 2) == 3
103104

104105

105-
def test_begin() -> None:
106-
env: Environment = standard_env()
106+
def test_begin(std_env: Environment) -> None:
107107
source = """
108108
(begin
109109
(define x (* 2 3))
110110
(* x 7)
111111
)
112112
"""
113-
got = evaluate(parse(source), env)
113+
got = evaluate(parse(source), std_env)
114114
assert got == 42
115115

116116

117-
def test_invocation_builtin_car() -> None:
118-
env: Environment = standard_env()
117+
def test_invocation_builtin_car(std_env: Environment) -> None:
119118
source = '(car (quote (11 22 33)))'
120-
got = evaluate(parse(source), env)
119+
got = evaluate(parse(source), std_env)
121120
assert got == 11
122121

123122

124-
def test_invocation_builtin_append() -> None:
125-
env: Environment = standard_env()
123+
def test_invocation_builtin_append(std_env: Environment) -> None:
126124
source = '(append (quote (a b)) (quote (c d)))'
127-
got = evaluate(parse(source), env)
125+
got = evaluate(parse(source), std_env)
128126
assert got == ['a', 'b', 'c', 'd']
129127

130128

131-
def test_invocation_builtin_map() -> None:
132-
env: Environment = standard_env()
129+
def test_invocation_builtin_map(std_env: Environment) -> None:
133130
source = '(map (lambda (x) (* x 2)) (quote (1 2 3))))'
134-
got = evaluate(parse(source), env)
131+
got = evaluate(parse(source), std_env)
135132
assert got == [2, 4, 6]
136133

137134

138-
def test_invocation_user_procedure() -> None:
139-
env: Environment = standard_env()
135+
def test_invocation_user_procedure(std_env: Environment) -> None:
140136
source = """
141137
(begin
142138
(define max (lambda (a b) (if (>= a b) a b)))
143139
(max 22 11)
144140
)
145141
"""
146-
got = evaluate(parse(source), env)
142+
got = evaluate(parse(source), std_env)
147143
assert got == 22

0 commit comments

Comments
 (0)