def test_String(self):
        regex = re.compile("^{}$".format(
            RegularExpressionVisitor.OnString(
                StringTypeInfo(min_length=2))[0]))
        self.assertTrue(regex.match("123"))
        self.assertTrue(regex.match("12"))
        self.assertFalse(regex.match("1"))

        regex = re.compile("^{}$".format(
            RegularExpressionVisitor.OnString(
                StringTypeInfo(max_length=2))[0]))
        self.assertTrue(regex.match("12"))
        self.assertFalse(regex.match("123"))

        regex = re.compile("^{}$".format(
            RegularExpressionVisitor.OnString(
                StringTypeInfo(min_length=2, max_length=3))[0]))
        self.assertTrue(regex.match("123"))
        self.assertTrue(regex.match("12"))
        self.assertFalse(regex.match("1234"))
        self.assertFalse(regex.match("1"))

        regex = re.compile("^{}$".format(
            RegularExpressionVisitor.OnString(
                StringTypeInfo(validation_expression=".est"))[0]))
        self.assertTrue(regex.match("test"))
        self.assertTrue(regex.match("Test"))
        self.assertTrue(regex.match("jest"))
        self.assertFalse(regex.match("tEst"))
 def test_Float(self):
     self.assertFalse(
         re.match(
             "^{}$".format(
                 RegularExpressionVisitor.OnFloat(FloatTypeInfo(min=0))[0]),
             "-3.0"))
     self.assertFalse(
         re.match(
             "^{}$".format(
                 RegularExpressionVisitor.OnFloat(FloatTypeInfo(max=0))[0]),
             "3.0"))
     self.assertFalse(
         re.match(
             "^{}$".format(
                 RegularExpressionVisitor.OnFloat(
                     FloatTypeInfo(min=0, max=10))[0]), "130.01234"))
 def test_Int(self):
     self.assertFalse(
         re.match(
             "^{}$".format(
                 RegularExpressionVisitor.OnInt(IntTypeInfo(min=0))[0]),
             "-3"))
     self.assertFalse(
         re.match(
             "^{}$".format(
                 RegularExpressionVisitor.OnInt(IntTypeInfo(max=0))[0]),
             "3"))
     self.assertFalse(
         re.match(
             "^{}$".format(
                 RegularExpressionVisitor.OnInt(IntTypeInfo(min=0,
                                                            max=9))[0]),
             "130"))
 def OnUri(type_info):
     return {
         "type":
         "string",
         "pattern":
         "^{}$".format(
             RegularExpression.PythonToJavaScript(
                 RegularExpressionVisitor().Accept(type_info)[0]))
     }
示例#5
0
 def OnGuid(type_info):
     return textwrap.dedent(
         """\
         <xsd:restriction base="xsd:string">
           <xsd:pattern value="{}" />
         </xsd:restriction>
         """, ).format(
             RegularExpression.PythonToJavaScript(
                 RegularExpressionVisitor().Accept(type_info)[0]))
    def test_Standard(self):
        # ----------------------------------------------------------------------
        def Test(method, to_match, regex_index=0):
            if not isinstance(to_match, list):
                to_match = [
                    to_match,
                ]

            regex_string = method()[regex_index]

            if isinstance(regex_string, tuple):
                regex_string, regex_options = regex_string
            else:
                regex_options = 0

            regex = re.compile("^{}$".format(regex_string), regex_options)

            for index, query in enumerate(to_match):
                self.assertTrue(
                    regex.match(query),
                    "{} did not match {} ({})".format(query, regex_string,
                                                      index))

        # ----------------------------------------------------------------------

        Test(lambda: RegularExpressionVisitor.OnBool(None), [
            "True",
            "T",
            "t",
            "Yes",
            "yes",
            "Y",
            "y",
            "1",
            "False",
            "false",
            "F",
            "f",
            "No",
            "no",
            "N",
            "n",
            "0",
        ])
        Test(lambda: RegularExpressionVisitor.OnDateTime(None), [
            "2018-04-26 22:29:00",
            "2018-04-26T22:29:00",
            "2018-04-26T22:29:00Z",
            "2018-04-26T22:29:00+08:00",
            "2018-04-26T22:29:00-10:34",
        ])
        Test(lambda: RegularExpressionVisitor.OnDate(None), [
            "2018-04-26",
            "2018/04/26",
            "2018.04.26",
        ],
             regex_index=0)
        Test(lambda: RegularExpressionVisitor.OnDate(None), [
            "04-26-2018",
            "04/26/2018",
            "04.26.2018",
        ],
             regex_index=1)
        Test(lambda: RegularExpressionVisitor.OnDate(None), [
            "18-04-26",
            "18/04/26",
            "18.04.26",
        ],
             regex_index=2)
        Test(lambda: RegularExpressionVisitor.OnDate(None), [
            "04-26-18",
            "04/26/18",
            "04.26.18",
        ],
             regex_index=3)
        Test(lambda: RegularExpressionVisitor.OnDirectory(None), "anything")
        Test(lambda: RegularExpressionVisitor.OnDuration(None), [
            "1.0:00:00.0",
            "1:0:00:00.0",
            "1.00:00:00",
            "1:00:00.0",
            "23:22:21",
            "23:22:21.20",
        ])
        Test(
            lambda: RegularExpressionVisitor.OnEnum(
                EnumTypeInfo([
                    "one",
                    "two",
                ])), [
                    "one",
                    "two",
                ])
        Test(lambda: RegularExpressionVisitor.OnFilename(None), "anything")
        Test(lambda: RegularExpressionVisitor.OnFloat(FloatTypeInfo()), [
            "0.0",
            "10.2",
            "-3.14",
        ])
        Test(lambda: RegularExpressionVisitor.OnGuid(None),
             "{54465641-ADF2-43B1-98EB-66BBD208622C}",
             regex_index=0)
        Test(lambda: RegularExpressionVisitor.OnGuid(None),
             "54465641-ADF2-43B1-98EB-66BBD208622C",
             regex_index=1)
        Test(lambda: RegularExpressionVisitor.OnGuid(None),
             "{54465641ADF243B198EB66BBD208622C}",
             regex_index=2)
        Test(lambda: RegularExpressionVisitor.OnGuid(None),
             "54465641ADF243B198EB66BBD208622C",
             regex_index=3)
        Test(lambda: RegularExpressionVisitor.OnInt(IntTypeInfo()), [
            "-10",
            "10",
            "0",
            "20",
        ])
        Test(lambda: RegularExpressionVisitor.OnString(StringTypeInfo()), [
            "test",
            "again",
            "and another",
        ])
        Test(lambda: RegularExpressionVisitor.OnTime(None), [
            "11:22:33",
            "11:22:33.44",
            "11:22:33",
        ])
        Test(lambda: RegularExpressionVisitor.OnUri(None),
             "http://one.two.three")