From cd5a4653ddb9c1bd793355850a3448b95c71e0fb Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Fri, 8 Sep 2017 15:00:38 -0400 Subject: [PATCH] type checking improved, added tests --- py25/bacpypes/primitivedata.py | 4 ++-- py27/bacpypes/primitivedata.py | 4 ++-- py34/bacpypes/primitivedata.py | 4 ++-- tests/test_primitive_data/test_integer.py | 13 ++++++++++++- tests/test_primitive_data/test_unsigned.py | 14 +++++++++++++- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/py25/bacpypes/primitivedata.py b/py25/bacpypes/primitivedata.py index 363672f..1335041 100755 --- a/py25/bacpypes/primitivedata.py +++ b/py25/bacpypes/primitivedata.py @@ -618,7 +618,7 @@ class Unsigned(Atomic): @classmethod def is_valid(cls, arg): """Return True if arg is valid value for the class.""" - return isinstance(arg, (int, long)) and (arg >= 0) + return isinstance(arg, (int, long)) and (not isinstance(arg, bool)) and (arg >= 0) def __str__(self): return "Unsigned(%s)" % (self.value, ) @@ -693,7 +693,7 @@ class Integer(Atomic): @classmethod def is_valid(cls, arg): """Return True if arg is valid value for the class.""" - return isinstance(arg, (int, long)) + return isinstance(arg, (int, long)) and (not isinstance(arg, bool)) def __str__(self): return "Integer(%s)" % (self.value, ) diff --git a/py27/bacpypes/primitivedata.py b/py27/bacpypes/primitivedata.py index 474bd39..1047f43 100755 --- a/py27/bacpypes/primitivedata.py +++ b/py27/bacpypes/primitivedata.py @@ -622,7 +622,7 @@ class Unsigned(Atomic): @classmethod def is_valid(cls, arg): """Return True if arg is valid value for the class.""" - return isinstance(arg, (int, long)) and (arg >= 0) + return isinstance(arg, (int, long)) and (not isinstance(arg, bool)) and (arg >= 0) def __str__(self): return "Unsigned(%s)" % (self.value, ) @@ -697,7 +697,7 @@ class Integer(Atomic): @classmethod def is_valid(cls, arg): """Return True if arg is valid value for the class.""" - return isinstance(arg, (int, long)) + return isinstance(arg, (int, long)) and (not isinstance(arg, bool)) def __str__(self): return "Integer(%s)" % (self.value, ) diff --git a/py34/bacpypes/primitivedata.py b/py34/bacpypes/primitivedata.py index 66aefba..d7d1306 100755 --- a/py34/bacpypes/primitivedata.py +++ b/py34/bacpypes/primitivedata.py @@ -640,7 +640,7 @@ class Unsigned(Atomic): @classmethod def is_valid(cls, arg): """Return True if arg is valid value for the class.""" - return isinstance(arg, (int, long)) and (arg >= 0) + return isinstance(arg, int) and (not isinstance(arg, bool)) and (arg >= 0) def __str__(self): return "Unsigned(%s)" % (self.value, ) @@ -713,7 +713,7 @@ class Integer(Atomic): @classmethod def is_valid(cls, arg): """Return True if arg is valid value for the class.""" - return isinstance(arg, int) + return isinstance(arg, int) and (not isinstance(arg, bool)) def __str__(self): return "Integer(%s)" % (self.value, ) diff --git a/tests/test_primitive_data/test_integer.py b/tests/test_primitive_data/test_integer.py index 5ff144a..464a85f 100644 --- a/tests/test_primitive_data/test_integer.py +++ b/tests/test_primitive_data/test_integer.py @@ -6,6 +6,7 @@ Test Primitive Data Integer --------------------------- """ +import sys import unittest from bacpypes.debugging import bacpypes_debugging, ModuleLogger, xtob @@ -78,6 +79,15 @@ class TestInteger(unittest.TestCase): obj = Integer() assert obj.value == 0 + assert Integer.is_valid(1) + assert Integer.is_valid(-1) + if sys.version[0] == 2: + assert Integer.is_valid(long(1)) + assert Integer.is_valid(long(-1)) + + assert not Integer.is_valid(True) + assert not Integer.is_valid(1.0) + with self.assertRaises(TypeError): Integer("some string") with self.assertRaises(TypeError): @@ -139,4 +149,5 @@ class TestInteger(unittest.TestCase): integer_endec(-8388608, '800000') integer_endec(2147483647, '7fffffff') - integer_endec(-2147483648, '80000000') \ No newline at end of file + integer_endec(-2147483648, '80000000') + diff --git a/tests/test_primitive_data/test_unsigned.py b/tests/test_primitive_data/test_unsigned.py index 7cc7e5f..65d6935 100644 --- a/tests/test_primitive_data/test_unsigned.py +++ b/tests/test_primitive_data/test_unsigned.py @@ -6,6 +6,7 @@ Test Primitive Data Unsigned ---------------------------- """ +import sys import unittest from bacpypes.debugging import bacpypes_debugging, ModuleLogger, xtob @@ -78,6 +79,16 @@ class TestUnsigned(unittest.TestCase): obj = Unsigned() assert obj.value == 0 + assert Unsigned.is_valid(1) + assert not Unsigned.is_valid(-1) + if sys.version[0] == 2: + assert Unsigned.is_valid(long(1)) + assert not Unsigned.is_valid(long(-1)) + + assert not Unsigned.is_valid(True) + assert not Unsigned.is_valid(-1) + assert not Unsigned.is_valid(1.0) + with self.assertRaises(TypeError): Unsigned("some string") with self.assertRaises(TypeError): @@ -138,4 +149,5 @@ class TestUnsigned(unittest.TestCase): unsigned_endec(8388608, '800000') unsigned_endec(2147483647, '7fffffff') - unsigned_endec(2147483648, '80000000') \ No newline at end of file + unsigned_endec(2147483648, '80000000') +