Пример #1
0
    def testBasicFormat(self):
        """ test basic formatter """
        f = QgsBasicNumericFormat()
        context = QgsNumericFormatContext()
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5, context), '5')
        self.assertEqual(f.formatDouble(5.5, context), '5.5')
        self.assertEqual(f.formatDouble(-5, context), '-5')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55,555,555.5')
        context.setDecimalSeparator('☕')
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(-5.5, context), '-5☕5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55,555,555☕5')
        context.setThousandsSeparator('⚡')
        self.assertEqual(f.formatDouble(-5.5, context), '-5☕5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55⚡555⚡555☕5')
        f.setShowThousandsSeparator(False)
        self.assertEqual(f.formatDouble(-5.5, context), '-5☕5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555☕5')
        context.setDecimalSeparator('.')
        f.setDecimalSeparator('⛹')
        self.assertEqual(f.formatDouble(-5.5, context), '-5⛹5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555⛹5')
        f.setNumberDecimalPlaces(0)
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5.5, context), '6')
        self.assertEqual(f.formatDouble(55555555.5, context), '55555556')
        self.assertEqual(f.formatDouble(55555555.123456, context), '55555555')
        self.assertEqual(f.formatDouble(-5.5, context), '-6')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555556')
        f.setNumberDecimalPlaces(3)
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5.5, context), '5⛹5')
        self.assertEqual(f.formatDouble(55555555.5, context), '55555555⛹5')
        self.assertEqual(f.formatDouble(55555555.123456, context),
                         '55555555⛹123')
        self.assertEqual(f.formatDouble(-5.5, context), '-5⛹5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555⛹5')
        f.setShowTrailingZeros(True)
        self.assertEqual(f.formatDouble(0, context), '0⛹000')
        self.assertEqual(f.formatDouble(5, context), '5⛹000')
        self.assertEqual(f.formatDouble(-5, context), '-5⛹000')
        self.assertEqual(f.formatDouble(5.5, context), '5⛹500')
        self.assertEqual(f.formatDouble(55555555.5, context), '55555555⛹500')
        self.assertEqual(f.formatDouble(55555555.123456, context),
                         '55555555⛹123')
        self.assertEqual(f.formatDouble(-5.5, context), '-5⛹500')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555⛹500')
        f.setShowPlusSign(True)
        self.assertEqual(f.formatDouble(0, context), '0⛹000')
        self.assertEqual(f.formatDouble(5, context), '+5⛹000')
        self.assertEqual(f.formatDouble(-5, context), '-5⛹000')
        self.assertEqual(f.formatDouble(5.5, context), '+5⛹500')
        self.assertEqual(f.formatDouble(55555555.5, context), '+55555555⛹500')
        self.assertEqual(f.formatDouble(55555555.123456, context),
                         '+55555555⛹123')
        self.assertEqual(f.formatDouble(-5.5, context), '-5⛹500')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555⛹500')
        context.setPositiveSign('w')
        self.assertEqual(f.formatDouble(5, context), 'w5⛹000')
        self.assertEqual(f.formatDouble(-5, context), '-5⛹000')
        self.assertEqual(f.formatDouble(5.5, context), 'w5⛹500')

        f.setShowPlusSign(False)
        f.setRoundingType(QgsBasicNumericFormat.SignificantFigures)
        self.assertEqual(f.formatDouble(0, context), '0⛹00')
        self.assertEqual(f.formatDouble(5, context), '5⛹00')
        self.assertEqual(f.formatDouble(-5, context), '-5⛹00')
        self.assertEqual(f.formatDouble(5.5, context), '5⛹50')
        self.assertEqual(f.formatDouble(1231.23123123123123, context), '1230')
        self.assertEqual(f.formatDouble(123.123123123123123, context), '123')
        self.assertEqual(f.formatDouble(12.3123123123123123, context), '12⛹3')
        self.assertEqual(f.formatDouble(1.23123123123123123, context), '1⛹23')
        self.assertEqual(f.formatDouble(-1231.23123123123123, context),
                         '-1230')
        self.assertEqual(f.formatDouble(-123.123123123123123, context), '-123')
        self.assertEqual(f.formatDouble(-12.3123123123123123, context),
                         '-12⛹3')
        self.assertEqual(f.formatDouble(-1.23123123123123123, context),
                         '-1⛹23')
        self.assertEqual(f.formatDouble(100, context), '100')
        self.assertEqual(f.formatDouble(1000, context), '1000')
        self.assertEqual(f.formatDouble(1001, context), '1000')
        self.assertEqual(f.formatDouble(9999, context), '10000')
        self.assertEqual(f.formatDouble(10, context), '10⛹0')
        self.assertEqual(f.formatDouble(1, context), '1⛹00')
        self.assertEqual(f.formatDouble(0.00000123456, context), '0⛹00000123')
        self.assertEqual(f.formatDouble(55555555.5, context), '55600000')
        self.assertEqual(f.formatDouble(55555555.123456, context), '55600000')
        self.assertEqual(f.formatDouble(-5.5, context), '-5⛹50')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55600000')

        f.setThousandsSeparator('✅')
        f.setShowThousandsSeparator(True)
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55✅600✅000')
        f.setShowThousandsSeparator(False)

        f.setShowPlusSign(True)

        f2 = f.clone()
        self.assertIsInstance(f2, QgsBasicNumericFormat)

        self.assertEqual(f2.showTrailingZeros(), f.showTrailingZeros())
        self.assertEqual(f2.showPlusSign(), f.showPlusSign())
        self.assertEqual(f2.numberDecimalPlaces(), f.numberDecimalPlaces())
        self.assertEqual(f2.showThousandsSeparator(),
                         f.showThousandsSeparator())
        self.assertEqual(f2.roundingType(), f.roundingType())
        self.assertEqual(f2.thousandsSeparator(), f.thousandsSeparator())
        self.assertEqual(f2.decimalSeparator(), f.decimalSeparator())

        doc = QDomDocument("testdoc")
        elem = doc.createElement("test")
        f2.writeXml(elem, doc, QgsReadWriteContext())

        f3 = QgsNumericFormatRegistry().createFromXml(elem,
                                                      QgsReadWriteContext())
        self.assertIsInstance(f3, QgsBasicNumericFormat)

        self.assertEqual(f3.showTrailingZeros(), f.showTrailingZeros())
        self.assertEqual(f3.showPlusSign(), f.showPlusSign())
        self.assertEqual(f3.numberDecimalPlaces(), f.numberDecimalPlaces())
        self.assertEqual(f3.showThousandsSeparator(),
                         f.showThousandsSeparator())
        self.assertEqual(f3.roundingType(), f.roundingType())
        self.assertEqual(f3.thousandsSeparator(), f.thousandsSeparator())
        self.assertEqual(f3.decimalSeparator(), f.decimalSeparator())
