示例#1
0
 def test_value_setter(self):
     """Set value appropriately given valid data."""
     qty = Quantity()
     qty.value = 100
     assert qty._numerator == 100
     assert qty._denominator == 1
     assert not qty.is_decimal
     assert qty._float == 100.0
     qty.value = 100
     assert qty._numerator == 100
     assert qty._denominator == 1
     assert not qty.is_decimal
     assert qty._float == 100.0
     qty.value = 3.1415
     assert qty.is_decimal
     assert qty._numerator is None
     assert qty._denominator is None
     assert qty._float == 3.1415
     qty.value = '3.1415'
     assert qty.is_decimal
     assert qty._numerator is None
     assert qty._denominator is None
     assert qty._float == 3.1415
     qty.value = Decimal('3.1415')
     assert qty.is_decimal
     assert qty._numerator is None
     assert qty._denominator is None
     assert qty._float == 3.1415
     qty.value = Fraction(1, 2)
     assert not qty.is_decimal
     assert qty._numerator == 1
     assert qty._denominator == 2
     assert qty._float == 0.5
     qty.value = Fraction(5, 2)
     assert not qty.is_decimal
     assert qty._numerator == 5
     assert qty._denominator == 2
     assert qty._float == 2.5
     qty.value = '3/4'
     assert not qty.is_decimal
     assert qty._numerator == 3
     assert qty._denominator == 4
     assert qty._float == 0.75
     qty.value = '1 1/4'
     assert not qty.is_decimal
     assert qty._numerator == 5
     assert qty._denominator == 4
     assert qty._float == 1.25
     qty.value = None
     assert qty.is_decimal is None
     assert qty._numerator is None
     assert qty._denominator is None
     assert qty._float is None
示例#2
0
 def test_value_getter(self):
     """Return value in appropriate format."""
     qty = Quantity()
     qty.value = '3.1415'
     assert qty.value == 3.1415
     qty.value = Decimal('5.21')
     assert qty.value == 5.21
     qty.value = 100
     assert qty.value == 100
     qty.value = '100'
     assert qty.value == 100
     qty.value = '4/3'
     assert qty.value == Fraction(4, 3)
     qty.value = Fraction(1, 2)
     assert qty.value == Fraction(1, 2)
     qty.value = None
     assert qty.value is None
示例#3
0
    def test_html_value(self):
        """Return HTML entities or special HTML for fractions.

        Return a string of self.value if it is not a fraction.
        """
        qty = Quantity()
        qty.value = Fraction(1, 4)
        assert qty.html_value == '¼'
        qty.value = Fraction(1, 2)
        assert qty.html_value == '½'
        qty.value = Fraction(3, 4)
        assert qty.html_value == '¾'
        qty.value = Fraction(1, 3)
        assert qty.html_value == '⅓'
        qty.value = Fraction(2, 3)
        assert qty.html_value == '⅔'
        qty.value = Fraction(1, 5)
        assert qty.html_value == '⅕'
        qty.value = Fraction(2, 5)
        assert qty.html_value == '⅖'
        qty.value = Fraction(3, 5)
        assert qty.html_value == '⅗'
        qty.value = Fraction(4, 5)
        assert qty.html_value == '⅘'
        qty.value = Fraction(1, 6)
        assert qty.html_value == '⅙'
        qty.value = Fraction(5, 6)
        assert qty.html_value == '⅚'
        qty.value = Fraction(1, 8)
        assert qty.html_value == '⅛'
        qty.value = Fraction(3, 8)
        assert qty.html_value == '⅜'
        qty.value = Fraction(5, 8)
        assert qty.html_value == '⅝'
        qty.value = Fraction(7, 8)
        assert qty.html_value == '⅞'
        qty.value = Fraction(9, 11)
        assert qty.html_value == '<span class="fraction"><sup>9</sup>&frasl;'\
                                 '<sub>11</sub></span>'
        qty.value = Decimal('3.1415')
        assert qty.html_value == '3.1415'
        qty.value = 100
        assert qty.html_value == '100'
        qty.value = '100'
        assert qty.html_value == '100'