예제 #1
0
def test_refer_all(ns_cache: atom.Atom[NamespaceMap]):
    ns1 = get_or_create_ns(sym.symbol("ns1"))

    var_sym1, var_val1 = sym.symbol("useful-value"), "cool string"
    var1 = Var(ns1, var_sym1)
    var1.set_value(var_val1)
    ns1.intern(var_sym1, var1)

    var_sym2, var_val2 = sym.symbol("private-value"), "private string"
    var2 = Var(ns1, var_sym2, meta=lmap.map({kw.keyword("private"): True}))
    var2.set_value(var_val2)
    ns1.intern(var_sym2, var2)

    var_sym3, var_val3 = sym.symbol("existing-value"), "interned string"
    var3 = Var(ns1, var_sym3)
    var3.set_value(var_val3)
    ns1.intern(var_sym3, var3)

    ns2 = get_or_create_ns(sym.symbol("ns2"))
    var_val4 = "some other value"
    var4 = Var(ns2, var_sym3)
    var4.set_value(var_val4)
    ns2.intern(var_sym3, var4)
    ns2.refer_all(ns1)

    assert var1 is ns2.get_refer(var_sym1)
    assert var1 is ns2.find(var_sym1)
    assert var_val1 == ns2.find(var_sym1).value

    assert None is ns2.get_refer(var_sym2)
    assert None is ns2.find(var_sym2)

    assert var3 is ns2.get_refer(var_sym3)
    assert var4 is ns2.find(var_sym3)
    assert var_val4 == ns2.find(var_sym3).value
예제 #2
0
def test_demunged_import(testdir: Testdir):
    with TemporaryDirectory() as tmpdir:
        tmp_module = os.path.join(
            tmpdir,
            "long__AMP__namespace_name__PLUS__with___LT__punctuation__GT__.lpy"
        )
        with open(tmp_module, mode="w") as module:
            code = """
            (ns long&namespace-name+with-<punctuation>)
            """
            module.write(code)

        with runtime.remove_ns_bindings():
            with patch("sys.path",
                       new=[tmpdir]), patch("sys.meta_path",
                                            new=[importer.BasilispImporter()]):
                importlib.import_module(
                    "long__AMP__namespace_name__PLUS__with___LT__punctuation__GT__"
                )

            assert (runtime.Namespace.get(
                sym.symbol("long&namespace-name+with-<punctuation>"))
                    is not None)
            assert (runtime.Namespace.get(
                sym.symbol(
                    "long__AMP__namespace_name__PLUS__with___LT__punctuation__GT__"
                )) is None)
예제 #3
0
def test_symbol_name_and_ns():
    sym = symbol("sym", ns="ns")
    assert sym.name == "sym"
    assert sym.ns == "ns"

    sym = symbol("sym")
    assert sym.name == "sym"
    assert sym.ns is None
예제 #4
0
def test_alter_atom_meta():
    a = atom.Atom(None)
    assert a.meta is None

    a.alter_meta(runtime.assoc, "type", sym.symbol("str"))
    assert a.meta == lmap.m(type=sym.symbol("str"))

    a.alter_meta(runtime.assoc, "tag", kw.keyword("async"))
    assert a.meta == lmap.m(type=sym.symbol("str"), tag=kw.keyword("async"))
예제 #5
0
def test_reset_atom_meta():
    a = atom.Atom(None)
    assert a.meta is None

    a.reset_meta(lmap.map({"type": sym.symbol("str")}))
    assert a.meta == lmap.m(type=sym.symbol("str"))

    a.reset_meta(lmap.m(tag=kw.keyword("async")))
    assert a.meta == lmap.m(tag=kw.keyword("async"))
예제 #6
0
 def test_literal_to_lisp(self):
     assert None is runtime.to_lisp(None)
     assert 1 == runtime.to_lisp(1)
     assert 1.6 == runtime.to_lisp(1.6)
     assert "string" == runtime.to_lisp("string")
     assert sym.symbol("sym") == runtime.to_lisp(sym.symbol("sym"))
     assert sym.symbol("sym", ns="ns") == runtime.to_lisp(sym.symbol("sym", ns="ns"))
     assert kw.keyword("kw") == runtime.to_lisp(kw.keyword("kw"))
     assert kw.keyword("kw", ns="ns") == runtime.to_lisp(kw.keyword("kw", ns="ns"))
예제 #7
0
def test_find_safe(
    ns_sym: sym.Symbol,
    var_name: sym.Symbol,
    intern_val,
):
    v = Var.intern(ns_sym, var_name, intern_val)
    ns_qualified_sym = sym.symbol(var_name.name, ns=ns_sym.name)
    v_in_ns = Var.find_safe(ns_qualified_sym)
    assert v == v_in_ns

    with pytest.raises(RuntimeException):
        Var.find_safe(sym.symbol("some-other-var", ns="doesnt.matter"))
