Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit 808eeaa

Browse files
authored
Merge pull request #625 from hdiogenes/ubint-tests
UBInt casting to int() + improved unit tests
2 parents ba551d7 + 8b9240d commit 808eeaa

File tree

4 files changed

+63
-43
lines changed

4 files changed

+63
-43
lines changed

pyof/foundation/base.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333

3434
# This will determine the order on sphinx documentation.
3535
__all__ = ('GenericStruct', 'GenericMessage', 'GenericType', 'GenericBitMask',
36-
'MetaStruct', 'MetaBitMask')
36+
'MetaStruct', 'MetaBitMask', 'UBIntBase')
3737

3838
# Classes
3939

4040

4141
class GenericType:
4242
"""Foundation class for all custom attributes.
4343
44-
Base class for :class:`~.UBInt8`, :class:`~.Char`
44+
Base class for :class:`~.UBIntBase`, :class:`~.Char`
4545
and others.
4646
"""
4747

@@ -259,8 +259,17 @@ def is_bitmask(self):
259259
return self._value and issubclass(type(self._value), GenericBitMask)
260260

261261

262+
class UBIntBase(GenericType):
263+
"""Base class for UBInt{8,16,32,64,128}."""
264+
def __int__(self):
265+
"""Allow converting an UBInt() back to an int()."""
266+
# Skip GenericType's checking if this is an Enum ou BitMask
267+
# (because it won't be), and convert directly from _value
268+
return int(self._value)
269+
270+
262271
class MetaStruct(type):
263-
"""MetaClass that dinamically handles openflow version of class attributes.
272+
"""MetaClass that dynamically handles openflow version of class attributes.
264273
265274
See more about it at:
266275
https://github.com/kytos/python-openflow/wiki/Version-Inheritance
@@ -424,7 +433,6 @@ def get_pyof_obj_new_version(name, obj, new_version):
424433
an instance of the new version of the 'obj'.
425434
426435
Example:
427-
428436
>>> from pyof.foundation.base import MetaStruct as ms
429437
>>> from pyof.v0x01.common.header import Header
430438
>>> name = 'header'
@@ -503,7 +511,7 @@ def __eq__(self, other):
503511
other (GenericStruct): The struct to be compared with.
504512
505513
Returns:
506-
bool: Returns the result of comparation.
514+
bool: Returns the comparison result.
507515
508516
"""
509517
return self.pack() == other.pack()
@@ -519,13 +527,13 @@ def _attr_fits_into_class(attr, cls):
519527

520528
@staticmethod
521529
def _is_pyof_attribute(obj):
522-
"""Return True if the object is a kytos attribute.
530+
"""Return True if the object is a pyof attribute.
523531
524-
To be a kytos attribute the item must be an instance of either
532+
To be a pyof attribute the item must be an instance of either
525533
GenericType or GenericStruct.
526534
527535
Returns:
528-
bool: Returns TRUE if the obj is a kytos attribute, otherwise False
536+
bool: Returns True if the obj is a pyof attribute, otherwise False
529537
530538
"""
531539
return isinstance(obj, (GenericType, GenericStruct))

pyof/foundation/basic_types.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66

77
# Local source tree imports
88
from pyof.foundation import exceptions
9-
from pyof.foundation.base import GenericStruct, GenericType
10-
11-
# Third-party imports
9+
from pyof.foundation.base import GenericStruct, GenericType, UBIntBase
1210

1311
__all__ = ('BinaryData', 'Char', 'ConstantTypeList', 'FixedTypeList',
1412
'IPAddress', 'DPID', 'HWAddress', 'Pad', 'UBInt8', 'UBInt16',
15-
'UBInt32', 'UBInt64')
13+
'UBInt32', 'UBInt64', 'UBInt128')
1614

1715

1816
class Pad(GenericType):
@@ -79,7 +77,7 @@ def __deepcopy__(self, memo):
7977
return Pad(length=self._length)
8078

8179

82-
class UBInt8(GenericType):
80+
class UBInt8(UBIntBase):
8381
"""Format character for an Unsigned Char.
8482
8583
Class for an 8-bit (1-byte) Unsigned Integer.
@@ -88,7 +86,7 @@ class UBInt8(GenericType):
8886
_fmt = "!B"
8987

9088

91-
class UBInt16(GenericType):
89+
class UBInt16(UBIntBase):
9290
"""Format character for an Unsigned Short.
9391
9492
Class for an 16-bit (2-byte) Unsigned Integer.
@@ -97,7 +95,7 @@ class UBInt16(GenericType):
9795
_fmt = "!H"
9896

9997

100-
class UBInt32(GenericType):
98+
class UBInt32(UBIntBase):
10199
"""Format character for an Unsigned Int.
102100
103101
Class for an 32-bit (4-byte) Unsigned Integer.
@@ -106,7 +104,7 @@ class UBInt32(GenericType):
106104
_fmt = "!I"
107105

108106

109-
class UBInt64(GenericType):
107+
class UBInt64(UBIntBase):
110108
"""Format character for an Unsigned Long Long.
111109
112110
Class for an 64-bit (8-byte) Unsigned Integer.
@@ -115,7 +113,7 @@ class UBInt64(GenericType):
115113
_fmt = "!Q"
116114

117115

