new module: pyversion.py; add test

This commit is contained in:
The MMGen Project 2023-05-17 15:44:11 +00:00
commit 6a96d7a3fd
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 121 additions and 0 deletions

54
mmgen/pyversion.py Executable file
View file

@ -0,0 +1,54 @@
#!/usr/bin/env python3
#
# mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
# Copyright (C)2013-2023 The MMGen Project <mmgen@tuta.io>
# Licensed under the GNU General Public License, Version 3:
# https://www.gnu.org/licenses
# Public project repositories:
# https://github.com/mmgen/mmgen
# https://gitlab.com/mmgen/mmgen
"""
pyversion: Python version string operations
"""
class PythonVersion(str):
def __new__(cls,arg=None):
if isinstance(arg,PythonVersion):
return arg
if arg:
major,minor = arg.split('.')
else:
import platform
major,minor = platform.python_version_tuple()[:2]
me = str.__new__( cls, f'{major}.{minor}' )
me.major = int(major)
me.minor = int(minor)
return me
def __lt__(self,other):
other = type(self)(other)
return self.major < other.major or (self.major == other.major and self.minor < other.minor)
def __le__(self,other):
other = type(self)(other)
return self.major < other.major or (self.major == other.major and self.minor <= other.minor)
def __eq__(self,other):
other = type(self)(other)
return self.major == other.major and self.minor == other.minor
def __ne__(self,other):
other = type(self)(other)
return not (self.major == other.major and self.minor == other.minor)
def __gt__(self,other):
other = type(self)(other)
return self.major > other.major or (self.major == other.major and self.minor > other.minor)
def __ge__(self,other):
other = type(self)(other)
return self.major > other.major or (self.major == other.major and self.minor >= other.minor)
python_version = PythonVersion()

67
test/unit_tests_d/ut_misc.py Executable file
View file

@ -0,0 +1,67 @@
#!/usr/bin/env python3
"""
test.unit_tests_d.ut_misc: miscellaneous unit tests for the MMGen suite
"""
class unit_tests:
def pyversion(self,name,ut):
from ..include.common import vmsg
from mmgen.pyversion import PythonVersion,python_version
ver = {}
fs = '{:<7} {:<9} {:<5} {}'
vmsg('\n' + fs.format('Version','PyVersion','Major','Minor'))
for k in ('current','3.3','3.12','4.3','7.0'):
obj = python_version if k == 'current' else PythonVersion(k)
major,minor = [int(s) for s in obj.split('.')]
assert obj.major == major and obj.minor == minor
vmsg(fs.format(k.upper(),obj,major,minor))
ver[k] = obj
vmsg('\nPerforming comparison tests:')
assert ver['3.3'] == '3.3'
assert ver['current'] < ver['7.0']
assert ver['3.3'] < ver['4.3']
assert ver['3.12'] < ver['7.0']
assert ver['3.3'] < ver['3.12']
assert ver['current'] < '7.0'
assert ver['3.3'] < '4.3'
assert ver['3.12'] < '7.0'
assert ver['3.3'] < '3.12'
assert ver['current'] <= ver['current']
assert ver['3.3'] <= '4.3'
assert ver['3.12'] <= '7.0'
assert ver['3.3'] <= '3.12'
assert ver['current'] == ver['current']
assert ver['3.3'] == ver['3.3']
assert ver['3.3'] != ver['3.12']
assert ver['3.3'] != ver['4.3']
assert ver['3.3'] == '3.3'
assert ver['3.3'] != '3.12'
assert ver['3.3'] != '4.3'
assert ver['current'] > '3.6'
assert ver['7.0'] > ver['current']
assert ver['4.3'] > '3.3'
assert ver['3.12'] > '3.3'
assert ver['current'] >= ver['current']
assert ver['7.0'] >= ver['current']
assert ver['4.3'] >= '3.3'
assert ver['3.12'] >= '3.12'
assert ver['3.12'] >= '3.3'
assert '3.0' < ver['3.12'] < '3.13'
assert '3.3' < ver['4.3'] <= '4.3'
assert '4.3' <= ver['4.3'] <= '4.3'
assert '4.3' == ver['4.3'] == '4.3'
return True