Exemple #1
0
 def test_DerivedRef5(self):
     element = self._element.Children[15]
     self.assertEqual(element.Name, "test_derived_ref5")
     self.assertTrue(isinstance(element.TypeInfo, ListTypeInfo))
     self.assertEqual(element.TypeInfo.Arity, Arity(20, 20))
     self._VerifyTestDerived(element.TypeInfo.ElementTypeInfo)
     self.assertEqual(element.TypeInfo.ElementTypeInfo.Arity, Arity(10, 10))
Exemple #2
0
 def test_Standard(self):
     self.assertEqual(
         _Invoke("<foo ?>").items[0].arity, Arity.FromString("?"))
     self.assertEqual(
         _Invoke("<foo *>").items[0].arity, Arity.FromString("*"))
     self.assertEqual(
         _Invoke("<foo +>").items[0].arity, Arity.FromString("+"))
     self.assertEqual(_Invoke("<foo {10}>").items[0].arity, Arity(10, 10))
     self.assertEqual(_Invoke("<foo {5, 20}>").items[0].arity, Arity(5, 20))
Exemple #3
0
    def test_TypeInfo(self):
        self.assertTrue(isinstance(self._element.TypeInfo, ClassTypeInfo))
        self.assertEqual(self._element.TypeInfo.Arity, Arity(1, 1))

        self.assertEqual(
            self._element.TypeInfo,
            ClassTypeInfo(
                {},
                require_exact_match=False,
                arity=Arity(1, 1),
            ),
        )
Exemple #4
0
 def test_TypeInfo(self):
     self.assertTrue(isinstance(self._element.TypeInfo, ClassTypeInfo))
     self.assertEqual(self._element.TypeInfo.Arity, Arity(1, 1))
     self.assertEqual(
         self._element.TypeInfo,
         ClassTypeInfo(
             {
                 "d": StringTypeInfo(),
                 "e": StringTypeInfo()
             },
             require_exact_match=True,
             arity=Arity(1, 1),
         ),
     )
Exemple #5
0
 def test_TypeInfo(self):
     self.assertTrue(self._element.TypeInfo, ClassTypeInfo)
     self.assertEqual(self._element.TypeInfo.Arity, Arity(1, 1))
     self.assertEqual(
         self._element.TypeInfo,
         ClassTypeInfo(
             {
                 "a": IntTypeInfo(),
                 "b": StringTypeInfo(),
                 None: StringTypeInfo()
             },
             require_exact_match=True,
             arity=Arity(1, 1),
         ),
     )
        def visitArityVariable(self, ctx):
            values = self._GetChildValues(ctx)
            assert len(values) == 2, values

            min_value, max_value = values

            if min_value <= 0:
                raise Exceptions.PopulateInvalidArityException(
                    self._source_name,
                    ctx.start.line,
                    ctx.start.column + 1,
                    value=min_value,
                )

            if max_value <= 0:
                raise Exceptions.PopulateInvalidArityException(
                    self._source_name,
                    ctx.start.line,
                    ctx.start.column + 1,
                    value=max_value,
                )

            if max_value < min_value:
                raise Exceptions.PopulateInvalidMaxArityException(
                    self._source_name,
                    ctx.start.line,
                    ctx.start.column + 1,
                    min=min_value,
                    max=max_value,
                )

            self._stack.append(Arity(min_value, max_value))
 def test_Arity(self):
     self.assertEqual(
         SimpleSchemaVisitor.Accept(
             IntTypeInfo(arity=Arity.FromString("*")), None), "<int *>")
     self.assertEqual(
         SimpleSchemaVisitor.Accept(
             IntTypeInfo(arity=Arity.FromString("+")), None), "<int +>")
     self.assertEqual(
         SimpleSchemaVisitor.Accept(
             IntTypeInfo(arity=Arity.FromString("?")), None), "<int ?>")
     self.assertEqual(
         SimpleSchemaVisitor.Accept(IntTypeInfo(arity=Arity(1, 2)), None),
         "<int {1,2}>")
     self.assertEqual(
         SimpleSchemaVisitor.Accept(IntTypeInfo(arity=Arity(2, 2)), None),
         "<int {2}>")
