Skip to content

Commit 96fd7af

Browse files
authored
Clean up the test suite (#596)
* Moved `ns` and `lcompile` fixtures into `tests/basilisp/conftest.py` so they can be used across the entire test suite. Updated all of the tests using similar fixtures to use these common fixtures instead. * This fixes a potential issue that can happen when Basilisp tests are run by filenames (as they are on CircleCI). If certain improperly isolated tests run before `tests/basilisp/prompt_test.py`, the current Namespace when that test is run may potentially have new `Vars` which cause the completion tests to fail with those additional Var names. * Moved `core_ns` fixtures into `tests/basilisp/conftest.py` so they can be used by all tests. * Parameterized all of the `tests/basilisp/lrepr_test.py` tests. * Moved remaining `basilisp.string` tests from `tests/basilisp/string_test.py` into `tests/basilisp/test_string.py` so all the tests are in one location and written in Basilisp. * Collapse `tests/basilisp/test_munge.py` into `tests/basilisp/test_langutil.py` (where `demunge` tests already lived alone). * Collapse `tests/basilisp/maybe_test.py` into `tests/basilisp/util_test.py` (where other `basilisp.util` tests already lived).
1 parent 8074178 commit 96fd7af

14 files changed

+624
-662
lines changed

tests/basilisp/compiler_test.py

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import uuid
99
from fractions import Fraction
1010
from tempfile import TemporaryDirectory
11-
from typing import Any, Callable, Dict, Optional
1211
from unittest.mock import Mock
1312

1413
import dateutil.parser as dateparser
@@ -28,9 +27,7 @@
2827
from basilisp.lang.interfaces import IType, IWithMeta
2928
from basilisp.lang.runtime import Var
3029
from basilisp.lang.util import demunge
31-
from tests.basilisp.helpers import get_or_create_ns
32-
33-
COMPILER_FILE_PATH = "compiler_test"
30+
from tests.basilisp.helpers import CompileFn, get_or_create_ns
3431

3532

3633
@pytest.fixture(scope="module", autouse=True)
@@ -49,18 +46,8 @@ def test_ns() -> str:
4946

5047

5148
@pytest.fixture
52-
def test_ns_sym(test_ns: str) -> sym.Symbol:
53-
return sym.symbol(test_ns)
54-
55-
56-
@pytest.fixture
57-
def ns(test_ns: str, test_ns_sym: sym.Symbol) -> runtime.Namespace:
58-
get_or_create_ns(test_ns_sym)
59-
with runtime.ns_bindings(test_ns) as ns:
60-
try:
61-
yield ns
62-
finally:
63-
runtime.Namespace.remove(test_ns_sym)
49+
def compiler_file_path() -> str:
50+
return "compiler_test"
6451

6552

6653
@pytest.fixture
@@ -81,30 +68,6 @@ def async_to_sync(asyncf, *args, **kwargs):
8168
return loop.run_until_complete(asyncf(*args, **kwargs))
8269

8370

84-
CompileFn = Callable[[str], Any]
85-
86-
87-
@pytest.fixture
88-
def lcompile(ns: runtime.Namespace) -> CompileFn:
89-
def _lcompile(
90-
s: str,
91-
resolver: Optional[reader.Resolver] = None,
92-
opts: Optional[Dict[str, bool]] = None,
93-
):
94-
"""Compile and execute the code in the input string.
95-
96-
Return the resulting expression."""
97-
ctx = compiler.CompilerContext(COMPILER_FILE_PATH, opts=opts)
98-
99-
last = None
100-
for form in reader.read_str(s, resolver=resolver):
101-
last = compiler.compile_and_exec_form(form, ctx, ns)
102-
103-
return last
104-
105-
return _lcompile
106-
107-
10871
class TestLiterals:
10972
def test_nil(self, lcompile: CompileFn):
11073
assert None is lcompile("nil")
@@ -309,14 +272,16 @@ def test_def_docstring_is_string(self, lcompile: CompileFn, ns: runtime.Namespac
309272
with pytest.raises(compiler.CompilerException):
310273
lcompile("(def a :not-a-docstring :a)")
311274

312-
def test_compiler_metadata(self, lcompile: CompileFn, ns: runtime.Namespace):
275+
def test_compiler_metadata(
276+
self, lcompile: CompileFn, ns: runtime.Namespace, compiler_file_path: str
277+
):
313278
lcompile('(def ^{:doc "Super cool docstring"} unique-oeuene :a)')
314279

315280
var = ns.find(sym.symbol("unique-oeuene"))
316281
meta = var.meta
317282

318283
assert 1 == meta.val_at(kw.keyword("line"))
319-
assert COMPILER_FILE_PATH == meta.val_at(kw.keyword("file"))
284+
assert compiler_file_path == meta.val_at(kw.keyword("file"))
320285
assert 1 == meta.val_at(kw.keyword("col"))
321286
assert sym.symbol("unique-oeuene") == meta.val_at(kw.keyword("name"))
322287
assert ns == meta.val_at(kw.keyword("ns"))
@@ -3323,7 +3288,7 @@ def test_warning_for_nested_let_if_warning_enabled(
33233288
) in caplog.record_tuples
33243289

33253290
def test_no_warning_for_nested_let_if_warning_disabled(
3326-
self, lcompile: CompileFn, caplog
3291+
self, lcompile: CompileFn, ns: runtime.Namespace, caplog
33273292
):
33283293
lcompile(
33293294
"""

tests/basilisp/conftest.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,61 @@
11
import sys
2+
from typing import Dict, Optional
23

34
import pytest
45

6+
import basilisp.lang.compiler as compiler
7+
import basilisp.lang.reader as reader
8+
import basilisp.lang.runtime as runtime
9+
import basilisp.lang.symbol as sym
10+
from tests.basilisp.helpers import CompileFn, get_or_create_ns
11+
512

613
@pytest.fixture(params=[3, 4] if sys.version_info < (3, 8) else [3, 4, 5])
714
def pickle_protocol(request) -> int:
815
return request.param
16+
17+
18+
@pytest.fixture
19+
def core_ns_sym() -> sym.Symbol:
20+
return runtime.CORE_NS_SYM
21+
22+
23+
@pytest.fixture
24+
def core_ns(core_ns_sym: sym.Symbol) -> runtime.Namespace:
25+
return get_or_create_ns(core_ns_sym)
26+
27+
28+
@pytest.fixture
29+
def test_ns_sym(test_ns: str) -> sym.Symbol:
30+
return sym.symbol(test_ns)
31+
32+
33+
@pytest.fixture
34+
def ns(test_ns: str, test_ns_sym: sym.Symbol) -> runtime.Namespace:
35+
get_or_create_ns(test_ns_sym)
36+
with runtime.ns_bindings(test_ns) as ns:
37+
try:
38+
yield ns
39+
finally:
40+
runtime.Namespace.remove(test_ns_sym)
41+
42+
43+
@pytest.fixture
44+
def lcompile(ns: runtime.Namespace, compiler_file_path: str) -> CompileFn:
45+
def _lcompile(
46+
s: str,
47+
resolver: Optional[reader.Resolver] = None,
48+
opts: Optional[Dict[str, bool]] = None,
49+
):
50+
"""Compile and execute the code in the input string.
51+
52+
Return the resulting expression."""
53+
ctx = compiler.CompilerContext(compiler_file_path, opts=opts)
54+
55+
last = None
56+
for form in reader.read_str(s, resolver=resolver):
57+
last = compiler.compile_and_exec_form(form, ctx, ns)
58+
59+
return last
60+
61+
return _lcompile

tests/basilisp/helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from typing import Tuple
1+
from typing import Any, Callable, Tuple
22

33
import basilisp.lang.symbol as sym
44
from basilisp.lang.runtime import CORE_NS_SYM, Namespace
55

6+
CompileFn = Callable[[str], Any]
7+
68

79
def get_or_create_ns(
810
name: sym.Symbol, refer: Tuple[sym.Symbol] = (CORE_NS_SYM,)

tests/basilisp/langutil_test.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
from basilisp.lang.util import _MUNGE_REPLACEMENTS, demunge
1+
import builtins
2+
import keyword
3+
4+
import pytest
5+
6+
from basilisp.lang.util import _MUNGE_REPLACEMENTS, demunge, munge
27

38

49
def test_demunge():
@@ -10,3 +15,33 @@ def test_demunge():
1015
assert "random--V--" == demunge("random__V__")
1116
assert "hi-how-are-you?" == demunge("hi_how_are_you__Q__")
1217
assert "hi-how-are-you----" == demunge("hi_how_are_you____")
18+
19+
20+
@pytest.mark.parametrize(
21+
"expected,input",
22+
[
23+
("__PLUS__", "+"),
24+
("_", "-"),
25+
("__STAR__ns__STAR__", "*ns*"),
26+
("__DIV__", "/"),
27+
("__LT__", "<"),
28+
("__GT__", ">"),
29+
("send__BANG__", "send!"),
30+
("__EQ__", "="),
31+
("string__Q__", "string?"),
32+
("__IDIV__", "\\"),
33+
("__AMP__form", "&form"),
34+
],
35+
)
36+
def test_munge_disallows_syms(expected, input):
37+
assert expected == munge(input)
38+
39+
40+
def test_munge_disallows_python_builtins():
41+
for name in builtins.__dict__.keys():
42+
assert f"{name}_" == munge(name)
43+
44+
45+
def test_munge_disallows_python_kws():
46+
for kw in keyword.kwlist:
47+
assert f"{kw}_" == munge(kw)

0 commit comments

Comments
 (0)