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
def ns(test_ns: str, test_ns_sym: sym.Symbol) -> runtime.Namespace: get_or_create_ns(test_ns_sym) with runtime.ns_bindings(test_ns) as ns: try: yield ns finally: runtime.Namespace.remove(test_ns_sym)
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
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)
def test_alias(ns_cache: atom.Atom[NamespaceMap]): ns1 = get_or_create_ns(sym.symbol("ns1")) ns2 = get_or_create_ns(sym.symbol("ns2")) ns1.add_alias(ns2, sym.symbol("n2")) assert None is ns1.get_alias(sym.symbol("ns2")) assert ns2 is ns1.get_alias(sym.symbol("n2")) ns1.remove_alias(sym.symbol("n2")) assert None is ns1.get_alias(sym.symbol("n2"))
def test_get_existing_ns(ns_sym: sym.Symbol, ns_cache_with_existing_ns: atom.Atom[NamespaceMap]): assert len(list(ns_cache_with_existing_ns.deref().keys())) == 2 ns = get_or_create_ns(ns_sym) assert isinstance(ns, Namespace) assert ns.name == ns_sym.name assert len(list(ns_cache_with_existing_ns.deref().keys())) == 2
def test_resolve_alias(self, core_ns): ns_name = "resolve-test" ns_sym = sym.symbol(ns_name) with runtime.ns_bindings(ns_name) as ns: runtime.Var.intern(ns_sym, sym.symbol("existing-var"), None) assert sym.symbol("existing-var", ns=ns_name) == runtime.resolve_alias( sym.symbol("existing-var"), ns=ns ) assert sym.symbol("non-existent-var", ns=ns_name) == runtime.resolve_alias( sym.symbol("non-existent-var"), ns=ns ) foo_ns_sym = sym.symbol("zux.bar.foo") foo_ns = get_or_create_ns(foo_ns_sym) ns.add_alias(foo_ns, sym.symbol("foo")) assert sym.symbol( "aliased-var", ns=foo_ns_sym.name ) == runtime.resolve_alias(sym.symbol("aliased-var", ns="foo"), ns=ns) assert sym.symbol( "non-existent-alias-var", ns="wee.woo" ) == runtime.resolve_alias( sym.symbol("non-existent-alias-var", ns="wee.woo"), ns=ns )
def test_dynamic_unbound( ns_sym: sym.Symbol, var_name: sym.Symbol, ): v = Var.intern_unbound(ns_sym, var_name, dynamic=True) assert isinstance(v, Var) assert ns_sym.name == v.ns.name assert var_name == v.name assert not v.is_bound assert v.dynamic assert not v.is_thread_bound assert Unbound(v) == v.root assert Unbound(v) == v.value assert Unbound(v) == v.deref() new_val = kw.keyword("new-val") try: v.push_bindings(new_val) assert v.is_bound assert v.dynamic assert v.is_thread_bound assert Unbound(v) == v.root assert new_val == v.value assert new_val == v.deref() finally: v.pop_bindings() assert not v.is_bound ns = get_or_create_ns(ns_sym) assert None is not ns assert ns.find(var_name) == v
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"))
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
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"))
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"))
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)
def test_intern_unbound( ns_sym: sym.Symbol, var_name: sym.Symbol, ): v = Var.intern_unbound(ns_sym, var_name) assert isinstance(v, Var) assert ns_sym.name == v.ns.name assert var_name == v.name assert not v.is_bound assert not v.dynamic assert not v.is_thread_bound assert Unbound(v) == v.root assert Unbound(v) == v.value assert Unbound(v) == v.deref() ns = get_or_create_ns(ns_sym) assert None is not ns assert ns.find(var_name) == v
def test_intern_does_not_overwrite(ns_cache: atom.Atom[NamespaceMap]): ns = get_or_create_ns(sym.symbol("ns1")) var_sym = sym.symbol("useful-value") var_val1 = "cool string" var1 = Var(ns, var_sym) var1.set_value(var_val1) ns.intern(var_sym, var1) var_val2 = "lame string" var2 = Var(ns, var_sym) var2.set_value(var_val2) ns.intern(var_sym, var2) assert var1 is ns.find(var_sym) assert var_val1 == ns.find(var_sym).value ns.intern(var_sym, var2, force=True) assert var2 is ns.find(var_sym) assert var_val2 == ns.find(var_sym).value
def test_intern( ns_sym: sym.Symbol, var_name: sym.Symbol, intern_val, ): meta = lmap.map({kw.keyword("hello"): "there"}) v = Var.intern(ns_sym, var_name, intern_val, meta=meta) assert isinstance(v, Var) assert ns_sym.name == v.ns.name assert var_name == v.name assert v.is_bound assert not v.dynamic assert not v.is_thread_bound assert intern_val == v.root assert intern_val == v.value assert intern_val == v.deref() assert meta == v.meta ns = get_or_create_ns(ns_sym) assert None is not ns assert ns.find(var_name) == v # Check that attempting to re-intern the Var will overwrite meta and dynamic new_meta = lmap.map({kw.keyword("general"): "kenobi"}) new_value = kw.keyword("new-value") re_v = Var.intern(ns_sym, var_name, new_value, dynamic=True, meta=new_meta) assert v is re_v assert ns_sym.name == v.ns.name assert var_name == v.name assert v.is_bound assert v.dynamic assert not v.is_thread_bound assert new_value == v.root assert new_value == v.value assert new_value == v.deref() assert new_meta == v.meta
def core_ns(core_ns_sym: sym.Symbol) -> runtime.Namespace: return get_or_create_ns(core_ns_sym)