def test_extract_format_spec__with_zero_and_type(self):
     formats = ["0s", "0d", "0Number", "0Number+"]
     for format in formats:
         format_spec = Field.extract_format_spec(format)
         expected_type = format[1:]
         expected_spec = make_format_spec(type=expected_type, zero=True)
         self.assertEqual(format_spec, expected_spec)
예제 #2
0
 def test_extract_format_spec__with_zero_and_type(self):
     formats = ["0s", "0d", "0Number", "0Number+"]
     for format in formats:
         format_spec = Field.extract_format_spec(format)
         expected_type  = format[1:]
         expected_spec = make_format_spec(type=expected_type, zero=True)
         self.assertEqual(format_spec, expected_spec)
 def test_extract_format_spec__with_width_and_type(self):
     formats = ["1s", "2d", "6s", "10d", "60f", "123456789s"]
     for format in formats:
         format_spec = Field.extract_format_spec(format)
         expected_type = format[-1]
         expected_width = format[:-1]
         expected_spec = make_format_spec(type=expected_type,
                                          width=expected_width)
         self.assertEqual(format_spec, expected_spec)
         self.assertValidFormatWidth(format_spec.width)
예제 #4
0
 def test_extract_format_spec__with_width_and_type(self):
     formats = ["1s", "2d", "6s", "10d", "60f", "123456789s"]
     for format in formats:
         format_spec = Field.extract_format_spec(format)
         expected_type  = format[-1]
         expected_width = format[:-1]
         expected_spec = make_format_spec(type=expected_type,
                                          width=expected_width)
         self.assertEqual(format_spec, expected_spec)
         self.assertValidFormatWidth(format_spec.width)
 def test_extract_format_spec__with_align_and_type(self):
     # -- ALIGN_CHARS = "<>=^"
     formats = ["<s", ">d", "=Number", "^Number+"]
     for format in formats:
         format_spec = Field.extract_format_spec(format)
         expected_align = format[0]
         expected_type = format[1:]
         expected_spec = make_format_spec(type=expected_type,
                                          align=expected_align)
         self.assertEqual(format_spec, expected_spec)
         self.assertValidFormatAlign(format_spec.align)
 def test_extract_format_spec__with_type(self):
     format_types = ["d", "w", "Number", "Number?", "Number*", "Number+"]
     for format_type in format_types:
         format_spec = Field.extract_format_spec(format_type)
         expected_spec = make_format_spec(format_type)
         self.assertEqual(format_spec.type, format_type)
         self.assertEqual(format_spec.width, "")
         self.assertEqual(format_spec.zero, False)
         self.assertIsNone(format_spec.align)
         self.assertIsNone(format_spec.fill)
         self.assertEqual(format_spec, expected_spec)
예제 #7
0
 def test_extract_format_spec__with_align_and_type(self):
     # -- ALIGN_CHARS = "<>=^"
     formats = ["<s", ">d", "=Number", "^Number+"]
     for format in formats:
         format_spec = Field.extract_format_spec(format)
         expected_align = format[0]
         expected_type  = format[1:]
         expected_spec = make_format_spec(type=expected_type,
                                          align=expected_align)
         self.assertEqual(format_spec, expected_spec)
         self.assertValidFormatAlign(format_spec.align)
예제 #8
0
 def test_extract_format_spec__with_type(self):
     format_types = ["d", "w", "Number", "Number?", "Number*", "Number+"]
     for format_type in format_types:
         format_spec = Field.extract_format_spec(format_type)
         expected_spec = make_format_spec(format_type)
         self.assertEqual(format_spec.type, format_type)
         self.assertEqual(format_spec.width, "")
         self.assertEqual(format_spec.zero, False)
         self.assertIsNone(format_spec.align)
         self.assertIsNone(format_spec.fill)
         self.assertEqual(format_spec, expected_spec)