예제 #8
0
def test_reset_ns_meta(
    ns_cache: atom.Atom[NamespaceMap],
    ns_sym: sym.Symbol,
):
    ns = get_or_create_ns(ns_sym)
    assert ns.meta is None

    ns.reset_meta(lmap.map({"type": sym.symbol("str")}))
    assert ns.meta == lmap.m(type=sym.symbol("str"))

    ns.reset_meta(lmap.m(tag=kw.keyword("async")))
    assert ns.meta == lmap.m(tag=kw.keyword("async"))
예제 #9
0
def test_alter_ns_meta(
    ns_cache: atom.Atom[NamespaceMap],
    ns_sym: sym.Symbol,
):
    ns = get_or_create_ns(ns_sym)
    assert ns.meta is None

    ns.alter_meta(runtime.assoc, "type", sym.symbol("str"))
    assert ns.meta == lmap.m(type=sym.symbol("str"))

    ns.alter_meta(runtime.assoc, "tag", kw.keyword("async"))
    assert ns.meta == lmap.m(type=sym.symbol("str"), tag=kw.keyword("async"))
예제 #10
0
def test_cannot_refer_private(ns_cache: atom.Atom[NamespaceMap]):
    ns1 = get_or_create_ns(sym.symbol("ns1"))
    var_sym, var_val = sym.symbol("useful-value"), "cool string"
    var = Var(ns1, var_sym, meta=lmap.map({kw.keyword("private"): True}))
    var.set_value(var_val)
    ns1.intern(var_sym, var)

    ns2 = get_or_create_ns(sym.symbol("ns2"))
    ns2.add_refer(var_sym, var)

    assert None is ns2.get_refer(var_sym)
    assert None is ns2.find(var_sym)
예제 #11
0
def test_refer(ns_cache: atom.Atom[NamespaceMap]):
    ns1 = get_or_create_ns(sym.symbol("ns1"))
    var_sym, var_val = sym.symbol("useful-value"), "cool string"
    var = Var(ns1, var_sym)
    var.set_value(var_val)
    ns1.intern(var_sym, var)

    ns2 = get_or_create_ns(sym.symbol("ns2"))
    ns2.add_refer(var_sym, var)

    assert var is ns2.get_refer(var_sym)
    assert var_val == ns2.find(var_sym).value
예제 #12
0
        def test_import_module_without_init(self, make_new_module,
                                            load_namespace):
            """Load a Basilisp namespace and a Basilisp child namespace in the
            typical Clojure fashion."""
            make_new_module("core.lpy", ns_name="core")
            make_new_module("core", "child.lpy", ns_name="core.child")

            core = load_namespace("core")
            core_child = load_namespace("core.child")

            assert "core" == core.find(sym.symbol("val")).value
            assert "core.child" == core_child.find(sym.symbol("val")).value
예제 #13
0
        def test_import_basilisp_child_with_basilisp_init(
                self, make_new_module, load_namespace):
            """Load a Basilisp package namespace setup as a Python package
            and a child Basilisp namespace of that package."""
            make_new_module("core", "__init__.lpy", ns_name="core")
            make_new_module("core", "sub.lpy", ns_name="core.sub")

            core = load_namespace("core")
            core_sub = load_namespace("core.sub")

            assert "core" == core.find(sym.symbol("val")).value
            assert "core.sub" == core_sub.find(sym.symbol("val")).value
예제 #14
0
def test_symbol_str_and_repr():
    sym = symbol("sym", ns="ns")
    assert str(sym) == "ns/sym"
    assert repr(sym) == "ns/sym"

    sym = symbol("sym", ns="some.ns")
    assert str(sym) == "some.ns/sym"
    assert repr(sym) == "some.ns/sym"

    sym = symbol("sym")
    assert str(sym) == "sym"
    assert repr(sym) == "sym"
예제 #15
0
def test_alter_var_meta(
    ns_sym: sym.Symbol,
    var_name: sym.Symbol,
    intern_val,
):
    v = Var.intern(ns_sym, var_name, intern_val)
    assert v.meta is None

    v.alter_meta(assoc, "type", sym.symbol("str"))
    assert v.meta == lmap.m(type=sym.symbol("str"))

    v.alter_meta(assoc, "tag", kw.keyword("async"))
    assert v.meta == lmap.m(type=sym.symbol("str"), tag=kw.keyword("async"))
