示例#1
0
    def test_runtime_checks(self) -> None:
        x = Runtime()
        with self.assertRaises(TypeError):
            # pyre-fixme[6]: Expected `Union[None, bool, thrift.py3.types.__NotSet]`
            #  for 1st param but got `int`.
            x(bool_val=5)

        with self.assertRaises(TypeError):
            # pyre-fixme[6]: Expected `Optional[bool]` for 1st param but got `int`.
            Runtime(bool_val=5)

        with self.assertRaises(TypeError):
            # pyre-fixme[6]: Expected `Union[None, Color,
            #  thrift.py3.types.__NotSet]` for 1st param but got `int`.
            x(enum_val=2)

        with self.assertRaises(TypeError):
            # pyre-fixme[6]: Expected `Optional[Color]` for 1st param but got `int`.
            Runtime(enum_val=2)

        with self.assertRaises(TypeError):
            # pyre-fixme[6]: Expected `Union[None, typing.Sequence[int],
            #  thrift.py3.types.__NotSet]` for 1st param but got `List[str]`.
            x(int_list_val=["foo", "bar", "baz"])

        with self.assertRaises(TypeError):
            # pyre-fixme[6]: Expected `Optional[typing.Sequence[int]]` for 1st param
            #  but got `List[str]`.
            Runtime(int_list_val=["foo", "bar", "baz"])
示例#2
0
    def test_struct_fields(self) -> None:
        r = inspect(Messy)

        self.assertEqual(len(r.fields), 4)

        self.assertEqual(r.fields[0].name, "opt_field")
        self.assertEqual(r.fields[0].type, str)
        self.assertEqual(r.fields[0].qualifier, Qualifier.OPTIONAL)
        self.assertEqual(r.fields[0].default, None)
        self.assertEqual(
            r.fields[0].annotations, {"some": "annotation", "a.b.c": "d.e.f"}
        )

        self.assertEqual(r.fields[1].name, "req_field")
        self.assertEqual(r.fields[1].type, str)
        self.assertEqual(r.fields[1].qualifier, Qualifier.REQUIRED)
        self.assertEqual(r.fields[1].default, None)
        self.assertEqual(r.fields[1].annotations, {})

        self.assertEqual(r.fields[2].name, "unq_field")
        self.assertEqual(r.fields[2].type, str)
        self.assertEqual(r.fields[2].qualifier, Qualifier.NONE)
        self.assertEqual(r.fields[2].default, "xyzzy")
        self.assertEqual(r.fields[2].annotations, {})

        self.assertEqual(r.fields[3].name, "struct_field")
        self.assertEqual(r.fields[3].type, Runtime)
        self.assertEqual(r.fields[3].qualifier, Qualifier.NONE)
        self.assertEqual(
            r.fields[3].default,
            Runtime(bool_val=True, enum_val=Color.blue, int_list_val=[10, 20, 30]),
        )
        self.assertEqual(r.fields[3].annotations, {})
示例#3
0
 def test_ordering(self) -> None:
     x = Runtime(bool_val=False, enum_val=Color.red, int_list_val=[64, 128])
     y = x(bool_val=True)
     self.assertLess(x, y)
     self.assertLessEqual(x, y)
     self.assertGreater(y, x)
     self.assertGreaterEqual(y, x)
     self.assertEquals([x, y], sorted([y, x]))
示例#4
0
    def test_runtime_checks(self) -> None:
        x = Runtime()
        with self.assertRaises(TypeError):
            x(bool_val=5)  # type: ignore

        with self.assertRaises(TypeError):
            Runtime(bool_val=5)  # type: ignore

        with self.assertRaises(TypeError):
            x(enum_val=2)  # type: ignore

        with self.assertRaises(TypeError):
            Runtime(enum_val=2)  # type: ignore

        with self.assertRaises(TypeError):
            x(int_list_val=['foo', 'bar', 'baz'])  # type: ignore

        with self.assertRaises(TypeError):
            Runtime(int_list_val=['foo', 'bar', 'baz'])  # type: ignore
示例#5
0
    def test_struct_fields(self) -> None:
        r = inspect(Messy)

        self.assertEqual(len(r.fields), 3)

        self.assertEqual(r.fields[0].id, 1)
        self.assertEqual(r.fields[0].name, "opt_field")
        self.assertEqual(r.fields[0].type, str)
        self.assertEqual(r.fields[0].kind, NumberType.NOT_A_NUMBER)
        self.assertEqual(r.fields[0].qualifier, Qualifier.OPTIONAL)
        self.assertEqual(r.fields[0].default, None)
        self.assertEqual(r.fields[0].annotations, {
            "some": "annotation",
            "a.b.c": "d.e.f"
        })

        self.assertEqual(r.fields[1].id, 3)
        self.assertEqual(r.fields[1].name, "unq_field")
        self.assertEqual(r.fields[1].type, str)
        self.assertEqual(r.fields[1].kind, NumberType.NOT_A_NUMBER)
        self.assertEqual(r.fields[1].qualifier, Qualifier.UNQUALIFIED)
        self.assertEqual(r.fields[1].default, "xyzzy")
        self.assertEqual(r.fields[1].annotations, {})

        self.assertEqual(r.fields[2].id, 4)
        self.assertEqual(r.fields[2].name, "struct_field")
        self.assertEqual(r.fields[2].type, Runtime)
        self.assertEqual(r.fields[2].kind, NumberType.NOT_A_NUMBER)
        self.assertEqual(r.fields[2].qualifier, Qualifier.UNQUALIFIED)
        self.assertEqual(
            r.fields[2].default,
            Runtime(bool_val=True,
                    enum_val=Color.blue,
                    int_list_val=[10, 20, 30]),
        )
        self.assertEqual(r.fields[2].annotations, {})