class TestFieldFormatSpec(TestCase):
    """
    Test Field.extract_format_spec().

    FORMAT-SPEC SCHEMA:
    [[fill]align][0][width][type]
    """
    def assertValidFormatWidth(self, width):
        self.assertIsInstance(width, str)
        for char in width:
            self.assertTrue(char.isdigit())

    def assertValidFormatAlign(self, align):
        self.assertIsInstance(align, str)
        self.assertEqual(len(align), 1)
        self.assertIn(align, Field.ALIGN_CHARS)

    def test_extract_format_spec__with_empty_string_raises_error(self):
        with self.assertRaises(ValueError) as cm:
            Field.extract_format_spec("")
        self.assertIn("INVALID-FORMAT", str(cm.exception))

    def test_extract_format_spec__with_type(self):
        format_types = ["d", "w", "Number", "Number?", "Number*", "Number+"]
        for format_type in format_types:
            format_spec = Field.extract_format_spec(format_type)
            expected_spec = make_format_spec(format_type)
            self.assertEqual(format_spec.type, format_type)
            self.assertEqual(format_spec.width, "")
            self.assertEqual(format_spec.zero, False)
            self.assertIsNone(format_spec.align)
            self.assertIsNone(format_spec.fill)
            self.assertEqual(format_spec, expected_spec)

    def test_extract_format_spec_with_width_only_raises_error(self):
        # -- INVALID-FORMAT: Width without type.
        with self.assertRaises(ValueError) as cm:
            Field.extract_format_spec("123")
        self.assertEqual("INVALID-FORMAT: 123 (without type)",
                         str(cm.exception))

    def test_extract_format_spec__with_zero_only_raises_error(self):
        # -- INVALID-FORMAT: Width without type.
        with self.assertRaises(ValueError) as cm:
            Field.extract_format_spec("0")
        self.assertEqual("INVALID-FORMAT: 0 (without type)", str(cm.exception))

    def test_extract_format_spec__with_align_only_raises_error(self):
        # -- INVALID-FORMAT: Width without type.
        for align in Field.ALIGN_CHARS:
            with self.assertRaises(ValueError) as cm:
                Field.extract_format_spec(align)
            self.assertEqual("INVALID-FORMAT: %s (without type)" % align,
                             str(cm.exception))

    def test_extract_format_spec_with_fill_and_align_only_raises_error(self):
        # -- INVALID-FORMAT: Width without type.
        fill = "_"
        for align in Field.ALIGN_CHARS:
            with self.assertRaises(ValueError) as cm:
                format = fill + align
                Field.extract_format_spec(format)
            self.assertEqual("INVALID-FORMAT: %s (without type)" % format,
                             str(cm.exception))

    def test_extract_format_spec__with_width_and_type(self):
        formats = ["1s", "2d", "6s", "10d", "60f", "123456789s"]
        for format in formats:
            format_spec = Field.extract_format_spec(format)
            expected_type = format[-1]
            expected_width = format[:-1]
            expected_spec = make_format_spec(type=expected_type,
                                             width=expected_width)
            self.assertEqual(format_spec, expected_spec)
            self.assertValidFormatWidth(format_spec.width)

    def test_extract_format_spec__with_zero_and_type(self):
        formats = ["0s", "0d", "0Number", "0Number+"]
        for format in formats:
            format_spec = Field.extract_format_spec(format)
            expected_type = format[1:]
            expected_spec = make_format_spec(type=expected_type, zero=True)
            self.assertEqual(format_spec, expected_spec)

    def test_extract_format_spec__with_align_and_type(self):
        # -- ALIGN_CHARS = "<>=^"
        formats = ["<s", ">d", "=Number", "^Number+"]
        for format in formats:
            format_spec = Field.extract_format_spec(format)
            expected_align = format[0]
            expected_type = format[1:]
            expected_spec = make_format_spec(type=expected_type,
                                             align=expected_align)
            self.assertEqual(format_spec, expected_spec)
            self.assertValidFormatAlign(format_spec.align)

    def test_extract_format_spec__with_fill_align_and_type(self):
        # -- ALIGN_CHARS = "<>=^"
        formats = ["X<s", "_>d", "0=Number", " ^Number+"]
        for format in formats:
            format_spec = Field.extract_format_spec(format)
            expected_fill = format[0]
            expected_align = format[1]
            expected_type = format[2:]
            expected_spec = make_format_spec(type=expected_type,
                                             align=expected_align,
                                             fill=expected_fill)
            self.assertEqual(format_spec, expected_spec)
            self.assertValidFormatAlign(format_spec.align)

    # -- ALIGN_CHARS = "<>=^"
    FORMAT_AND_FORMAT_SPEC_DATA = [
        ("^010Number+",
         make_format_spec(type="Number+",
                          width="10",
                          zero=True,
                          align="^",
                          fill=None)),
        ("X<010Number+",
         make_format_spec(type="Number+",
                          width="10",
                          zero=True,
                          align="<",
                          fill="X")),
        ("_>0098Number?",
         make_format_spec(type="Number?",
                          width="098",
                          zero=True,
                          align=">",
                          fill="_")),
        ("*=129Number*",
         make_format_spec(type="Number*",
                          width="129",
                          zero=False,
                          align="=",
                          fill="*")),
        ("X129Number?",
         make_format_spec(type="X129Number?",
                          width="",
                          zero=False,
                          align=None,
                          fill=None)),
    ]

    def test_extract_format_spec__with_all(self):
        for format, expected_spec in self.FORMAT_AND_FORMAT_SPEC_DATA:
            format_spec = Field.extract_format_spec(format)
            self.assertEqual(format_spec, expected_spec)
            self.assertValidFormatWidth(format_spec.width)
            if format_spec.align is not None:
                self.assertValidFormatAlign(format_spec.align)

    def test_make_format(self):
        for expected_format, format_spec in self.FORMAT_AND_FORMAT_SPEC_DATA:
            format = Field.make_format(format_spec)
            self.assertEqual(format, expected_format)
            format_spec2 = Field.extract_format_spec(format)
            self.assertEqual(format_spec2, format_spec)
 def test_not_equal__with_unsupported(self):
     UNSUPPORTED_TYPES = [None, make_format_spec(), True, False, 10]
     field = Field()
     for other in UNSUPPORTED_TYPES:
         with self.assertRaises(ValueError):
             field != other
예제 #11
0
 def test_not_equal__with_unsupported(self):
     UNSUPPORTED_TYPES =  [None, make_format_spec(), True, False, 10]
     field = Field()
     for other in UNSUPPORTED_TYPES:
         with self.assertRaises(ValueError):
             field != other