Skip to content

Commit

Permalink
Test get
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrink10 committed Jun 17, 2020
1 parent 3add231 commit 036802e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 31 deletions.
75 changes: 44 additions & 31 deletions src/basilisp/lang/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
IPersistentVector,
ISeq,
ISeqable,
ITransientSet,
)
from basilisp.lang.reference import ReferenceBase
from basilisp.lang.typing import CompilerOpts, LispNumber
Expand Down Expand Up @@ -1036,7 +1037,7 @@ def nth(coll, i: int, notfound=__nth_sentinel):

@nth.register(type(None))
def _nth_none(_: None, i: int, notfound=__nth_sentinel) -> None:
return None
return notfound if notfound is not __nth_sentinel else None


@nth.register(Sequence)
Expand All @@ -1061,6 +1062,48 @@ def _nth_iseq(coll: ISeq, i: int, notfound=__nth_sentinel):
raise IndexError(f"Index {i} out of bounds")


@functools.singledispatch
def contains(coll, k):
"""Return true if o contains the key k."""
return k in coll


@contains.register(IAssociative)
def _contains_iassociative(coll, k):
return coll.contains(k)


@functools.singledispatch
def get(m, k, default=None):
"""Return the value of k in m. Return default if k not found in m."""
return default


@get.register(dict)
@get.register(list)
@get.register(str)
def _get_others(m, k, default=None):
try:
return m[k]
except (KeyError, IndexError):
return default


@get.register(IPersistentSet)
@get.register(ITransientSet)
@get.register(frozenset)
@get.register(set)
def _get_settypes(m, k, default=None):
if k in m:
return k
return default


@get.register(ILookup)
def _get_ilookup(m, k, default=None):
return m.val_at(k, default)


@functools.singledispatch
def assoc(m, *kvs):
"""Associate keys to values in associative data structure m. If m is None,
Expand Down Expand Up @@ -1225,36 +1268,6 @@ def __ge__(self, other):
return lseq.sequence(sorted(coll, key=key))


@functools.singledispatch
def contains(coll, k):
"""Return true if o contains the key k."""
return k in coll


@contains.register(IAssociative)
def _contains_iassociative(coll, k):
return coll.contains(k)


@functools.singledispatch
def get(m, k, default=None):
"""Return the value of k in m. Return default if k not found in m."""
try:
return m[k]
except (KeyError, IndexError):
return default


@get.register(type(None))
def _get_none(_: None, k, default=None) -> None:
return None


@get.register(ILookup)
def _get_ilookup(m, k, default=None):
return m.val_at(k, default)


def is_special_form(s: sym.Symbol) -> bool:
"""Return True if s names a special form."""
return s in _SPECIAL_FORMS
Expand Down
41 changes: 41 additions & 0 deletions tests/basilisp/runtime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def test_apply():

def test_nth():
assert None is runtime.nth(None, 1)
assert "not found" == runtime.nth(None, 4, "not found")
assert "l" == runtime.nth("hello world", 2)
assert "l" == runtime.nth(["h", "e", "l", "l", "o"], 2)
assert "l" == runtime.nth(llist.l("h", "e", "l", "l", "o"), 2)
Expand Down Expand Up @@ -194,6 +195,46 @@ def test_nth():
runtime.nth(3, 1, "Z")


def test_get():
assert None is runtime.get(None, "a")
assert keyword.keyword("nada") is runtime.get(None, "a", keyword.keyword("nada"))
assert None is runtime.get(3, "a")
assert keyword.keyword("nada") is runtime.get(3, "a", keyword.keyword("nada"))
assert 1 == runtime.get(lmap.map({"a": 1}), "a")
assert None is runtime.get(lmap.map({"a": 1}), "b")
assert 2 == runtime.get(lmap.map({"a": 1}), "b", 2)
assert 1 == runtime.get(lmap.map({"a": 1}).to_transient(), "a")
assert None is runtime.get(lmap.map({"a": 1}).to_transient(), "b")
assert 2 == runtime.get(lmap.map({"a": 1}).to_transient(), "b", 2)
assert 1 == runtime.get(vec.v(1, 2, 3), 0)
assert None is runtime.get(vec.v(1, 2, 3), 3)
assert "nada" == runtime.get(vec.v(1, 2, 3), 3, "nada")
assert 1 == runtime.get(vec.v(1, 2, 3).to_transient(), 0)
assert None is runtime.get(vec.v(1, 2, 3).to_transient(), 3)
assert "nada" == runtime.get(vec.v(1, 2, 3).to_transient(), 3, "nada")
assert "l" == runtime.get("hello world", 2)
assert None is runtime.get("hello world", 50)
assert "nada" == runtime.get("hello world", 50, "nada")
assert "l" == runtime.get(["h", "e", "l", "l", "o"], 2)
assert None is runtime.get(["h", "e", "l", "l", "o"], 50)
assert "nada" == runtime.get(["h", "e", "l", "l", "o"], 50, "nada")
assert 1 == runtime.get({"a": 1}, "a")
assert None is runtime.get({"a": 1}, "b")
assert 2 == runtime.get({"a": 1}, "b", 2)
assert "a" == runtime.get({"a", "b", "c"}, "a")
assert None is runtime.get({"a", "b", "c"}, "d")
assert 2 == runtime.get({"a", "b", "c"}, "d", 2)
assert "a" == runtime.get(frozenset({"a", "b", "c"}), "a")
assert None is runtime.get(frozenset({"a", "b", "c"}), "d")
assert 2 == runtime.get(frozenset({"a", "b", "c"}), "d", 2)
assert "a" == runtime.get(lset.set({"a", "b", "c"}), "a")
assert None is runtime.get(lset.set({"a", "b", "c"}), "d")
assert 2 == runtime.get(lset.set({"a", "b", "c"}), "d", 2)
assert "a" == runtime.get(lset.set({"a", "b", "c"}).to_transient(), "a")
assert None is runtime.get(lset.set({"a", "b", "c"}).to_transient(), "d")
assert 2 == runtime.get(lset.set({"a", "b", "c"}).to_transient(), "d", 2)


def test_assoc():
assert lmap.Map.empty() == runtime.assoc(None)
assert lmap.map({"a": 1}) == runtime.assoc(None, "a", 1)
Expand Down

0 comments on commit 036802e

Please sign in to comment.