示例#1
0
    def testFallbackFormat(self):
        """ test fallback formatter """
        f = QgsFallbackNumericFormat()
        context = QgsNumericFormatContext()
        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')

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

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

        f3 = QgsNumericFormatRegistry().createFromXml(elem,
                                                      QgsReadWriteContext())
        self.assertIsInstance(f3, QgsFallbackNumericFormat)
示例#2
0
    def testRegistry(self):
        registry = QgsNumericFormatRegistry()
        self.assertTrue(registry.formats())
        for f in registry.formats():
            self.assertEqual(registry.format(f).id(), f)

        self.assertIn('default', registry.formats())
        registry.addFormat(TestFormat())
        self.assertIn('test', registry.formats())
        self.assertTrue(isinstance(registry.format('test'), TestFormat))
        self.assertTrue(
            isinstance(registry.create('test', {}, QgsReadWriteContext()),
                       TestFormat))

        registry.removeFormat('test')

        self.assertNotIn('test', registry.formats())
        self.assertTrue(
            isinstance(registry.format('test'), QgsFallbackNumericFormat))
        self.assertTrue(
            isinstance(registry.create('test', {}, QgsReadWriteContext()),
                       QgsFallbackNumericFormat))

        self.assertTrue(
            isinstance(registry.fallbackFormat(), QgsFallbackNumericFormat))

        self.assertEqual(registry.visibleName('default'), 'General')
        self.assertEqual(registry.visibleName('basic'), 'Number')

        self.assertEqual(registry.sortKey('default'), 0)
        self.assertEqual(registry.sortKey('basic'), 1)
        self.assertEqual(registry.sortKey('currency'), 100)
示例#3
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())
示例#4
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())
示例#5
0
    def testScientificFormat(self):
        """ test scientific formatter """
        f = QgsScientificNumericFormat()
        context = QgsNumericFormatContext()
        self.assertEqual(f.formatDouble(0, context), '0e+00')
        self.assertEqual(f.formatDouble(5, context), '5e+00')
        self.assertEqual(f.formatDouble(5.5, context), '5.5e+00')
        self.assertEqual(f.formatDouble(-5, context), '-5e+00')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.5e+00')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-5.555556e+07')
        context.setDecimalSeparator('x')
        self.assertEqual(f.formatDouble(0, context), '0e+00')
        self.assertEqual(f.formatDouble(-5.5, context), '-5x5e+00')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-5x555556e+07')
        context.setDecimalSeparator('.')

        # places must be at least 1 for scientific notation!
        f.setNumberDecimalPlaces(0)
        self.assertEqual(f.numberDecimalPlaces(), 1)
        self.assertEqual(f.formatDouble(0, context), '0e+00')
        self.assertEqual(f.formatDouble(5.5, context), '5.5e+00')
        self.assertEqual(f.formatDouble(55555555.5, context), '5.6e+07')
        self.assertEqual(f.formatDouble(55555555.123456, context), '5.6e+07')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.5e+00')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-5.6e+07')

        f.setNumberDecimalPlaces(3)
        self.assertEqual(f.formatDouble(0, context), '0e+00')
        self.assertEqual(f.formatDouble(5.5, context), '5.5e+00')
        self.assertEqual(f.formatDouble(55555555.5, context), '5.556e+07')
        self.assertEqual(f.formatDouble(55555555.123456, context), '5.556e+07')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.5e+00')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-5.556e+07')
        f.setShowTrailingZeros(True)
        self.assertEqual(f.formatDouble(0, context), '0.000e+00')
        self.assertEqual(f.formatDouble(5, context), '5.000e+00')
        self.assertEqual(f.formatDouble(-5, context), '-5.000e+00')
        self.assertEqual(f.formatDouble(5.5, context), '5.500e+00')
        self.assertEqual(f.formatDouble(55555555.5, context), '5.556e+07')
        self.assertEqual(f.formatDouble(55555555.123456, context), '5.556e+07')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.500e+00')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-5.556e+07')
        f.setShowPlusSign(True)
        self.assertEqual(f.formatDouble(0, context), '0.000e+00')
        self.assertEqual(f.formatDouble(5, context), '+5.000e+00')
        self.assertEqual(f.formatDouble(-5, context), '-5.000e+00')
        self.assertEqual(f.formatDouble(5.5, context), '+5.500e+00')
        self.assertEqual(f.formatDouble(55555555.5, context), '+5.556e+07')
        self.assertEqual(f.formatDouble(55555555.123456, context),
                         '+5.556e+07')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.500e+00')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-5.556e+07')

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

        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, QgsScientificNumericFormat)

        self.assertEqual(f3.showTrailingZeros(), f.showTrailingZeros())
        self.assertEqual(f3.showPlusSign(), f.showPlusSign())
        self.assertEqual(f3.numberDecimalPlaces(), f.numberDecimalPlaces())
        self.assertEqual(f3.showThousandsSeparator(),
                         f.showThousandsSeparator())
