def setUp(self):
     self.currentdir = os.path.abspath(os.curdir)
     self.testdir = os.path.join(self.currentdir, "tests")
     self.assetdir = os.path.join(self.testdir, "assets")
     self.consMaker = PrimitiveMaker("constant string")
     self.constrMaker = PrimitiveMaker("constraint string")
     self.nnmaker = PrimitiveMaker("non numeric string")
 def test_primitive_maker_choice(self):
     check = False
     try:
         pmaker = PrimitiveMaker("weird choice")
         pmaker.make(mystr="my string")
     except ValueError:
         check = True
     self.assertEqual(check, True)
 def test_constant_string(self):
     "test for constant string primitive"
     pmaker = PrimitiveMaker("constant string")
     myconstr1 = pmaker.make(mystr="my true constant string and unicode š")
     myconstr2 = pmaker.make(mystr="my true constant string and unicode š")
     myconstr3 = pmaker.make(mystr="my true constant string and unicode ḫ")
     self.assertEqual(True, myconstr1.isValid())
     self.assertEqual(False, myconstr1 == myconstr3)
     self.assertEqual(True, myconstr1 == myconstr2)
 def from_element(cls, element: etree.Element):
     ""
     elclass = element.get("class")
     eltag = element.tag
     cls.check_value_error(elclass, "NonNumericString", "Given element class: ")
     cls.check_value_error(eltag, "primitive", "Given element tag: ")
     cstr = element.text
     constr = cls.text_to_constant_string(cstr)
     maker = PrimitiveMaker("non numeric string")
     return maker.make(mystr=constr)
        def from_dict(cls, cdict: dict):
            "make object from given dict"
            objclass = cdict.get("class", "")
            objtype = cdict.get("type", "")
            cls.check_value_error(objclass, "NonNumericString", "Given object class: ")
            cls.check_value_error(objtype, "primitive", "Given object type: ")

            constr = cls.dict_to_constant_string(cdict["value"])
            maker = PrimitiveMaker("non numeric string")
            return maker.make(mystr=constr)
 def from_dict(cls, cdict: dict) -> ConstraintString:
     "transform dict to ConstraintString"
     objclass = cdict.get("class", "")
     objtype = cdict.get("type", "")
     cls.check_value_error(objtype, "primitive", "Given object type: ")
     cls.check_value_error(objclass, "ConstraintString", "Given object class: ")
     myfn = bytes.fromhex(cdict["constraint"])
     myfnc = dill.loads(myfn)
     constrdict = cdict["value"]
     constr = cls.dict_to_constant_string(constrdict)
     maker = PrimitiveMaker("constraint string")
     return maker.make(mystr=constr, fnc=myfnc)
 def from_element(cls, el: etree.Element) -> ConstantString:
     "from element"
     elclass = el.get("class")
     cls.check_value_error(
         objval=elclass,
         wantedVal="ConstantString",
         messPrefix="Given tag class: ",
     )
     elstr = el.text
     maker = PrimitiveMaker("constant string")
     constr = maker.make(mystr=elstr)
     return constr
        def from_element(cls, element: etree.Element):
            ""
            elclass = element.get("class")
            eltag = element.tag
            cls.check_value_error(elclass, "ConstraintString", "Given element class: ")
            cls.check_value_error(eltag, "primitive", "Given element tag: ")

            myfn = bytes.fromhex(element.get("constraint"))
            myfn = dill.loads(myfn)
            cstr = element.text
            constr = cls.text_to_constant_string(cstr)
            maker = PrimitiveMaker("constraint string")
            return maker.make(mystr=constr, fnc=myfn)
 def from_dict(cls, cdict: dict) -> ConstantString:
     "render constant string from dict"
     objtype = cdict.get("type", "")
     objclass = cdict.get("class", "")
     cls.check_value_error(
         objtype, "primitive", messPrefix="Given object type: "
     )
     cls.check_value_error(
         objclass, "ConstantString", messPrefix="Given object class: "
     )
     maker = PrimitiveMaker("constant string")
     constr = maker.make(mystr=cdict["value"])
     return constr
class _ContainerXmlIo(_ContainerIo):
    "Container input output"
    cmaker = ContainerMaker("")
    pmaker = PrimitiveMaker("")
    primitiveIo = ConstantStringIo.getIoClass("xml")

    def __init__(self, container, containerType):
        super().__init__(container, containerType)

    @classmethod
    def element_to_dict(cls, el: etree.Element) -> dict:
        "transform element to dict"
        eldict = el.attrib
        eldict["text"] = el.text
        eldict["tag"] = el.tag
        return eldict

    @classmethod
    def unit_to_member(cls, el: etree.Element):
        "transform element to member"
        return cls.primitiveIo.from_element(el)

    def member_to_unit(self, member: ConstantString) -> etree.Element:
        "transform member to element"
        consio = self.primitiveIo(member)
        return consio.to_element()

    def member_to_dict(self, member: ConstantString) -> dict:
        "transform member to element"
        consio = self.primitiveIo(member)
        return consio.to_dict()
