def test_ns_completion(self, ns: Namespace): assert {"basilisp.string/"} == set(ns.complete("basilisp.st")) assert {"basilisp.string/join" } == set(ns.complete("basilisp.string/j")) assert {"str/", "string?", "str"} == set(ns.complete("st")) assert {"map"} == set(ns.complete("m")) assert {"map"} == set(ns.complete("ma"))
def get_or_create_ns( name: sym.Symbol, refer: Tuple[sym.Symbol] = (CORE_NS_SYM,) ) -> Namespace: """Get or create the namespace named by `name`, referring in all of the symbols of the namespaced named by `refer`.""" ns = Namespace.get_or_create(name) for refer_name in filter(lambda s: s != name, refer): refer_ns = Namespace.get_or_create(refer_name) ns.refer_all(refer_ns) return ns
def test_remove_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 = Namespace.remove(ns_sym) assert isinstance(ns, Namespace) assert ns.name == ns_sym.name assert len(list(ns_cache_with_existing_ns.deref().keys())) == 1
def ns_cache_with_existing_ns(ns_sym: sym.Symbol, core_ns_sym: sym.Symbol, core_ns: Namespace) -> atom.Atom[NamespaceMap]: """Patch the Namespace cache with a test fixture with an existing namespace.""" with patch( "basilisp.lang.runtime.Namespace._NAMESPACES", atom.Atom( lmap.map({ core_ns_sym: core_ns, ns_sym: Namespace(ns_sym) })), ) as cache: yield cache
def ns_cache(ns_sym: sym.Symbol) -> atom.Atom[NamespaceMap]: with patch( "basilisp.lang.runtime.Namespace._NAMESPACES", atom.Atom(lmap.map({ns_sym: Namespace(ns_sym)})), ) as ns_cache: yield ns_cache
def test_remove_non_existent_ns(other_ns_sym: sym.Symbol, ns_cache_with_existing_ns: patch): assert len(list(ns_cache_with_existing_ns.deref().keys())) == 2 ns = Namespace.remove(other_ns_sym) assert ns is None assert len(list(ns_cache_with_existing_ns.deref().keys())) == 2
def test_import_and_alias(self, ns: Namespace): assert {"time/"} == set(ns.complete("ti")) assert {"time/asctime"} == set(ns.complete("time/as")) assert {"py-time/"} == set(ns.complete("py-t")) assert {"py-time/asctime"} == set(ns.complete("py-time/as"))
def ns(self) -> Namespace: ns_sym = sym.symbol("test") ns = Namespace(ns_sym) str_ns_alias = sym.symbol("basilisp.string") join_sym = sym.symbol("join") chars_sym = sym.symbol("chars") str_ns = Namespace(str_ns_alias) str_ns.intern(join_sym, Var(ns, join_sym)) str_ns.intern( chars_sym, Var(ns, chars_sym, meta=lmap.map({kw.keyword("private"): True}))) ns.add_alias(str_ns, str_ns_alias) str_alias = sym.symbol("str") ns.add_alias(Namespace(str_alias), str_alias) str_sym = sym.symbol("str") ns.intern(str_sym, Var(ns, str_sym)) is_string_sym = sym.symbol("string?") ns.intern(is_string_sym, Var(ns, is_string_sym)) time_sym = sym.symbol("time") time_alias = sym.symbol("py-time") ns.add_import(time_sym, __import__("time"), time_alias) core_ns = Namespace(sym.symbol("basilisp.core")) map_alias = sym.symbol("map") ns.add_refer(map_alias, Var(core_ns, map_alias)) return ns
def test_cannot_remove_core(ns_cache: atom.Atom[NamespaceMap]): with pytest.raises(ValueError): Namespace.remove(sym.symbol("basilisp.core"))