From 6a96d7a3fd958bd95abcce92173e34f2e5491e8c Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 17 May 2023 15:44:11 +0000 Subject: [PATCH] new module: pyversion.py; add test --- mmgen/pyversion.py | 54 +++++++++++++++++++++++++++++ test/unit_tests_d/ut_misc.py | 67 ++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100755 mmgen/pyversion.py create mode 100755 test/unit_tests_d/ut_misc.py diff --git a/mmgen/pyversion.py b/mmgen/pyversion.py new file mode 100755 index 00000000..cfab1720 --- /dev/null +++ b/mmgen/pyversion.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# +# mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet +# Copyright (C)2013-2023 The MMGen Project +# 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() diff --git a/test/unit_tests_d/ut_misc.py b/test/unit_tests_d/ut_misc.py new file mode 100755 index 00000000..ea499c1b --- /dev/null +++ b/test/unit_tests_d/ut_misc.py @@ -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