Exemple #8
0
 def test_CRef4(self):
     element = self._element.Children[10]
     self.assertEqual(element.Name, "c_ref4")
     self.assertTrue(isinstance(element, ListElement))
     self.assertEqual(
         element.TypeInfo,
         ListTypeInfo(
             ListTypeInfo(
                 StringTypeInfo(
                     min_length=12,
                     arity="*",
                 ),
                 arity=Arity(5, 5),
             ),
             arity=Arity(10, 10),
         ),
     )
Exemple #9
0
 def test_TypeInfo(self):
     self.assertTrue(self._element.TypeInfo, ClassTypeInfo)
     self.assertEqual(self._element.TypeInfo.Arity, Arity(1, 1))
     self.assertEqual(
         self._element.TypeInfo,
         ClassTypeInfo(
             {
                 "a": StringTypeInfo(arity="*", ),
             },
             require_exact_match=True,
             arity=Arity(1, 1),
         ),
     )
     self.assertEqual(
         self._element.Children[0].TypeInfo,
         StringTypeInfo(arity="*", ),
     )
 def test_ToString(self):
     self.assertEqual(Arity(0, 1).ToString(), "?")
     self.assertEqual(Arity(1, 1).ToString(), "")
     self.assertEqual(Arity(0, None).ToString(), "*")
     self.assertEqual(Arity(1, None).ToString(), "+")
     self.assertEqual(Arity(2, 2).ToString(), "{2}")
     self.assertEqual(Arity(2, 4).ToString(), "{2,4}")
     self.assertEqual(Arity(2, 4).ToString(('[', ']')), "[2,4]")
Exemple #11
0
    def test_TypeInfo(self):
        self.assertTrue(isinstance(self._element.TypeInfo, ClassTypeInfo))
        self.assertEqual(self._element.TypeInfo.Arity, Arity(1, 1))

        self.assertEqual(
            self._element.TypeInfo,
            ClassTypeInfo(
                {
                    "a": IntTypeInfo(),
                    "b": StringTypeInfo(),
                    "c": StringTypeInfo(),
                    "d": StringTypeInfo(),
                    "e": StringTypeInfo(),
                    None: StringTypeInfo(),
                },
                require_exact_match=False,
                arity=Arity(1, 1),
            ),
        )
Exemple #12
0
 def test_CRef2(self):
     element = self._element.Children[7]
     self.assertEqual(element.Name, "c_ref2")
     self.assertTrue(isinstance(element, FundamentalElement))
     self.assertEqual(
         element.TypeInfo,
         StringTypeInfo(
             min_length=12,
             arity=Arity(20, 20),
         ),
     )
 def test_FromString(self):
     self.assertEqual(Arity.FromString('?'), Arity(0, 1))
     self.assertEqual(Arity.FromString('1'), Arity(1, 1))
     self.assertEqual(Arity.FromString('*'), Arity(0, None))
     self.assertEqual(Arity.FromString('+'), Arity(1, None))
     self.assertEqual(Arity.FromString('{3}'), Arity(3, 3))
     self.assertEqual(Arity.FromString('{3,4}'), Arity(3, 4))
