コード例 #1
0
def test_atoms_int():
    "Test the atoms rule will generate encoders and decoders for integers."

    decoder = std.atoms(verb=JSON2PY, typ=int, ctx=Rules())
    assert decoder(77) == 77

    encoder = std.atoms(verb=PY2JSON, typ=int, ctx=Rules())
    assert encoder(77) == 77

    inspect = std.atoms(verb=INSP_PY, typ=int, ctx=Rules())
    assert not inspect("string")
    assert inspect(5)

    inspect = std.atoms(verb=INSP_JSON, typ=int, ctx=Rules())
    assert not inspect("string")
    assert inspect(5)
コード例 #2
0
def test_atoms_str():
    "Test the atoms rule will generate encoders and decoders for strings."

    decoder = std.atoms(verb=JSON2PY, typ=str, ctx=Rules())
    assert decoder("some string") == "some string"

    encoder = std.atoms(verb=PY2JSON, typ=str, ctx=Rules())
    assert encoder("some string") == "some string"

    inspect = std.atoms(verb=INSP_PY, typ=str, ctx=Rules())
    assert inspect("string")
    assert not inspect(5)

    inspect = std.atoms(verb=INSP_JSON, typ=str, ctx=Rules())
    assert inspect("string")
    assert not inspect(5)
コード例 #3
0
def test_atoms_bool():
    "Test the atoms rule will generate encoders and decoders for booleans."

    decoder = std.atoms(verb=JSON2PY, typ=bool, ctx=Rules())
    assert decoder(False) is False
    assert decoder(True) is True

    encoder = std.atoms(verb=PY2JSON, typ=bool, ctx=Rules())
    assert encoder(False) is False
    assert encoder(True) is True

    inspect = std.atoms(verb=INSP_PY, typ=bool, ctx=Rules())
    assert not inspect("string")
    assert inspect(True)

    inspect = std.atoms(verb=INSP_JSON, typ=bool, ctx=Rules())
    assert not inspect("string")
    assert inspect(False)
コード例 #4
0
def test_atoms_picklable():
    "Test that actions generated by the atoms rule can be pickled."

    actions = [
        std.atoms(verb=verb, typ=typ, ctx=Rules())
        for verb in [JSON2PY, PY2JSON, INSP_PY, INSP_JSON]
        for typ in [str, int, bool, NoneType]
    ]
    assert None not in actions
    dumps(actions)
コード例 #5
0
def test_atoms_null():
    "Test the atoms rule will generate encoders and decoders for None / null."

    decoder = std.atoms(verb=JSON2PY, typ=NoneType, ctx=Rules())
    assert decoder(None) is None
    with pytest.raises(ValueError):
        decoder(5)

    encoder = std.atoms(verb=PY2JSON, typ=NoneType, ctx=Rules())
    assert encoder(None) is None
    with pytest.raises(ValueError):
        encoder(5)

    inspect = std.atoms(verb=INSP_PY, typ=NoneType, ctx=Rules())
    assert inspect(None)
    assert not inspect(0)

    inspect = std.atoms(verb=INSP_JSON, typ=NoneType, ctx=Rules())
    assert inspect(None)
    assert not inspect(0)
コード例 #6
0
def test_atoms_disregard():
    "Test the atoms rule will disregard unknown types and verbs."

    assert std.atoms(verb="unknown", typ=str, ctx=Rules()) is None
    for verb in (JSON2PY, PY2JSON, INSP_PY, INSP_JSON):
        assert std.atoms(verb=verb, typ=Mystery, ctx=Rules()) is None