示例#6
0
    def testPercentageFormat(self):
        """ test percentage formatter """
        f = QgsPercentageNumericFormat()
        f.setInputValues(QgsPercentageNumericFormat.ValuesArePercentage)
        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%')

        f = QgsPercentageNumericFormat()
        f.setInputValues(QgsPercentageNumericFormat.ValuesAreFractions)
        context = QgsNumericFormatContext()
        self.assertEqual(f.formatDouble(0, context), '0%')
        self.assertEqual(f.formatDouble(5, context), '500%')
        self.assertEqual(f.formatDouble(5.5, context), '550%')
        self.assertEqual(f.formatDouble(-5, context), '-500%')
        self.assertEqual(f.formatDouble(-5.5, context), '-550%')
        self.assertEqual(f.formatDouble(-55555555.5, context),
                         '-5,555,555,550%')
        context.setDecimalSeparator('x')
        self.assertEqual(f.formatDouble(0, context), '0%')
        self.assertEqual(f.formatDouble(-5.5, context), '-550%')
        self.assertEqual(f.formatDouble(-0.005, context), '-0x5%')
        context.setThousandsSeparator('y')
        self.assertEqual(f.formatDouble(-5.5, context), '-550%')
        self.assertEqual(f.formatDouble(-55555555.5, context),
                         '-5y555y555y550%')
        f.setShowThousandsSeparator(False)
        self.assertEqual(f.formatDouble(-5.5, context), '-550%')
        self.assertEqual(f.formatDouble(-55555555.5, context), '-5555555550%')
        context.setDecimalSeparator('.')
        f.setNumberDecimalPlaces(0)
        self.assertEqual(f.formatDouble(0, context), '0%')
        self.assertEqual(f.formatDouble(5.5, context), '550%')
        self.assertEqual(f.formatDouble(55555555.5, context), '5555555550%')
        self.assertEqual(f.formatDouble(0.123456, context), '12%')
        self.assertEqual(f.formatDouble(-5.5, context), '-550%')
        self.assertEqual(f.formatDouble(-0.123456, context), '-12%')
        f.setNumberDecimalPlaces(3)
        self.assertEqual(f.formatDouble(0, context), '0%')
        self.assertEqual(f.formatDouble(5.5, context), '550%')
        self.assertEqual(f.formatDouble(55555555.5, context), '5555555550%')
        self.assertEqual(f.formatDouble(0.123456, context), '12.346%')
        self.assertEqual(f.formatDouble(-5.5, context), '-550%')
        self.assertEqual(f.formatDouble(-0.123456, context), '-12.346%')
        f.setShowTrailingZeros(True)
        self.assertEqual(f.formatDouble(0, context), '0.000%')
        self.assertEqual(f.formatDouble(5, context), '500.000%')
        self.assertEqual(f.formatDouble(-5, context), '-500.000%')
        self.assertEqual(f.formatDouble(0.5, context), '50.000%')
        self.assertEqual(f.formatDouble(55555555.5, context),
                         '5555555550.000%')
        self.assertEqual(f.formatDouble(0.123456, context), '12.346%')
        self.assertEqual(f.formatDouble(-5.5, context), '-550.000%')
        self.assertEqual(f.formatDouble(-1234.5, context), '-123450.000%')
        f.setShowPlusSign(True)
        self.assertEqual(f.formatDouble(0, context), '0.000%')
        self.assertEqual(f.formatDouble(5, context), '+500.000%')
        self.assertEqual(f.formatDouble(-5, context), '-500.000%')
        self.assertEqual(f.formatDouble(5.5, context), '+550.000%')
        self.assertEqual(f.formatDouble(-5.5, context), '-550.000%')

        context.setPercent('p')
        self.assertEqual(f.formatDouble(0, context), '0.000p')
        self.assertEqual(f.formatDouble(5, context), '+500.000p')
        self.assertEqual(f.formatDouble(-5, context), '-500.000p')

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

        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.inputValues(), f.inputValues())

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

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

        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.inputValues(), f.inputValues())