class _ContainerJsonIo(_ContainerIo):
    "Container input output json"
    cmaker = ContainerMaker("")
    pmaker = PrimitiveMaker("")
    primitiveIo = ConstantStringIo.getIoClass("json")

    def __init__(self, container, containerType):
        super().__init__(container, containerType)

    @classmethod
    def json_to_dict(cls, jsonstr: str) -> dict:
        return json.loads(jsonstr)

    def member_to_unit(self, member: ConstantString) -> dict:
        consio = self.primitiveIo(member)
        return consio.to_dict()

    @classmethod
    def unit_to_member(cls, cdict: dict):
        return cls.primitiveIo.from_dict(cdict)

    def to_json(self):
        return dict_dump(self.to_dict())

    def toJSON(self):
        return self.to_json()
    class XmlIo(_ContainerXmlIo):
        "pair xml io"

        cmaker = ContainerMaker("pair")
        pmaker = PrimitiveMaker(choice="constant string")
        primitiveIo = ConstantStringIo.getIoClass("xml")

        def __init__(self, pair):
            super().__init__(pair, Pair)

        def to_element(self):
            "transform pair to xml"
            str1 = self.container.arg1
            str2 = self.container.arg2
            el = etree.Element("pair")
            el.set("class", self.containerType.__name__)
            el1 = self.member_to_unit(str1)
            el2 = self.member_to_unit(str2)
            self.add_members_to_parent(el, [el1, el2])
            return el

        def to_dict(self) -> dict:
            "to dict pair"
            str1 = self.container.arg1
            str2 = self.container.arg2
            pdict = {}
            pdict["class"] = self.containerType.__name__
            pdict["type"] = "pair"
            member1 = self.member_to_dict(str1)
            member2 = self.member_to_dict(str2)
            pdict["members"] = []
            self.add_members_to_parent(pdict["members"], [member1, member2])
            return pdict

        @classmethod
        def from_element(cls, el: etree.Element):
            "Obtain pair from element"
            eltag = el.tag
            elclass = el.get("class")
            cls.check_value_error(eltag, "pair", "Given element tag: ")
            cls.check_value_error(elclass, "Pair", "Given element class: ")
            arg1 = cls.unit_to_member(el[0])
            arg2 = cls.unit_to_member(el[1])
            pair = cls.cmaker.make(arg1=arg1, arg2=arg2)
            return pair
 def test_primitive_maker_constant_string(self):
     pmaker = PrimitiveMaker("char")
     check = False
     try:
         pmaker.make_constant_string(1235)
     except TypeError:
         check = True
     self.assertTrue(check)
     check = False
     try:
         pmaker.make_constant_string(1235.05)
     except TypeError:
         check = True
     self.assertTrue(check)
     check = False
     try:
         pmaker.make_constant_string(True)
     except TypeError:
         check = True
     self.assertTrue(check)
    class JsonIo(_ContainerJsonIo):
        "pair json io"
        cmaker = ContainerMaker("pair")
        pmaker = PrimitiveMaker(choice="constant string")
        primitiveIo = ConstantStringIo.getIoClass("json")

        def __init__(self, pair):
            super().__init__(pair, Pair)

        def to_dict(self) -> dict:
            "to dict pair"
            str1 = self.container.arg1
            str2 = self.container.arg2
            pdict = {}
            pdict["class"] = self.containerType.__name__
            pdict["type"] = "pair"
            member1 = self.member_to_unit(str1)
            member2 = self.member_to_unit(str2)
            pdict["members"] = []
            self.add_members_to_parent(pdict["members"], [member1, member2])
            return pdict

        @classmethod
        def from_json(cls, jsonstr: str):
            "obtain pair from json object"
            objdict = json.loads(jsonstr)
            return cls.from_dict(objdict)

        @classmethod
        def from_dict(cls, cdict: dict):
            "obtain pair from dict"
            objtype = cdict.get("type", "")
            objclass = cdict.get("class", "")
            cls.check_value_error(objtype, "pair", "Given object type: ")
            cls.check_value_error(objclass, "Pair", "Given object class: ")
            members = cdict["members"]
            member1 = cls.unit_to_member(members[0])
            member2 = cls.unit_to_member(members[1])
            pair = cls.cmaker.make(arg1=member1, arg2=member2)
            return pair