118-
class UBInt128(GenericType):
116+
class UBInt128(UBIntBase):
119117
"""Format character for an Unsigned Long Long.
120118
121119
Class for an 128-bit (16-byte) Unsigned Integer.

pyof/foundation/network_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ def __init__(self, version=6, tclass=0, flabel=0, length=0,
643643
next_header=0, hop_limit=255, source="0:0:0:0:0:0:0:0",
644644
destination="0:0:0:0:0:0:0:0", data=b''):
645645
"""Create an IPv6 with the parameters below.
646+
646647
Args:
647648
version (int): IP protocol version. Defaults to 6.
648649
tclass (int): DS (6 bits) + ECN (2 bits). Default is 0.

tests/unit/test_foundation/test_basic_types.py

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33

44
from pyof.foundation import basic_types
5+
from pyof.foundation.exceptions import PackException
56
from pyof.foundation.basic_types import BinaryData
67

78

@@ -10,21 +11,29 @@ class TestUBInt8(unittest.TestCase):
1011

1112
def setUp(self):
1213
"""Basic test setup."""
13-
self.ubint8 = basic_types.UBInt8()
14+
self.ubint8 = basic_types.UBInt8(255)
1415

1516
def test_get_size(self):
1617
"""[Foundation/BasicTypes/UBInt8] - size 1."""
1718
self.assertEqual(self.ubint8.get_size(), 1)
1819

19-
@unittest.skip('Not yet implemented')
2020
def test_pack(self):
2121
"""[Foundation/BasicTypes/UBInt8] - packing."""
22-
pass
22+
self.assertEqual(self.ubint8.pack(), b'\xff')
2323

24-
@unittest.skip('Not yet implemented')
2524
def test_unpack(self):
2625
"""[Foundation/BasicTypes/UBInt8] - unpacking."""
27-
pass
26+
u = basic_types.UBInt8()
27+
u.unpack(b'\xfe')
28+
self.assertEqual(u.value, 254)
29+
30+
def test_pack_error(self):
31+
"""[Foundation/BasicTypes/UBInt8] - packing exception."""
32+
u = basic_types.UBInt8(256)
33+
self.assertRaises(PackException, u.pack)
34+
35+
def test_cast_to_int(self):
36+
self.assertEqual(255, int(self.ubint8))
2837

2938

3039
class TestUBInt16(unittest.TestCase):
@@ -38,16 +47,6 @@ def test_get_size(self):
3847
"""[Foundation/BasicTypes/UBInt16] - size 2."""
3948
self.assertEqual(self.ubint16.get_size(), 2)
4049

41-
@unittest.skip('Not yet implemented')
42-
def test_pack(self):
43-
"""[Foundation/BasicTypes/UBInt16] - packing."""
44-
pass
45-
46-
@unittest.skip('Not yet implemented')
47-
def test_unpack(self):
48-
"""[Foundation/BasicTypes/UBInt16] - unpacking."""
49-
pass
50-
5150

5251
class TestUBInt32(unittest.TestCase):
5352
"""Test of UBInt32 BasicType."""
@@ -60,15 +59,29 @@ def test_get_size(self):
6059
"""[Foundation/BasicTypes/UBInt32] - size 4."""
6160
self.assertEqual(self.ubint32.get_size(), 4)
6261

63-
@unittest.skip('Not yet implemented')
64-
def test_pack(self):
65-
"""[Foundation/BasicTypes/UBInt32] - packing."""
66-
pass
6762

68-
@unittest.skip('Not yet implemented')
69-
def test_unpack(self):
70-
"""[Foundation/BasicTypes/UBInt32] - unpacking."""
71-
pass
63+
class TestUBInt64(unittest.TestCase):
64+
"""Test of UBInt64 BasicType."""
65+
66+
def setUp(self):
67+
"""Basic test setup."""
68+
self.ubint64 = basic_types.UBInt64()
69+
70+
def test_get_size(self):
71+
"""[Foundation/BasicTypes/UBInt64] - size 8."""
72+
self.assertEqual(self.ubint64.get_size(), 8)
73+
74+
75+
class TestUBInt128(unittest.TestCase):
76+
"""Test of UBInt128 BasicType."""
77+
78+
def setUp(self):
79+
"""Basic test setup."""
80+
self.ubint128 = basic_types.UBInt128()
81+
82+
def test_get_size(self):
83+
"""[Foundation/BasicTypes/UBInt128] - size 16."""
84+
self.assertEqual(self.ubint128.get_size(), 16)
7285

7386

7487
class TestChar(unittest.TestCase):
@@ -100,7 +113,7 @@ def test_unpack(self):
100113
self.assertEqual(char2.value, 'foo')
101114

102115

103-
class TestHWaddress(unittest.TestCase):
116+
class TestHWAddress(unittest.TestCase):
104117
"""Test of HWAddress BasicType."""
105118

106119
def test_unpack_packed(self):
@@ -199,5 +212,5 @@ def test_pack_packable_value(self):
199212

200213
def test_unexpected_value_as_parameter(self):
201214
"""Should raise ValueError if pack value is not bytes."""
202-
data= BinaryData('Some string')
203-
self.assertRaises(ValueError, data.pack, "can't be string")
215+
data = BinaryData('Some string')
216+
self.assertRaises(ValueError, data.pack, "can't be a string")

0 commit comments

Comments
 (0)