Пример #2
0
    def testFractionFormat(self):
        """ test fraction formatter """
        f = QgsFractionNumericFormat()
        f.setUseUnicodeSuperSubscript(False)
        context = QgsNumericFormatContext()
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5, context), '5')
        self.assertEqual(f.formatDouble(5.5, context), '5 1/2')
        self.assertEqual(f.formatDouble(-5, context), '-5')
        self.assertEqual(f.formatDouble(-5.5, context), '-5 1/2')
        self.assertEqual(f.formatDouble(-55555555.5, context),
                         '-55,555,555 1/2')
        context.setThousandsSeparator('⚡')
        self.assertEqual(f.formatDouble(-55555555.5, context),
                         '-55⚡555⚡555 1/2')
        f.setShowThousandsSeparator(False)
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555 1/2')
        f.setShowPlusSign(True)
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5, context), '+5')
        self.assertEqual(f.formatDouble(-5, context), '-5')
        self.assertEqual(f.formatDouble(5.5, context), '+5 1/2')
        self.assertEqual(f.formatDouble(-5.5, context), '-5 1/2')
        self.assertEqual(f.formatDouble(55555555.5, context), '+55555555 1/2')
        self.assertEqual(f.formatDouble(55555555.123456, context),
                         '+55555555 5797/46956')
        self.assertEqual(f.formatDouble(-5.5, context), '-5 1/2')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555 1/2')
        context.setPositiveSign('w')
        self.assertEqual(f.formatDouble(5, context), 'w5')
        self.assertEqual(f.formatDouble(-5, context), '-5')
        self.assertEqual(f.formatDouble(5.5, context), 'w5 1/2')

        f.setShowPlusSign(False)
        f.setUseDedicatedUnicodeCharacters(True)
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5, context), '5')
        self.assertEqual(f.formatDouble(5.5, context), '5 ½')
        self.assertEqual(f.formatDouble(-5, context), '-5')
        self.assertEqual(f.formatDouble(-5.5, context), '-5 ½')
        self.assertEqual(
            f.formatDouble(5.333333333333333333333333333, context), '5 ⅓')
        self.assertEqual(
            f.formatDouble(5.666666666666666666666666666, context), '5 ⅔')
        self.assertEqual(f.formatDouble(5.25, context), '5 ¼')
        self.assertEqual(f.formatDouble(5.75, context), '5 ¾')
        self.assertEqual(f.formatDouble(5.2, context), '5 ⅕')
        self.assertEqual(f.formatDouble(5.4, context), '5 ⅖')
        self.assertEqual(f.formatDouble(5.6, context), '5 ⅗')
        self.assertEqual(f.formatDouble(5.8, context), '5 ⅘')
        self.assertEqual(
            f.formatDouble(5.1666666666666666666666666666666666, context),
            '5 ⅙')
        self.assertEqual(
            f.formatDouble(5.8333333333333333333333333333333333, context),
            '5 ⅚')
        self.assertEqual(f.formatDouble(5.14285714285714285, context), '5 ⅐')
        self.assertEqual(f.formatDouble(5.125, context), '5 ⅛')
        self.assertEqual(f.formatDouble(5.375, context), '5 ⅜')
        self.assertEqual(f.formatDouble(5.625, context), '5 ⅝')
        self.assertEqual(f.formatDouble(5.875, context), '5 ⅞')
        self.assertEqual(f.formatDouble(5.1111111111111111, context), '5 ⅑')
        self.assertEqual(f.formatDouble(5.1, context), '5 ⅒')
        self.assertEqual(f.formatDouble(5.13131313133, context), '5 13/99')

        f.setUseUnicodeSuperSubscript(True)
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5, context), '5')
        self.assertEqual(f.formatDouble(5.5, context), '5 ½')
        self.assertEqual(f.formatDouble(-5, context), '-5')
        self.assertEqual(f.formatDouble(-5.5, context), '-5 ½')
        self.assertEqual(f.formatDouble(5.55555555, context),
                         '5 ¹¹¹¹¹¹¹¹/₂₀₀₀₀₀₀₀')
        self.assertEqual(f.formatDouble(-5.55555555, context),
                         '-5 ¹¹¹¹¹¹¹¹/₂₀₀₀₀₀₀₀')
        self.assertEqual(f.formatDouble(0.555, context), '¹¹¹/₂₀₀')

        f.setShowPlusSign(True)
        f.setUseUnicodeSuperSubscript(False)

        f2 = f.clone()
        self.assertIsInstance(f2, QgsFractionNumericFormat)

        self.assertEqual(f2.showPlusSign(), f.showPlusSign())
        self.assertEqual(f2.showThousandsSeparator(),
                         f.showThousandsSeparator())
        self.assertEqual(f2.thousandsSeparator(), f.thousandsSeparator())
        self.assertEqual(f2.useDedicatedUnicodeCharacters(),
                         f.useDedicatedUnicodeCharacters())
        self.assertEqual(f2.useUnicodeSuperSubscript(),
                         f.useUnicodeSuperSubscript())

        doc = QDomDocument("testdoc")
        elem = doc.createElement("test")
        f2.writeXml(elem, doc, QgsReadWriteContext())

        f3 = QgsNumericFormatRegistry().createFromXml(elem,
                                                      QgsReadWriteContext())
        self.assertIsInstance(f3, QgsFractionNumericFormat)

        self.assertEqual(f3.showPlusSign(), f.showPlusSign())
        self.assertEqual(f3.showThousandsSeparator(),
                         f.showThousandsSeparator())
        self.assertEqual(f3.thousandsSeparator(), f.thousandsSeparator())
        self.assertEqual(f3.useDedicatedUnicodeCharacters(),
                         f.useDedicatedUnicodeCharacters())
        self.assertEqual(f3.useUnicodeSuperSubscript(),
                         f.useUnicodeSuperSubscript())
