diff options
author | Jason Madden <jamadden@gmail.com> | 2017-12-17 11:48:17 -0600 |
---|---|---|
committer | Jason Madden <jamadden@gmail.com> | 2017-12-17 11:48:51 -0600 |
commit | e00031bdd9a9ee2f12d7fa687713a36d5e35d02f (patch) | |
tree | 823042d3ba55668f53b99b5c6f2f523cb63b6f18 | |
parent | 245e54e3fdfecd09ef49e9810d6ce4fca1e7b77d (diff) | |
download | zope-i18n-e00031bdd9a9ee2f12d7fa687713a36d5e35d02f.tar.gz |
100% coverage for format.py
There's one #pragma: no cover for a formatting state I didn't go to the trouble of walking through the complicated logic to try to figure out how to produce.
-rw-r--r-- | CHANGES.rst | 2 | ||||
-rw-r--r-- | src/zope/i18n/format.py | 16 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_formats.py | 51 | ||||
-rw-r--r-- | tox.ini | 2 |
4 files changed, 63 insertions, 8 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index ec9931f..2015abb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -24,6 +24,8 @@ KeyError when the ``week`` mapping contained an integer for ``firstDay`` as documented. +- Reach 100% test coverage and maintain in through tox.ini and coveralls.io. + 4.2.0 (2017-05-23) ================== diff --git a/src/zope/i18n/format.py b/src/zope/i18n/format.py index 220e07f..5cb4d35 100644 --- a/src/zope/i18n/format.py +++ b/src/zope/i18n/format.py @@ -471,8 +471,8 @@ class NumberFormat(object): if bin_pattern[PADDING2] is not None and pre_padding > 0: if bin_pattern[PADDING1] is not None: text += bin_pattern[PADDING2] - else: - text += bin_pattern[PADDING2]*pre_padding + else: # pragma: no cover + text += bin_pattern[PADDING2] * pre_padding text += number if bin_pattern[PADDING3] is not None and post_padding > 0: if bin_pattern[PADDING4] is not None: @@ -555,7 +555,10 @@ def parseDateTimePattern(pattern, DATETIMECHARS="aGyMdEDFwWhHmsSkKz"): # Some cleaning up if state == IN_QUOTE: - if quote_start == -1: + if quote_start == -1: # pragma: no cover + # It should not be possible to get into this state. + # The only time we set quote_start to -1 we also set the state + # to DEFAULT. raise DateTimePatternParseError( 'Waaa: state = IN_QUOTE and quote_start = -1!') else: @@ -709,9 +712,9 @@ def buildDateTimeInfo(dt, calendar, pattern): # month in year (Text and Number) for entry in _findFormattingCharacterInPattern('M', pattern): if entry[1] == 1: - info[entry] = u"%i" %dt.month + info[entry] = u"%i" % dt.month elif entry[1] == 2: - info[entry] = u"%.2i" %dt.month + info[entry] = u"%.2i" % dt.month elif entry[1] == 3: info[entry] = calendar.months[dt.month][1] else: @@ -781,8 +784,7 @@ def parseNumberPattern(pattern): length = len(pattern) state = BEGIN helper = '' - for pos in range(length): - char = pattern[pos] + for pos, char in enumerate(pattern): if state == BEGIN: if char == '*': state = READ_PADDING_1 diff --git a/src/zope/i18n/tests/test_formats.py b/src/zope/i18n/tests/test_formats.py index 6d1380d..a57b4fb 100644 --- a/src/zope/i18n/tests/test_formats.py +++ b/src/zope/i18n/tests/test_formats.py @@ -28,6 +28,7 @@ from zope.i18n.format import DateTimePatternParseError, DateTimeParseError from zope.i18n.interfaces import INumberFormat from zope.i18n.format import NumberFormat, NumberParseError from zope.i18n.format import parseNumberPattern +from zope.i18n.format import NumberPatternParseError class LocaleStub(object): @@ -194,6 +195,13 @@ class TestDateTimePatternParser(_TestCase): # Test correct length of characters in datetime fields parseDateTimePattern("HHHHH") + def testParseDateTimePatternRepeatDateTimeChars(self): + result = parseDateTimePattern('aG') + self.assertEqual( + result, + [('a', 1), ('G', 1)] + ) + class TestBuildDateTimeParseInfo(_TestCase): """This class tests the functionality of the buildDateTimeParseInfo() @@ -668,6 +676,18 @@ class TestDateTimeFormat(_TestCase): u"2. Freitag im Januar, 2003") + def testFormatGregorianEra(self): + self.assertEqual( + self.format.format(datetime.date(2017, 12, 17), 'G'), + u'n. Chr.' + ) + + def testFormateMonthLengthOne(self): + self.assertEqual( + self.format.format(datetime.date(2017, 12, 17), 'M'), + u'12' + ) + class TestNumberPatternParser(_TestCase): """Extensive tests for the ICU-based-syntax number pattern parser.""" @@ -886,6 +906,37 @@ class TestNumberPatternParser(_TestCase): ((None, '', None, '###0', '', '', None, 'DEM', None, 0), (None, '', None, '###0', '', '', None, 'DEM', None, 0))) + def testParseInvalidBegin(self): + with self.assertRaisesRegex(NumberPatternParseError, + "Wrong syntax at beginning"): + parseNumberPattern(".") + + def testParseFractionQuate(self): + pattern, neg_pattern = parseNumberPattern("0.'") + self.assertEqual( + (None, '', None, '0', '', '', None, '', None, 0), + pattern) + self.assertEqual( + (None, '', None, '0', '', '', None, '', None, 0), + neg_pattern) + + def testParseExponentialQuote(self): + pattern, neg_pattern = parseNumberPattern("0E'") + self.assertEqual( + (None, '', None, '0', '', '', None, '', None, 0), + pattern) + self.assertEqual( + (None, '', None, '0', '', '', None, '', None, 0), + neg_pattern) + + def testParseExponentialNumber(self): + pattern, neg_pattern = parseNumberPattern("0E1") + self.assertEqual( + (None, '', None, '0', '', '', None, '1', None, 0), + pattern) + self.assertEqual( + (None, '', None, '0', '', '', None, '1', None, 0), + neg_pattern) class TestNumberFormat(_TestCase): """Test the functionality of an implmentation of the NumberFormat.""" @@ -16,7 +16,7 @@ basepython = commands = coverage run -m zope.testrunner --test-path=src [] coverage run -a -m sphinx -b doctest -d {envdir}/.cache/doctrees docs {envdir}/.cache/doctest - coverage report --fail-under=99 + coverage report --fail-under=100 deps = {[testenv]deps} coverage |