예제 #16
0
def test_reset_var_meta(
    ns_sym: sym.Symbol,
    var_name: sym.Symbol,
    intern_val,
):
    v = Var.intern(ns_sym, var_name, intern_val)
    assert v.meta is None

    v.reset_meta(lmap.map({"type": sym.symbol("str")}))
    assert v.meta == lmap.m(type=sym.symbol("str"))

    v.reset_meta(lmap.m(tag=kw.keyword("async")))
    assert v.meta == lmap.m(tag=kw.keyword("async"))
예제 #17
0
def test_unmap(ns_cache: atom.Atom[NamespaceMap]):
    ns = get_or_create_ns(sym.symbol("ns1"))
    var_sym = sym.symbol("useful-value")

    var_val = "cool string"
    var = Var(ns, var_sym)
    var.set_value(var_val)
    ns.intern(var_sym, var)

    assert var is ns.find(var_sym)
    assert var_val == ns.find(var_sym).value

    ns.unmap(var_sym)

    assert None is ns.find(var_sym)
예제 #18
0
        def test_import_module_with_namespace_only_pkg(self, make_new_module,
                                                       load_namespace):
            """Load a Basilisp namespace and another Basilisp namespace using
            a Python namespace package."""
            make_new_module("core.lpy", ns_name="core")
            make_new_module("core",
                            "nested",
                            "child.lpy",
                            ns_name="core.nested.child")

            core = load_namespace("core")
            core_nested_child = load_namespace("core.nested.child")

            assert "core" == core.find(sym.symbol("val")).value
            assert ("core.nested.child" == core_nested_child.find(
                sym.symbol("val")).value)
예제 #19
0
    def run_result(self, pytester: Pytester) -> RunResult:
        code = """
        (ns test-testrunner
          (:require
           [basilisp.test :refer [deftest is are testing]]))

        (deftest assertion-test
          (testing "is assertions"
            (is true)
            (is false)
            (is (= "string" "string"))
            (is (thrown? basilisp.lang.exception/ExceptionInfo (throw (ex-info "Exception" {}))))
            (is (thrown? basilisp.lang.exception/ExceptionInfo (throw (python/Exception))))
            (is (throw (ex-info "Uncaught exception" {}))))

          (testing "are assertions"
            (are [exp actual] (= exp actual)
              1      1
              :hi    :hi
              "true" false
              4.6    4.6)))

        (deftest passing-test
          (is true))

        (deftest error-test
          (throw
            (ex-info "This test will count as an error." {})))
        """
        pytester.makefile(".lpy", test_testrunner=code)
        yield pytester.runpytest()
        runtime.Namespace.remove(sym.symbol("test-testrunner"))
예제 #20
0
def test_imports(ns_cache: atom.Atom[NamespaceMap]):
    ns = get_or_create_ns(sym.symbol("ns1"))
    time = __import__("time")
    ns.add_import(sym.symbol("time"), time, sym.symbol("py-time"),
                  sym.symbol("py-tm"))
    assert time == ns.get_import(sym.symbol("time"))
    assert time == ns.get_import(sym.symbol("py-time"))
    assert time == ns.get_import(sym.symbol("py-tm"))
    assert None is ns.get_import(sym.symbol("python-time"))
예제 #21
0
def bootstrap_repl(ctx: compiler.CompilerContext,
                   which_ns: str) -> types.ModuleType:
    """Bootstrap the REPL with a few useful vars and returned the bootstrapped
    module so it's functions can be used by the REPL command."""
    ns = runtime.Namespace.get_or_create(sym.symbol(which_ns))
    eval_str(f"(ns {sym.symbol(which_ns)} (:use basilisp.repl))", ctx, ns,
             object())
    return importlib.import_module(REPL_NS)
예제 #22
0
        def test_import_module_with_non_code_child(self, make_new_module,
                                                   load_namespace):
            make_new_module("core.lpy", ns_name="core")
            make_new_module("core", "resource.txt", module_text="{}")

            core = load_namespace("core")

            assert "core" == core.find(sym.symbol("val")).value
예제 #23
0
def test_find(
    ns_sym: sym.Symbol,
    var_name: sym.Symbol,
    intern_val,
):
    v = Var.intern(ns_sym, var_name, intern_val)
    ns_qualified_sym = sym.symbol(var_name.name, ns=ns_sym.name)
    v_in_ns = Var.find(ns_qualified_sym)
    assert v == v_in_ns
예제 #24
0
def test_refer_does_not_shadow_intern(ns_cache: atom.Atom[NamespaceMap]):
    ns1 = get_or_create_ns(sym.symbol("ns1"))
    var_sym = sym.symbol("useful-value")

    var_val1 = "cool string"
    var1 = Var(ns1, var_sym)
    var1.set_value(var_val1)
    ns1.intern(var_sym, var1)

    ns2 = get_or_create_ns(sym.symbol("ns2"))
    var_val2 = "lame string"
    var2 = Var(ns1, var_sym)
    var2.set_value(var_val2)
    ns2.intern(var_sym, var2)

    ns2.add_refer(var_sym, var1)

    assert var1 is ns2.get_refer(var_sym)
    assert var_val2 == ns2.find(var_sym).value