Exemple #14
0
def _ResolveArity(plugin, item):
    for sub_item in item.Enumerate(variant_includes_self=True, ):
        if sub_item.arity is not None:
            continue

        if (sub_item.element_type == Elements.ReferenceElement
                and not plugin.Flags & ParseFlag.MaintainReferenceArity):
            assert len(sub_item.references) == 1, sub_item.references

            _ResolveArity(plugin, sub_item.references[0])
            sub_item.arity = sub_item.references[0].arity
        else:
            sub_item.arity = Arity(1, 1)
        def visitArityFixed(self, ctx):
            values = self._GetChildValues(ctx)
            assert len(values) == 1, values

            value = values[0]

            if value <= 0:
                raise Exceptions.PopulateInvalidArityException(
                    self._source_name,
                    ctx.start.line,
                    ctx.start.column + 1,
                    value=value,
                )

            self._stack.append(Arity(value, value))
    def test_IsMethods(self):
        # ----------------------------------------------------------------------
        def Test(
            arity,
            is_single=False,
            is_optional=False,
            is_collection=False,
            is_optional_collection=False,
            is_fixed_collection=False,
            is_zero_or_more=False,
            is_one_or_more=False,
            is_range=False,
        ):
            self.assertEqual(arity.IsSingle, is_single)
            self.assertEqual(arity.IsOptional, is_optional)
            self.assertEqual(arity.IsCollection, is_collection)
            self.assertEqual(arity.IsOptionalCollection,
                             is_optional_collection)
            self.assertEqual(arity.IsFixedCollection, is_fixed_collection)
            self.assertEqual(arity.IsZeroOrMore, is_zero_or_more)
            self.assertEqual(arity.IsOneOrMore, is_one_or_more)
            self.assertEqual(arity.IsRange, is_range)

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

        Test(Arity(1, 1), is_single=True)
        Test(Arity(0, 1), is_optional=True)
        Test(Arity(3, 5), is_collection=True, is_range=True)
        Test(Arity(0, 5),
             is_collection=True,
             is_optional_collection=True,
             is_range=True)
        Test(Arity(0, None),
             is_collection=True,
             is_optional_collection=True,
             is_zero_or_more=True)
        Test(Arity(4, 4), is_collection=True, is_fixed_collection=True)
        Test(Arity(1, 5), is_collection=True, is_range=True)
 def test_LessThan(self):
     self.assertTrue(Arity(1, 1) < 4)
     self.assertTrue(Arity(1, 1000) < Arity(2, 100))
     self.assertTrue(Arity(1, 1000) < Arity(1, None))
     self.assertTrue(Arity(1, 100) < Arity(1, 200))
Exemple #18
0
    def _VerifyTestDerived(
        self,
        type_info,
        verify_derived_refs=True,
    ):
        self.assertTrue(isinstance(type_info, ClassTypeInfo))
        self.assertEqual(
            list(six.iterkeys(type_info.Items)),
            [
                "b",
                "c",
                "v1",
                "v2",
                "v3",
                "c_ref0",
                "c_ref1",
                "c_ref2",
                "c_ref25",
                "c_ref3",
                "c_ref4",
                "test_derived_ref1",
                "test_derived_ref2",
                "test_derived_ref3",
                "test_derived_ref4",
                "test_derived_ref5",
            ],
        )
        self.assertEqual(type_info.Items["b"], BoolTypeInfo())
        self.assertEqual(
            type_info.Items["c"],
            StringTypeInfo(
                min_length=12,
                arity="*",
            ),
        )
        self.assertEqual(
            type_info.Items["v1"],
            AnyOfTypeInfo([
                BoolTypeInfo(),
                StringTypeInfo(),
                IntTypeInfo(min=20, ),
            ], ),
        )
        self.assertEqual(
            type_info.Items["v2"],
            AnyOfTypeInfo([
                BoolTypeInfo(),
                BoolTypeInfo(),
                StringTypeInfo(),
                IntTypeInfo(min=20, ),
            ], ),
        )
        self.assertEqual(
            type_info.Items["v3"],
            AnyOfTypeInfo([
                UriTypeInfo(),
                BoolTypeInfo(),
                BoolTypeInfo(),
                StringTypeInfo(),
                IntTypeInfo(min=20, ),
                FilenameTypeInfo(ensure_exists=True, ),
            ], ),
        )
        self.assertEqual(
            type_info.Items["c_ref0"],
            StringTypeInfo(
                min_length=12,
                arity="*",
            ),
        )
        self.assertEqual(
            type_info.Items["c_ref1"],
            StringTypeInfo(
                min_length=12,
                max_length=20,
                arity="*",
            ),
        )
        self.assertEqual(
            type_info.Items["c_ref2"],
            StringTypeInfo(
                min_length=12,
                arity=Arity(20, 20),
            ),
        )
        self.assertEqual(
            type_info.Items["c_ref25"],
            StringTypeInfo(
                min_length=12,
                max_length=20,
                arity=Arity(20, 20),
            ),
        )
        self.assertEqual(
            type_info.Items["c_ref3"],
            ListTypeInfo(
                StringTypeInfo(
                    min_length=12,
                    arity="*",
                ),
                arity=Arity(5, 5),
            ),
        )
        self.assertEqual(
            type_info.Items["c_ref4"],
            ListTypeInfo(
                ListTypeInfo(
                    StringTypeInfo(
                        min_length=12,
                        arity="*",
                    ),
                    arity=Arity(5, 5),
                ),
                arity=Arity(10, 10),
            ),
        )

        self.assertTrue(
            isinstance(type_info.Items["test_derived_ref1"], ClassTypeInfo))
        self.assertTrue(
            isinstance(type_info.Items["test_derived_ref2"], ClassTypeInfo))
        self.assertTrue(
            isinstance(type_info.Items["test_derived_ref3"], ClassTypeInfo))
        self.assertTrue(
            isinstance(type_info.Items["test_derived_ref4"], ClassTypeInfo))
        self.assertTrue(
            isinstance(type_info.Items["test_derived_ref5"], ListTypeInfo))
        self.assertTrue(
            isinstance(type_info.Items["test_derived_ref5"].ElementTypeInfo,
                       ClassTypeInfo))

        if verify_derived_refs:
            self._VerifyTestDerived(
                type_info.Items["test_derived_ref1"],
                verify_derived_refs=False,
            )
            self._VerifyTestDerived(
                type_info.Items["test_derived_ref2"],
                verify_derived_refs=False,
            )
            self._VerifyTestDerived(
                type_info.Items["test_derived_ref3"],
                verify_derived_refs=False,
            )
            self._VerifyTestDerived(
                type_info.Items["test_derived_ref4"],
                verify_derived_refs=False,
            )
            self._VerifyTestDerived(
                type_info.Items["test_derived_ref5"].ElementTypeInfo,
                verify_derived_refs=False,
            )
 def test_Str(self):
     self.assertEqual(str(Arity(1, 2)), "Arity(1, 2)")
     self.assertEqual(str(Arity(2, 2)), "Arity(2, 2)")
     self.assertEqual(str(Arity(1, None)), "Arity(1, None)")
 def test_InvalidConstruction(self):
     self.assertRaises(Exception, lambda: Arity(None, None))
     self.assertRaises(Exception, lambda: Arity(-1, None))
     self.assertRaises(Exception, lambda: Arity(5, 4))
