summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey Stepanov <penguinolog@users.noreply.github.com>2023-03-29 09:58:00 +0200
committerGitHub <noreply@github.com>2023-03-29 09:58:00 +0200
commit9e7ec1c69d33c7b365b081ca72074e6c6979c85f (patch)
tree1524d8ee245edafc226fb30b616d39979cfc32a9
parentbc43d926e1fc339d9a07ecd1d0919f83f311c09a (diff)
downloadurwid-9e7ec1c69d33c7b365b081ca72074e6c6979c85f.tar.gz
Test fixes (#524)
* asyncio coroutine was not awaited, so test was false-positive (poor visible on Travis CI) Fix test. * FloatEdit unittests passed, incorrect output doctests not caused exception on CI * warnings on version number * warnings about deprecated setup.py usage for tests run * python2.7 is not able to execute tests for asyncio due to mandatory syntax changes (async def). Fix logic: 1. added `isinstance` check before getting significance 2. Fix FloatEdit and add corner case tests 3. normalize version number 4. execute tests via `unittest` 5. stop using python 2.7 in tests run instead of increasing complexity Co-authored-by: Aleksei Stepanov <alekseis@nvidia.com>
-rw-r--r--tox.ini26
-rw-r--r--urwid/numedit.py24
-rw-r--r--urwid/tests/test_event_loops.py6
-rw-r--r--urwid/tests/test_floatedit.py34
-rw-r--r--urwid/version.py4
5 files changed, 47 insertions, 47 deletions
diff --git a/tox.ini b/tox.ini
index 87c7b17..0142550 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,11 +1,5 @@
[tox]
-envlist =
- py27
- py35
- py36
- py37
- py38
- pypy
+envlist = py3{6,7,8,9,10,11},pypy3
[testenv]
usedevelop = true
@@ -13,18 +7,8 @@ deps =
setuptools
tornado<5.0.0
coverage
- py27: twisted==16.6.0
- py35: twisted
- py36: twisted
- py37: twisted
- py38: twisted
- pypy: twisted
- py35: trio
- py36: trio
- py37: trio
- py38: trio
- py27: mock
- pypy: mock
-commands =
- coverage run ./setup.py test
+ twisted
+ trio
+commands =
+ coverage run -m unittest discover -s urwid -v
diff --git a/urwid/numedit.py b/urwid/numedit.py
index 0502fa6..3e073cf 100644
--- a/urwid/numedit.py
+++ b/urwid/numedit.py
@@ -21,7 +21,7 @@
from urwid import Edit
-from decimal import Decimal, localcontext
+from decimal import Decimal
import re
@@ -206,7 +206,7 @@ class FloatEdit(NumEdit):
>>> e.keypress(size, '.')
>>> e.keypress(size, '5')
>>> e.keypress(size, '1')
- >>> assert e.value() == Decimal("51.51")
+ >>> assert e.value() == Decimal("51.51"), e.value()
>>> e, size = FloatEdit(decimalSeparator=":"), (10,)
Traceback (most recent call last):
...
@@ -227,7 +227,7 @@ class FloatEdit(NumEdit):
>>> e.keypress(size, '1')
>>> e.keypress(size, '5')
>>> e.keypress(size, '9')
- >>> assert e.value() == Decimal("3.1416")
+ >>> assert e.value() == Decimal("3.1416"), e.value()
>>> e, size = FloatEdit("", ""), (10,)
>>> assert e.value() is None
>>> e, size = FloatEdit(u"", 10.0), (10,)
@@ -247,13 +247,13 @@ class FloatEdit(NumEdit):
raise ValueError("default: Only 'str', 'int', "
"'long' or Decimal input allowed")
- if isinstance(default, str) and len(default):
+ if isinstance(default, str) and default:
# check if it is a float, raises a ValueError otherwise
float(default)
default = Decimal(default)
- if preserveSignificance:
- self.significance = abs(default.as_tuple()[2])
+ if preserveSignificance and isinstance(default, Decimal):
+ self.significance = default
val = str(default)
@@ -265,13 +265,9 @@ class FloatEdit(NumEdit):
Return the numeric value of self.edit_text.
"""
if self.edit_text:
- # get the ip and fp, handles also the case that there is no '.'
- ip, fp = ([v for v in
- self.edit_text.split(self._decimalSeparator)] + [0])[0:2]
-
- with localcontext() as ctx:
- ctx = (self.significance or len(str(fp)))+1
- return Decimal(self.edit_text.replace(
- self._decimalSeparator, '.')) * 1
+ normalized = Decimal(self.edit_text.replace(self._decimalSeparator, '.'))
+ if self.significance is not None:
+ return normalized.quantize(self.significance)
+ return normalized
return None
diff --git a/urwid/tests/test_event_loops.py b/urwid/tests/test_event_loops.py
index 0e94812..49d1270 100644
--- a/urwid/tests/test_event_loops.py
+++ b/urwid/tests/test_event_loops.py
@@ -201,14 +201,14 @@ else:
evl.alarm(0.5, lambda: 1 / 0) # Simulate error in event loop
self.assertRaises(ZeroDivisionError, evl.run)
- async def test_coroutine_error(self):
+ def test_coroutine_error(self):
evl = self.evl
async def error_coro():
result = 1 / 0 # Simulate error in coroutine
- yield result
+ return result
- asyncio.ensure_future(await error_coro())
+ asyncio.ensure_future(error_coro())
self.assertRaises(ZeroDivisionError, evl.run)
diff --git a/urwid/tests/test_floatedit.py b/urwid/tests/test_floatedit.py
index 77c581e..7ac5d27 100644
--- a/urwid/tests/test_floatedit.py
+++ b/urwid/tests/test_floatedit.py
@@ -1,21 +1,41 @@
import unittest
from urwid.numedit import FloatEdit
-from decimal import Decimal, getcontext
+from decimal import Decimal
from itertools import product
+
+
class FloatEditNoPreservePrecicionTest(unittest.TestCase):
def test(self):
for v in ('1.01', '2.5', '300', '4.100', '5.001'):
f = FloatEdit(preserveSignificance=False)
f.set_edit_text(v)
print(f.value(), Decimal(v))
- assert f.value() == Decimal(v), f'{f.value()} != {Decimal(v)}'
+ self.assertEqual(Decimal(v), f.value())
+
+ def test_int(self):
+ corner_value = "1" # just int
+ f = FloatEdit(preserveSignificance=False)
+ f.set_edit_text(corner_value)
+ self.assertEqual(Decimal(corner_value), f.value())
+
+ def test_no_post_dot(self):
+ corner_value = "1." # forced float 1.0 with trimmed end
+ f = FloatEdit(preserveSignificance=False)
+ f.set_edit_text(corner_value)
+ self.assertEqual(Decimal(corner_value), f.value())
+
class FloatEditPreservePrecicionTest(unittest.TestCase):
def test(self):
- for v, sig, sep in product(('1.010', '2.500', '300.000', '4.100', '5.001'), (1, 2, 3), ('.', ',')):
- f = FloatEdit(preserveSignificance=True, default='0.' + '0'*sig, decimalSeparator=sep)
+ for v, sig, sep in product(('1.010', '2.500', '300.000', '4.100', '5.001'), (0, 1, 2, 3), ('.', ',')):
+ precision_template = '0.' + '0'*sig
+ expected = Decimal(v).quantize(Decimal(precision_template))
+ f = FloatEdit(preserveSignificance=True, default=precision_template, decimalSeparator=sep)
+
f.set_edit_text(v.replace('.', sep))
- getcontext().prec = sig+1
- print(f.value(), Decimal(v))
- assert str(f.value()) == str(Decimal(v) * 1), f'{str(f.value())} != {str(Decimal(v)*1)} (significance={sig})'
+ self.assertEqual(
+ expected,
+ f.value(),
+ f'{f.value()} != {expected} (significance={sig!r})'
+ )
diff --git a/urwid/version.py b/urwid/version.py
index 5f23c1e..f88733b 100644
--- a/urwid/version.py
+++ b/urwid/version.py
@@ -1,4 +1,4 @@
from __future__ import division, print_function
-VERSION = (2, 1, 3, 'dev')
-__version__ = ''.join(['-.'[type(x) == int]+str(x) for x in VERSION])[1:]
+VERSION = (2, 1, 3, 'dev0')
+__version__ = '.'.join([str(x) for x in VERSION])