class TestContainer(unittest.TestCase):
    "test container.py"

    def setUp(self):
        self.currentdir = os.path.abspath(os.curdir)
        self.testdir = os.path.join(self.currentdir, "tests")
        self.assetdir = os.path.join(self.testdir, "assets")
        self.consMaker = PrimitiveMaker("constant string")
        self.constrMaker = PrimitiveMaker("constraint string")
        self.nnmaker = PrimitiveMaker("non numeric string")

    def test_pair(self):
        pmaker = ContainerMaker("pair")
        mystr1 = self.consMaker.make(mystr="mystr1")
        mystr2 = self.nnmaker.from_string(mystr="mystr2")
        mystr3 = self.consMaker.make(mystr="mystr3")
        check = True
        try:
            pair = pmaker.make(arg1=mystr1, arg2=mystr2)
        except ValueError or TypeError:
            check = False
        self.assertTrue(check, "either value or type error triggered")
        check = False
        try:
            pair = pmaker.make(arg1=mystr1, arg2=mystr3)
        except ValueError or TypeError:
            check = True
        self.assertTrue(check, "either value or type error should have been triggered")

    def test_array(self):
        pmaker = ContainerMaker("array")
        mystr1 = self.consMaker.make(mystr="mystr1")
        mystr2 = self.nnmaker.from_string(mystr="mystr2")
        mystr3 = self.consMaker.make(mystr="mystr3")
        check = True
        try:
            arr = pmaker.make(elements=[mystr1, mystr3])
        except ValueError or TypeError:
            check = False
        self.assertTrue(check, "either value or type error triggered")
        check = False
        try:
            arr = pmaker.make(elements=[mystr1, mystr2])
        except ValueError or TypeError:
            check = True
        self.assertTrue(check, "either value or type error should have been triggered")
    def test_primitive_maker_from_str(self):
        pmaker = PrimitiveMaker("constant string")
        cmaker = PrimitiveMaker("constraint string")
        check = True
        try:
            constr = pmaker.from_string(mystr="a str")
        except ValueError or TypeError:
            check = False
        self.assertTrue(check)

        def lfn(x):
            return x.constr.islower()

        check = True
        try:
            constr = cmaker.from_string(mystr="another str", fnc=lfn)
        except ValueError or TypeError:
            check = False
        self.assertTrue(check)
    def test_constraint_string(self):
        pmaker = PrimitiveMaker("constant string")

        def lfn(x: ConstantString):
            return x.constr.islower()

        costr1 = pmaker.make(mystr="my true string")
        costr2 = pmaker.make(mystr="MY False String")
        cstr1 = pmaker.from_type(primitiveType=ConstraintString,
                                 mystr=costr1,
                                 fnc=lfn)
        check = False
        try:
            cstr2 = pmaker.from_type(primitiveType=ConstraintString,
                                     mystr=costr2,
                                     fnc=lfn)
        except ValueError:
            check = True

        self.assertEqual(True, cstr1.isValid())
        self.assertEqual(True, check)
 def text_to_constant_string(text: str) -> ConstantString:
     "transform string to constant string"
     maker = PrimitiveMaker("constant string")
     return maker.make(mystr=text)
    def test_nonnumeric_string(self):
        pmaker = PrimitiveMaker("constant string")
        costr1 = pmaker.make(mystr="my true string")
        costr2 = pmaker.make(mystr="1 my another true string 1")
        costr3 = pmaker.make(mystr="123")
        costr4 = pmaker.make(mystr="123.45")
        costr5 = pmaker.make(mystr="123j")
        costr6 = pmaker.make(mystr="123/12335")

        pmaker = PrimitiveMaker("non numeric string")
        cstr1 = pmaker.make(mystr=costr1)
        cstr2 = pmaker.make(mystr=costr2)
        check = False
        try:
            cstr3 = pmaker.make(mystr=costr3)
        except ValueError:
            check = True
        self.assertEqual(True, check)
        check = False
        try:
            cstr4 = pmaker.make(mystr=costr4)
        except ValueError:
            check = True
        self.assertEqual(True, check)
        check = False
        try:
            cstr5 = pmaker.make(mystr=costr5)
        except ValueError:
            check = True
        self.assertEqual(True, check)
        check = False
        try:
            cstr6 = pmaker.make(mystr=costr6)
        except ValueError:
            check = True
        self.assertEqual(True, check)
Exemple #20
0
    def setUp(self):
        "setup small data"

        def lfn(x: ConstantString):
            return x.constr.islower()

        pmakerc = PrimitiveMaker("constraint string")
        pmakercons = PrimitiveMaker("constant string")
        nnmaker = PrimitiveMaker("non numeric string")
        cmaker = ContainerMaker
        self.constrio = ConstraintStringIo
        self.consio = ConstantStringIo

        self.mycstr = pmakerc.from_string(mystr="my valid constraint string",
                                          fnc=lfn)
        self.myfn = dill.dumps(self.mycstr.fn)
        self.mynnstr = nnmaker.from_string(mystr="my valid non numeric string")
        self.mynnfn = dill.dumps(self.mynnstr.fn)
        self.parg1 = pmakercons.make(mystr="mystr1")
        self.parg2 = pmakercons.make(mystr="mystr2")
        self.pair = cmaker.from_type(objType=Pair,
                                     arg1=self.parg1,
                                     arg2=self.parg2)
        mystr1 = pmakerc.from_string(mystr="my true string", fnc=lfn)
        mystr2 = pmakerc.from_string(mystr="my another true string", fnc=lfn)
        self.scpair1 = cmaker.from_type(objType=SingleConstraintPair,
                                        arg1=mystr1,
                                        arg2=mystr2)