示例#7
0
    def testBearingFormat(self):
        """ test bearing formatter """
        f = QgsBearingNumericFormat()
        f.setDirectionFormat(
            QgsBearingNumericFormat.UseRange0To180WithEWDirectionalSuffix)
        context = QgsNumericFormatContext()
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(90, context), '90°E')
        self.assertEqual(f.formatDouble(180, context), '180°')
        self.assertEqual(f.formatDouble(270, context), '90°W')
        self.assertEqual(f.formatDouble(300, context), '60°W')
        self.assertEqual(f.formatDouble(5, context), '5°E')
        self.assertEqual(f.formatDouble(5.5, context), '5.5°E')
        self.assertEqual(f.formatDouble(-5, context), '5°W')
        self.assertEqual(f.formatDouble(-5.5, context), '5.5°W')
        context.setDecimalSeparator('x')
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(-5.5, context), '5x5°W')
        self.assertEqual(f.formatDouble(180, context), '180°')
        context.setDecimalSeparator('.')
        f.setNumberDecimalPlaces(0)
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(5.5, context), '6°E')
        self.assertEqual(f.formatDouble(-5.5, context), '6°W')
        self.assertEqual(f.formatDouble(180, context), '180°')
        f.setNumberDecimalPlaces(3)
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(5.5, context), '5.5°E')
        self.assertEqual(f.formatDouble(-5.5, context), '5.5°W')
        self.assertEqual(f.formatDouble(180, context), '180°')
        f.setShowTrailingZeros(True)
        self.assertEqual(f.formatDouble(0, context),
                         '0.000°E')  # todo - fix and avoid E
        self.assertEqual(f.formatDouble(5, context), '5.000°E')
        self.assertEqual(f.formatDouble(-5, context), '5.000°W')
        self.assertEqual(f.formatDouble(5.5, context), '5.500°E')
        self.assertEqual(f.formatDouble(-5.5, context), '5.500°W')
        self.assertEqual(f.formatDouble(180, context),
                         '180.000°E')  # todo fix and avoid E

        f = QgsBearingNumericFormat()
        f.setDirectionFormat(
            QgsBearingNumericFormat.UseRangeNegative180ToPositive180)
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(90, context), '90°')
        self.assertEqual(f.formatDouble(180, context), '180°')
        self.assertEqual(f.formatDouble(270, context), '-90°')
        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°')
        context.setDecimalSeparator('x')
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(-5.5, context), '-5x5°')
        self.assertEqual(f.formatDouble(180, context), '180°')
        context.setDecimalSeparator('.')
        f.setNumberDecimalPlaces(0)
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(5.5, context), '6°')
        self.assertEqual(f.formatDouble(-5.5, context), '-6°')
        self.assertEqual(f.formatDouble(180, context), '180°')
        f.setNumberDecimalPlaces(3)
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(5.5, context), '5.5°')
        self.assertEqual(f.formatDouble(-5.5, context), '-5.5°')
        self.assertEqual(f.formatDouble(180, context), '180°')
        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(-5.5, context), '-5.500°')
        self.assertEqual(f.formatDouble(180, context), '180.000°')

        f = QgsBearingNumericFormat()
        f.setDirectionFormat(QgsBearingNumericFormat.UseRange0To360)
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(90, context), '90°')
        self.assertEqual(f.formatDouble(180, context), '180°')
        self.assertEqual(f.formatDouble(270, context), '270°')
        self.assertEqual(f.formatDouble(5, context), '5°')
        self.assertEqual(f.formatDouble(5.5, context), '5.5°')
        self.assertEqual(f.formatDouble(-5, context), '355°')
        self.assertEqual(f.formatDouble(-5.5, context), '354.5°')
        context.setDecimalSeparator('x')
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(-5.5, context), '354x5°')
        self.assertEqual(f.formatDouble(180, context), '180°')
        context.setDecimalSeparator('.')
        f.setNumberDecimalPlaces(0)
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(5.5, context), '6°')
        self.assertEqual(f.formatDouble(-5.4, context), '355°')
        self.assertEqual(f.formatDouble(180, context), '180°')
        f.setNumberDecimalPlaces(3)
        self.assertEqual(f.formatDouble(0, context), '0°')
        self.assertEqual(f.formatDouble(5.5, context), '5.5°')
        self.assertEqual(f.formatDouble(-5.5, context), '354.5°')
        self.assertEqual(f.formatDouble(180, context), '180°')
        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), '355.000°')
        self.assertEqual(f.formatDouble(5.5, context), '5.500°')
        self.assertEqual(f.formatDouble(-5.5, context), '354.500°')
        self.assertEqual(f.formatDouble(180, context), '180.000°')

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

        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.directionFormat(), f.directionFormat())

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

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

        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.directionFormat(), f.directionFormat())
示例#8
0
    def testCurrencyFormat(self):
        """ test currency formatter """
        f = QgsCurrencyNumericFormat()
        f.setPrefix('$')
        context = QgsNumericFormatContext()
        f.setShowTrailingZeros(False)
        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')

        f.setSuffix('AUD')
        self.assertEqual(f.formatDouble(0, context), '$0.000AUD')
        self.assertEqual(f.formatDouble(5, context), '+$5.000AUD')
        self.assertEqual(f.formatDouble(-5, context), '-$5.000AUD')
        self.assertEqual(f.formatDouble(5.5, context), '+$5.500AUD')
        self.assertEqual(f.formatDouble(55555555.5, context),
                         '+$55555555.500AUD')
        self.assertEqual(f.formatDouble(55555555.123456, context),
                         '+$55555555.123AUD')
        self.assertEqual(f.formatDouble(-5.5, context), '-$5.500AUD')
        self.assertEqual(f.formatDouble(-55555555.5, context),
                         '-$55555555.500AUD')

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

        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.prefix(), f.prefix())
        self.assertEqual(f2.suffix(), f.suffix())

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

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

        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.prefix(), f.prefix())
        self.assertEqual(f3.suffix(), f.suffix())
示例#9
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())