Exemplo n.º 1
0
def test_to_seq():
    assert None is runtime.to_seq(None)
    assert None is runtime.to_seq(llist.PersistentList.empty())
    assert None is runtime.to_seq(vec.PersistentVector.empty())
    assert None is runtime.to_seq(lmap.PersistentMap.empty())
    assert None is runtime.to_seq(lset.PersistentSet.empty())
    assert None is runtime.to_seq("")

    assert None is not runtime.to_seq(llist.l(None))
    assert None is not runtime.to_seq(llist.l(None, None, None))
    assert None is not runtime.to_seq(llist.l(1))
    assert None is not runtime.to_seq(vec.v(1))
    assert None is not runtime.to_seq(lmap.map({"a": 1}))
    assert None is not runtime.to_seq(lset.s(1))
    assert None is not runtime.to_seq("string")

    one_elem = llist.l(kw.keyword("kw"))
    assert one_elem == runtime.to_seq(one_elem)

    seqable = vec.v(kw.keyword("kw"))
    assert seqable == runtime.to_seq(seqable)

    v1 = vec.v(kw.keyword("kw"), 1, llist.l("something"), 3)
    s1 = runtime.to_seq(v1)
    assert isinstance(s1, ISeq)
    for v, s in zip(v1, s1):
        assert v == s

    py_list = [1, 2, 3]
    assert llist.l(1, 2, 3) == runtime.to_seq(py_list)
Exemplo n.º 2
0
def test_set_conj():
    meta = lmap.m(tag="async")
    s1 = lset.s(keyword("kw1"), meta=meta)
    s2 = s1.cons(keyword("kw2"))
    assert s1 is not s2
    assert s1 != s2
    assert len(s2) == 2
    assert meta == s1.meta
    assert meta == s2.meta
Exemplo n.º 3
0
def test_set_with_meta():
    s1 = lset.s("vec")
    assert s1.meta is None

    meta1 = lmap.m(type=symbol("str"))
    s2 = lset.s("vec", meta=meta1)
    assert s2.meta == meta1

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

    meta3 = lmap.m(tag=keyword("macro"))
    s4 = s3.with_meta(meta3)
    assert s3 is not s4
    assert s3 == s4
    assert s4.meta == lmap.m(tag=keyword("macro"))
Exemplo n.º 4
0
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)
    assert "l" == runtime.nth(vec.v("h", "e", "l", "l", "o"), 2)
    assert "l" == runtime.nth(lseq.sequence(["h", "e", "l", "l", "o"]), 2)

    assert "Z" == runtime.nth(llist.l("h", "e", "l", "l", "o"), 7, "Z")
    assert "Z" == runtime.nth(lseq.sequence(["h", "e", "l", "l", "o"]), 7, "Z")
    assert "Z" == runtime.nth(vec.v("h", "e", "l", "l", "o"), 7, "Z")

    with pytest.raises(IndexError):
        runtime.nth(llist.l("h", "e", "l", "l", "o"), 7)

    with pytest.raises(IndexError):
        runtime.nth(lseq.sequence(["h", "e", "l", "l", "o"]), 7)

    with pytest.raises(IndexError):
        runtime.nth(vec.v("h", "e", "l", "l", "o"), 7)

    with pytest.raises(TypeError):
        runtime.nth(lmap.PersistentMap.empty(), 2)

    with pytest.raises(TypeError):
        runtime.nth(lmap.map({"a": 1, "b": 2, "c": 3}), 2)

    with pytest.raises(TypeError):
        runtime.nth(lset.PersistentSet.empty(), 2)

    with pytest.raises(TypeError):
        runtime.nth(lset.s(1, 2, 3), 2)

    with pytest.raises(TypeError):
        runtime.nth(3, 1)

    with pytest.raises(TypeError):
        runtime.nth(3, 1, "Z")
Exemplo n.º 5
0
def test_conj():
    assert llist.l(1) == runtime.conj(None, 1)
    assert llist.l(3, 2, 1) == runtime.conj(None, 1, 2, 3)
    assert llist.l(llist.l(1, 2, 3)) == runtime.conj(None, llist.l(1, 2, 3))

    assert llist.l(1) == runtime.conj(llist.PersistentList.empty(), 1)
    assert llist.l(3, 2, 1) == runtime.conj(llist.PersistentList.empty(), 1, 2, 3)
    assert llist.l(3, 2, 1, 1) == runtime.conj(llist.l(1), 1, 2, 3)
    assert llist.l(llist.l(1, 2, 3), 1) == runtime.conj(llist.l(1), llist.l(1, 2, 3))

    assert lset.s(1) == runtime.conj(lset.PersistentSet.empty(), 1)
    assert lset.s(1, 2, 3) == runtime.conj(lset.PersistentSet.empty(), 1, 2, 3)
    assert lset.s(1, 2, 3) == runtime.conj(lset.s(1), 1, 2, 3)
    assert lset.s(1, lset.s(1, 2, 3)) == runtime.conj(lset.s(1), lset.s(1, 2, 3))

    assert vec.v(1) == runtime.conj(vec.PersistentVector.empty(), 1)
    assert vec.v(1, 2, 3) == runtime.conj(vec.PersistentVector.empty(), 1, 2, 3)
    assert vec.v(1, 1, 2, 3) == runtime.conj(vec.v(1), 1, 2, 3)
    assert vec.v(1, vec.v(1, 2, 3)) == runtime.conj(vec.v(1), vec.v(1, 2, 3))

    assert lmap.map({"a": 1}) == runtime.conj(lmap.PersistentMap.empty(), ["a", 1])
    assert lmap.map({"a": 1, "b": 93}) == runtime.conj(
        lmap.PersistentMap.empty(), ["a", 1], ["b", 93]
    )
    assert lmap.map({"a": 1, "b": 93}) == runtime.conj(
        lmap.map({"a": 8}), ["a", 1], ["b", 93]
    )

    with pytest.raises(ValueError):
        runtime.conj(lmap.map({"a": 8}), "a", 1, "b", 93)

    with pytest.raises(TypeError):
        runtime.conj(3, 1, "a")

    with pytest.raises(TypeError):
        runtime.conj("b", 1, "a")