Exemple #21
0
 def test_DerivedRef4(self):
     element = self._element.Children[14]
     self.assertEqual(element.Name, "test_derived_ref4")
     self._VerifyTestDerived(element.TypeInfo)
     self.assertEqual(element.TypeInfo.Arity, Arity(30, 30))
 def test_Equal(self):
     self.assertEqual(Arity(0, None), Arity.FromString('*'))
     self.assertTrue(Arity(0, None) != Arity.FromString('+'))
Exemple #23
0
    parser = _FromParser()

    result = parser.Pushed_ItemFromJson(item, always_include_optional,
                                        process_additional_data)

    return result


# ----------------------------------------------------------------------
# |
# |  TypeInfo
# |
# ----------------------------------------------------------------------
ChangeInfo_TypeInfo = AnyOfTypeInfo(
    arity=Arity(min=1, max_or_none=1),
    type_info_list=[
        DictTypeInfo(arity=Arity(min=1, max_or_none=1),
                     items={
                         "id":
                         StringTypeInfo(arity=Arity(min=1, max_or_none=1),
                                        min_length=1),
                         "author":
                         StringTypeInfo(arity=Arity(min=1, max_or_none=1),
                                        min_length=1),
                         "date":
                         DateTimeTypeInfo(arity=Arity(min=1, max_or_none=1)),
                         "description":
                         StringTypeInfo(arity=Arity(min=1, max_or_none=1),
                                        min_length=0),
                         "branch":
Exemple #24
0
 def test_TypeInfo(self):
     self._VerifyTestDerived(self._element.TypeInfo)
     self.assertEqual(self._element.TypeInfo.Arity, Arity(1, 1))
 def test_Construct(self):
     self.TeenTypeInfo(Arity(1, 1))
     self.TeenTypeInfo('?')
     self.assertRaises(
         Exception, lambda: self.TeenTypeInfo(
             Arity(1, 1), collection_validation_func=lambda items: None))
Exemple #26
0
 def test_DerivedRef3(self):
     element = self._element.Children[13]
     self.assertEqual(element.Name, "test_derived_ref3")
     self._VerifyTestDerived(element.TypeInfo)
     self.assertEqual(element.TypeInfo.Arity, Arity(10, 10))