Пример #3
0
    def testBasicFormat(self):
        """ test basic formatter """
        f = QgsBasicNumericFormat()
        context = QgsNumericFormatContext()
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5, context), '5')
        self.assertEqual(f.formatDouble(5.5, context), '5.5')
        self.assertEqual(f.formatDouble(-5, context), '-5')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55,555,555.5')
        context.setDecimalSeparator('x')
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(-5.5, context), '-5x5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55,555,555x5')
        context.setThousandsSeparator('y')
        self.assertEqual(f.formatDouble(-5.5, context), '-5x5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55y555y555x5')
        f.setShowThousandsSeparator(False)
        self.assertEqual(f.formatDouble(-5.5, context), '-5x5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555x5')
        context.setDecimalSeparator('.')
        f.setNumberDecimalPlaces(0)
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5.5, context), '6')
        self.assertEqual(f.formatDouble(55555555.5, context), '55555556')
        self.assertEqual(f.formatDouble(55555555.123456, context), '55555555')
        self.assertEqual(f.formatDouble(-5.5, context), '-6')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555556')
        f.setNumberDecimalPlaces(3)
        self.assertEqual(f.formatDouble(0, context), '0')
        self.assertEqual(f.formatDouble(5.5, context), '5.5')
        self.assertEqual(f.formatDouble(55555555.5, context), '55555555.5')
        self.assertEqual(f.formatDouble(55555555.123456, context),
                         '55555555.123')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.5')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555.5')
        f.setShowTrailingZeros(True)
        self.assertEqual(f.formatDouble(0, context), '0.000')
        self.assertEqual(f.formatDouble(5, context), '5.000')
        self.assertEqual(f.formatDouble(-5, context), '-5.000')
        self.assertEqual(f.formatDouble(5.5, context), '5.500')
        self.assertEqual(f.formatDouble(55555555.5, context), '55555555.500')
        self.assertEqual(f.formatDouble(55555555.123456, context),
                         '55555555.123')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.500')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555.500')
        f.setShowPlusSign(True)
        self.assertEqual(f.formatDouble(0, context), '0.000')
        self.assertEqual(f.formatDouble(5, context), '+5.000')
        self.assertEqual(f.formatDouble(-5, context), '-5.000')
        self.assertEqual(f.formatDouble(5.5, context), '+5.500')
        self.assertEqual(f.formatDouble(55555555.5, context), '+55555555.500')
        self.assertEqual(f.formatDouble(55555555.123456, context),
                         '+55555555.123')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.500')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555.500')
        context.setPositiveSign('w')
        self.assertEqual(f.formatDouble(5, context), 'w5.000')
        self.assertEqual(f.formatDouble(-5, context), '-5.000')
        self.assertEqual(f.formatDouble(5.5, context), 'w5.500')

        f2 = f.clone()
        self.assertIsInstance(f2, QgsBasicNumericFormat)

        self.assertEqual(f2.showTrailingZeros(), f.showTrailingZeros())
        self.assertEqual(f2.showPlusSign(), f.showPlusSign())
        self.assertEqual(f2.numberDecimalPlaces(), f.numberDecimalPlaces())
        self.assertEqual(f2.showThousandsSeparator(),
                         f.showThousandsSeparator())

        doc = QDomDocument("testdoc")
        elem = doc.createElement("test")
        f2.writeXml(elem, doc, QgsReadWriteContext())

        f3 = QgsNumericFormatRegistry().createFromXml(elem,
                                                      QgsReadWriteContext())
        self.assertIsInstance(f3, QgsBasicNumericFormat)

        self.assertEqual(f3.showTrailingZeros(), f.showTrailingZeros())
        self.assertEqual(f3.showPlusSign(), f.showPlusSign())
        self.assertEqual(f3.numberDecimalPlaces(), f.numberDecimalPlaces())
        self.assertEqual(f3.showThousandsSeparator(),
                         f.showThousandsSeparator())