Exemplo n.º 6
0
 (-1.0, -1),
 (True, True),
 (False, False),
 (None, None),
 ("", ""),
 ("not empty", "not empty"),
 (Fraction("1/2"), Fraction("1/2")),
 (Decimal("3.14159"), Decimal("3.14159")),
 (llist.PersistentList.empty(), llist.PersistentList.empty()),
 (llist.l(1, 2, 3), llist.l(1, 2, 3)),
 (lmap.PersistentMap.empty(), lmap.PersistentMap.empty()),
 (lmap.map({"a": 1, "b": 2}), lmap.map({"a": 1, "b": 2})),
 (lset.PersistentSet.empty(), lset.PersistentSet.empty()),
 (lqueue.PersistentQueue.empty(), lqueue.PersistentQueue.empty()),
 (lqueue.q(1, 2, 3), lqueue.q(1, 2, 3)),
 (lset.s(1, 2, 3), lset.s(1, 2, 3)),
 (vec.PersistentVector.empty(), vec.PersistentVector.empty()),
 (vec.v(1, 2, 3), vec.v(1, 2, 3)),
 (lseq.EMPTY, lseq.EMPTY),
 (lseq.EMPTY.cons(3).cons(2).cons(1), lseq.EMPTY.cons(3).cons(2).cons(1)),
 (lqueue.q(1, 2, 3), lseq.EMPTY.cons(3).cons(2).cons(1)),
 (vec.v(1, 2, 3), lseq.EMPTY.cons(3).cons(2).cons(1)),
 (llist.PersistentList.empty(), vec.PersistentVector.empty()),
 (llist.l(1, 2, 3), vec.v(1, 2, 3)),
 (lseq.EMPTY, vec.PersistentVector.empty()),
 (lqueue.q(1, 2, 3), vec.v(1, 2, 3)),
 (lqueue.q(1, 2, 3), llist.l(1, 2, 3)),
 (lseq.EMPTY, lqueue.PersistentQueue.empty()),
 (lseq.EMPTY.cons(3).cons(2).cons(1), vec.v(1, 2, 3)),
 (llist.PersistentList.empty(), lseq.EMPTY),
 (lseq.EMPTY.cons(3).cons(2).cons(1), llist.l(1, 2, 3)),
Exemplo n.º 7
0
def test_set_seq():
    assert None is lset.PersistentSet.empty().seq()
    assert {1} == set(lset.s(1).seq())
    assert {1, 2} == set(lset.s(1, 2).seq())
    assert {1, 2, 3} == set(lset.s(1, 2, 3).seq())
Exemplo n.º 8
0
def test_set_meta():
    assert lset.s("vec").meta is None
    meta = lmap.m(type=symbol("str"))
    assert lset.s("vec", meta=meta).meta == meta
Exemplo n.º 9
0
def test_set_as_function():
    assert None is lset.s()(1)
    assert None is lset.s()("abc")
    assert None is lset.s(1, 2, 3)("abc")
    assert 1 == lset.s(1, 2, 3)(1)
    assert 3 == lset.s(1, 2, 3)(3)
Exemplo n.º 10
0
def test_set_interface_membership(interface):
    assert isinstance(lset.s(), interface)
    assert issubclass(lset.PersistentSet, interface)
Exemplo n.º 11
0
    assert s3 is not s4
    assert s3 == s4
    assert s4.meta == lmap.m(tag=keyword("macro"))


def test_set_seq():
    assert None is lset.PersistentSet.empty().seq()
    assert {1} == set(lset.s(1).seq())
    assert {1, 2} == set(lset.s(1, 2).seq())
    assert {1, 2, 3} == set(lset.s(1, 2, 3).seq())


@pytest.mark.parametrize(
    "o",
    [
        lset.s(),
        lset.s(keyword("kw1")),
        lset.s(keyword("kw1"), 2),
        lset.s(keyword("kw1"), 2, None, "nothingness"),
        lset.s(keyword("kw1"), lset.s("string", 4)),
    ],
)
def test_set_pickleability(pickle_protocol: int, o: lset.PersistentSet):
    assert o == pickle.loads(pickle.dumps(o, protocol=pickle_protocol))


@pytest.mark.parametrize(
    "l,str_repr",
    [
        (lset.s(), {"#{}"}),
        (lset.s(keyword("kw1")), {"#{:kw1}"}),