예제 #25
0
def test_symbol_with_meta():
    sym = symbol("sym")
    assert sym.meta is None

    meta1 = lmap.m(type=symbol("str"))
    sym1 = symbol("sym", meta=meta1)
    assert sym1.meta == meta1

    meta2 = lmap.m(tag=keyword("async"))
    sym2 = sym1.with_meta(meta2)
    assert sym1 is not sym2
    assert sym1 == sym2
    assert sym2.meta == lmap.m(tag=keyword("async"))

    meta3 = lmap.m(tag=keyword("macro"))
    sym3 = sym2.with_meta(meta3)
    assert sym2 is not sym3
    assert sym2 == sym3
    assert sym3.meta == lmap.m(tag=keyword("macro"))
예제 #26
0
    def test_import_module_with_invalid_cache_magic_number(
            self, module_dir, cached_module_ns, cached_module_file,
            load_namespace):
        cache_filename = importer._cache_from_source(
            os.path.join(module_dir, cached_module_file))
        with open(cache_filename, mode="r+b") as f:
            f.seek(0)
            f.write(b"1999")

        using_cache = load_namespace(cached_module_ns)
        assert cached_module_ns == using_cache.find(sym.symbol("val")).value
예제 #27
0
def test_multi_function():
    def dispatch(v) -> kw.Keyword:
        if v == "i":
            return kw.keyword("a")
        elif v == "ii":
            return kw.keyword("b")
        return kw.keyword("default")

    def fn_a(v) -> str:
        return "1"

    def fn_b(v) -> str:
        return "2"

    def fn_default(v) -> str:
        return "BLAH"

    f = multifn.MultiFunction(sym.symbol("test-fn"), dispatch,
                              kw.keyword("default"))
    f.add_method(kw.keyword("a"), fn_a)
    f.add_method(kw.keyword("b"), fn_b)
    f.add_method(kw.keyword("default"), fn_default)

    assert (lmap.map({
        kw.keyword("a"): fn_a,
        kw.keyword("b"): fn_b,
        kw.keyword("default"): fn_default,
    }) == f.methods)

    assert kw.keyword("default") == f.default

    assert fn_a is f.get_method(kw.keyword("a"))
    assert fn_b is f.get_method(kw.keyword("b"))
    assert fn_default is f.get_method(kw.keyword("default"))
    assert fn_default is f.get_method(kw.keyword("other"))

    assert "1" == f("i")
    assert "2" == f("ii")
    assert "BLAH" == f("iii")
    assert "BLAH" == f("whatever")

    f.remove_method(kw.keyword("b"))

    assert "1" == f("i")
    assert "BLAH" == f("ii")
    assert "BLAH" == f("iii")
    assert "BLAH" == f("whatever")

    f.remove_all_methods()

    assert lmap.PersistentMap.empty() == f.methods

    with pytest.raises(NotImplementedError):
        f("blah")
예제 #28
0
        def test_import_basilisp_and_python_module_siblings(
                self, make_new_module, load_namespace):
            """Load a Python module and Basilisp namespace which are siblings."""
            make_new_module("core.lpy", ns_name="core")
            make_new_module("main.py", module_text="""val = __name__""")

            core = load_namespace("core")
            pymain = importlib.import_module("main")

            assert "core" == core.find(sym.symbol("val")).value
            assert "main" == pymain.val
예제 #29
0
    def test_import_module_with_truncated_timestamp(self, module_dir,
                                                    cached_module_ns,
                                                    cached_module_file,
                                                    load_namespace):
        cache_filename = importer._cache_from_source(
            os.path.join(module_dir, cached_module_file))
        with open(cache_filename, mode="w+b") as f:
            f.write(importer.MAGIC_NUMBER)
            f.write(b"abc")

        using_cache = load_namespace(cached_module_ns)
        assert cached_module_ns == using_cache.find(sym.symbol("val")).value
예제 #30
0
    def test_import_module_with_invalid_rawsize(self, module_dir,
                                                cached_module_ns,
                                                cached_module_file,
                                                load_namespace):
        cache_filename = importer._cache_from_source(
            os.path.join(module_dir, cached_module_file))
        with open(cache_filename, mode="r+b") as f:
            f.seek(8)
            f.write(importer._w_long(7733))

        using_cache = load_namespace(cached_module_ns)
        assert cached_module_ns == using_cache.find(sym.symbol("val")).value