1
0
mirror of https://github.com/JoelBender/bacpypes synced 2025-09-28 22:15:23 +08:00

type checking improved, added tests

This commit is contained in:
Joel Bender 2017-09-08 15:00:38 -04:00
parent a6ceef3f67
commit cd5a4653dd
5 changed files with 31 additions and 8 deletions

View File

@ -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, )

View File

@ -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, )

View File

@ -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, )

View File

@ -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')
integer_endec(-2147483648, '80000000')

View File

@ -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')
unsigned_endec(2147483648, '80000000')