Ejemplo n.º 1
0
    def test_float(self):

        f = Float32()

        # check valid float
        f.check(3.14)

        # check non-float
        with self.assertRaises(ValueError) as cm:
            f.check('1.1')
        self.assertIn('not a valid real', cm.exception.args[0])

        f = Float64(min_value=0, max_value=100)
        with self.assertRaises(ValueError) as cm:
            f.check(101)
        self.assertIn('is greater than', cm.exception.args[0])

        with self.assertRaises(ParameterError) as cm:
            Float64(min_value=0, max_value=10**330)
        self.assertIn('too large for a float', cm.exception.args[0])

        with self.assertRaises(ParameterError) as cm:
            Float32(min_value=0, max_value=10**50)
        self.assertIn('greater than the maximum value', cm.exception.args[0])

        # check that bad ranges are rejected
        self.assertRaises(ParameterError, lambda: Float64(min_value=1j))
        self.assertRaises(ParameterError, lambda: Float64(max_value='10'))
Ejemplo n.º 2
0
def _make_namespace_with_a_union():
    # type: (...) -> ApiNamespace
    ns = ApiNamespace('ns_with_a_union')

    u1 = Union(name='Union', namespace=ns, ast_node=None, closed=True)
    u1.set_attributes(
        doc=None,
        fields=[
            UnionField(name="first", doc=None, data_type=Void(),
                       ast_node=None),
            UnionField(name="last", doc=None, data_type=Void(), ast_node=None),
        ],
    )
    ns.add_data_type(u1)

    # A more interesting case with non-void variants.
    shape_union = Union(name='Shape', namespace=ns, ast_node=None, closed=True)
    shape_union.set_attributes(
        doc=None,
        fields=[
            UnionField(name="point", doc=None, data_type=Void(),
                       ast_node=None),
            UnionField(name="circle",
                       doc=None,
                       data_type=Float64(),
                       ast_node=None),
        ],
    )
    ns.add_data_type(shape_union)

    return ns