Ejemplo n.º 1
0
    def testToOneOf(self):
        """Test the ToOneOf converter."""
        cvtList = [
            cvt.Converter(cvt.toType, float),
            cvt.Converter(cvt.toType, str),
        ]
        converter = cvt.toOneOf

        s1 = "test"
        f1 = "1234"

        rs = s1
        rf = float(f1)

        t = converter(s1, cvtList)
        self.assertEqual(rs, t, "Incorrect conversion of a string.")

        t = converter(f1, cvtList)
        self.assertEqual(rf, t, "Incorrect conversion of an Epoch.")

        self.assertRaises(Exception,
                          converter, [1, 2],
                          cvtList,
                          name='value',
                          msg="List argument should be an error.")
Ejemplo n.º 2
0
    def testToListOf(self):
        """Test the ToListOf converter."""
        cvtType = cvt.Converter(cvt.toType, float)
        converter = cvt.toListOf

        l1 = ["123", 1234.0, 5]
        right = map(float, l1)

        v = converter(l1, cvtType)
        self.assertEqual(right, v, "Incorrect conversion of list.")

        v = converter(right, cvtType)
        self.assertEqual(right, v, "Incorrect conversion of floats.")

        v = converter(tuple(l1), cvtType)
        self.assertEqual(right, v, "Incorrect conversion of tuples.")

        self.assertRaises(Exception,
                          converter, ["foo bar"],
                          cvtType,
                          name='value',
                          msg="Invalid string argument should be an error.")
        self.assertRaises(Exception,
                          converter,
                          "foo bar",
                          cvtType,
                          name='value',
                          msg="Invalid non-list argument should be an error.")
Ejemplo n.º 3
0
    def testAllowNone(self):
        """None item promotion"""
        cvtType = cvt.Converter(cvt.toType, float)
        converter = cvt.toListOf

        v = converter(None, cvtType, allowNone=True)
        self.assertEqual(None, v, "Incorrect conversion of none item.")
Ejemplo n.º 4
0
    def testAllowOne(self):
        """Single item promotion"""
        cvtType = cvt.Converter(cvt.toType, float)
        converter = cvt.toListOf

        l1 = ["123"]
        right = map(float, l1)

        v = converter(l1[0], cvtType, allowOne=True)
        self.assertEqual(right, v, "Incorrect conversion of single item.")
Ejemplo n.º 5
0
class MySubSubSubStyle( S.types.SubStyle ):
   """A Sub-Classed Style."""

   prop = S.types.StyleProperty( default = 0.0, validator = \
                                 cvt.Converter( cvt.toType, float,
                                                allowNone=True ) )

   value = S.types.StyleProperty( default = None )

   #-----------------------------------------------------------------------
   def __init__( self, **kwargs ):
      S.types.SubStyle.__init__( self, **kwargs )
Ejemplo n.º 6
0
    def testAllowNone(self):
        """Test allow none."""

        converter = cvt.toExactListOf
        cvtList = [cvt.Converter(cvt.toType, float)]

        result = converter(None, cvtList, allowNone=True)
        self.assertEqual(None, result, "Incorrect conversion of none.")

        s1 = "123"
        right = [123]

        result = converter([s1], cvtList, allowNone=True)
        self.assertEqual(right, result,
                         "Incorrect conversion with allow none.")
Ejemplo n.º 7
0
    def testToExactListOf(self):
        """Test the ToExactListOf converter."""

        # Test the case with only one element.
        converter = cvt.toExactListOf
        cvtList = [cvt.Converter(cvt.toType, float, allowNone=False)]

        d1 = "10"
        right = [10]

        result = converter([d1], cvtList)
        self.assertEqual(right, result,
                         "Incorrect conversion of list with one element.")

        self.assertRaises(Exception,
                          converter,
                          d1,
                          cvtList,
                          name='value',
                          msg="Use non-list argument should be an error.")
        self.assertRaises(Exception,
                          converter, [d1, d1],
                          cvtList,
                          name='value',
                          msg="Use list with wrong size should be an error.")
        self.assertRaises(Exception,
                          converter, ["foo bar"],
                          cvtList,
                          name='value',
                          msg="Use list with wrong type should be an error.")
        self.assertRaises(
            Exception,
            converter,
            None,
            cvtList,
            name='value',
            msg="Use None with AllowNone=False should be an error.")

        # Test the case with multiple elements.
        cvtList = [
            cvt.Converter(cvt.toType, float),
            cvt.Converter(cvt.toType, str)
        ]

        s1 = "test"
        f1 = "123"
        right = [123.0, "test"]

        result = converter([f1, s1], cvtList)
        self.assertEqual(
            right, result,
            "Incorrect conversion of list with multiple elements.")

        result = converter((f1, s1), cvtList)
        self.assertEqual(right, result, "Incorrect conversion of tuple.")

        self.assertRaises(Exception,
                          converter,
                          s1,
                          cvtList,
                          msg="Use non-list argument should be an error.")
        self.assertRaises(Exception,
                          converter, [s1],
                          cvtList,
                          msg="Use list with wrong size should be an error.")
        self.assertRaises(Exception,
                          converter, [f1, s1],
                          cvtList,
                          msg="Use list with wrong order should be an error.")
        self.assertRaises(
            Exception,
            converter,
            None,
            cvtList,
            msg="Use None with AllowNone=False should be an error.")