def test_enums_picklable(): "Test that actions generated by the enums rule can be pickled." actions = [ std.enums(verb=verb, typ=typ, ctx=Rules()) for verb in [JSON2PY, PY2JSON, INSP_PY, INSP_JSON] for typ in [Enum1, Enum2] ] assert None not in actions dumps(actions)
def test_enums_int(): "Test the enums rule will generate encoders and decoders for enumerated type subclasses." decoder = std.enums(verb=JSON2PY, typ=Enum2, ctx=Fail()) assert decoder("ALPHA") == Enum2.ALPHA assert decoder("GAMMA") == Enum2.GAMMA encoder = std.enums(verb=PY2JSON, typ=Enum2, ctx=Fail()) assert encoder(Enum2.BETA) == "BETA" assert encoder(Enum2.GAMMA) == "GAMMA" inspect = std.enums(verb=INSP_PY, typ=Enum2, ctx=Fail()) assert not inspect("ALPA") assert not inspect(Enum1.CHARLIE) assert inspect(Enum2.BETA) inspect = std.enums(verb=INSP_JSON, typ=Enum2, ctx=Fail()) assert not inspect(Enum2.GAMMA) assert inspect("BETA") assert not inspect("ABLE")
def test_enums(): "Test the enums rule will generate encoders and decoders for enumerated types." decoder = std.enums(verb=JSON2PY, typ=Enum1, ctx=Rules()) assert decoder("ABLE") == Enum1.ABLE assert decoder("CHARLIE") == Enum1.CHARLIE encoder = std.enums(verb=PY2JSON, typ=Enum1, ctx=Rules()) assert encoder(Enum1.BAKER) == "BAKER" assert encoder(Enum1.CHARLIE) == "CHARLIE" inspect = std.enums(verb=INSP_PY, typ=Enum1, ctx=Rules()) assert not inspect("ABLE") assert inspect(Enum1.CHARLIE) assert not inspect(Enum2.BETA) inspect = std.enums(verb=INSP_JSON, typ=Enum1, ctx=Rules()) assert not inspect(Enum1.BAKER) assert not inspect("BETA") assert inspect("CHARLIE")
def test_enums_disregard(): "Test the iso_dates rule will disregard unknown types and verbs." assert std.enums(verb="unknown", typ=Enum1, ctx=Rules()) is None assert std.enums(verb=JSON2PY, typ=Mystery, ctx=Rules()) is None assert std.enums(verb=PY2JSON, typ=Mystery, ctx=Rules()) is None