[style] apply google python style guide (#4501)

This commit applies and enforces Google's python style for tests.
This commit is contained in:
Yakun Xu
2020-02-04 10:27:50 -08:00
committed by GitHub
parent 8368d440dd
commit 33808ebfba
501 changed files with 6674 additions and 5515 deletions
+87 -124
View File
@@ -39,9 +39,7 @@ import network_data
def convert_route_to_bytearray(route):
return struct.pack(
">HB", route.border_router_16, ((route.prf & 0x03) << 6)
)
return struct.pack(">HB", route.border_router_16, ((route.prf & 0x03) << 6))
def convert_routes_to_bytearray(routes):
@@ -60,13 +58,10 @@ def convert_border_router_to_bytearray(border_router):
data = struct.pack(
">HBB",
border_router.border_router_16,
(border_router.o & 0x01)
| ((border_router.r & 0x01) << 1)
| ((border_router.c & 0x01) << 2)
| ((border_router.d & 0x01) << 3)
| ((border_router.s & 0x01) << 4)
| ((border_router.p & 0x01) << 5)
| ((border_router.prf & 0x03) << 6),
(border_router.o & 0x01) | ((border_router.r & 0x01) << 1) |
((border_router.c & 0x01) << 2) | ((border_router.d & 0x01) << 3) |
((border_router.s & 0x01) << 4) | ((border_router.p & 0x01) << 5) |
((border_router.prf & 0x03) << 6),
((border_router.n & 0x01) << 7),
)
@@ -75,8 +70,7 @@ def convert_border_router_to_bytearray(border_router):
def convert_lowpan_id_to_bytearray(lowpan_id):
return bytearray(
[lowpan_id.cid | (lowpan_id.c << 4), lowpan_id.context_length]
)
[lowpan_id.cid | (lowpan_id.c << 4), lowpan_id.context_length])
def convert_prefix_sub_tlvs_to_bytearray(sub_tlvs):
@@ -124,16 +118,13 @@ def convert_service_sub_tlvs_to_bytearray(sub_tlvs):
def convert_service_to_bytearray(service):
return (
struct.pack(
">BLB",
((service.t & 0x01) << 7) | ((service.id) & 0x0F),
service.enterprise_number,
service.service_data_length,
)
+ service.service_data
+ convert_service_sub_tlvs_to_bytearray(service.sub_tlvs)
)
return (struct.pack(
">BLB",
((service.t & 0x01) << 7) | ((service.id) & 0x0F),
service.enterprise_number,
service.service_data_length,
) + service.service_data +
convert_service_sub_tlvs_to_bytearray(service.sub_tlvs))
def any_border_router_16():
@@ -171,12 +162,9 @@ def any_prefix(prefix_length=None):
if prefix_length is None:
prefix_length = any_prefix_length()
return bytearray(
[
random.getrandbits(8)
for _ in range(int(math.ceil(prefix_length / 8)))
]
)
return bytearray([
random.getrandbits(8) for _ in range(int(math.ceil(prefix_length / 8)))
])
def any_p():
@@ -231,9 +219,8 @@ def any_border_router():
def any_lowpan_id():
return network_data.LowpanId(
any_c(), any_cid(), any_context_length(), any_stable()
)
return network_data.LowpanId(any_c(), any_cid(), any_context_length(),
any_stable())
def any_prefix_sub_tlvs():
@@ -283,9 +270,7 @@ def any_server_data(data_length=None):
def any_server():
return network_data.Server(
any_server_16(), any_server_data(), any_stable()
)
return network_data.Server(any_server_16(), any_server_data(), any_stable())
def any_service_sub_tlvs():
@@ -305,8 +290,9 @@ def any_stable():
class TestRoute(unittest.TestCase):
def test_should_return_border_router_16_value_when_border_router_16_property_is_called(
self):
self):
# GIVEN
border_router_16 = any_border_router_16()
@@ -332,9 +318,9 @@ class TestRoute(unittest.TestCase):
class TestRouteFactory(unittest.TestCase):
def test_should_create_Route_from_bytearray_when_parse_method_is_called(
self
):
self):
# GIVEN
border_router_16 = any_border_router_16()
prf = any_prf()
@@ -342,8 +328,7 @@ class TestRouteFactory(unittest.TestCase):
factory = network_data.RouteFactory()
data = convert_route_to_bytearray(
network_data.Route(border_router_16, prf)
)
network_data.Route(border_router_16, prf))
# WHEN
actual_route = factory.parse(io.BytesIO(data), None)
@@ -355,8 +340,9 @@ class TestRouteFactory(unittest.TestCase):
class TestRoutesFactory(unittest.TestCase):
def test_should_create_Route_list_from_bytearray_when_parse_method_is_called(
self):
self):
# GIVEN
routes = any_routes()
@@ -372,6 +358,7 @@ class TestRoutesFactory(unittest.TestCase):
class TestHasRoute(unittest.TestCase):
def test_should_return_routes_value_when_routes_property_is_called(self):
# GIVEN
routes = any_routes()
@@ -398,16 +385,15 @@ class TestHasRoute(unittest.TestCase):
class TestHasRouteFactory(unittest.TestCase):
def test_should_create_HasRoute_from_bytearray_when_parse_method_is_called(
self
):
self):
# GIVEN
routes = any_routes()
stable = any_stable()
factory = network_data.HasRouteFactory(
network_data.RoutesFactory(network_data.RouteFactory())
)
network_data.RoutesFactory(network_data.RouteFactory()))
data = convert_routes_to_bytearray(routes)
@@ -424,9 +410,9 @@ class TestHasRouteFactory(unittest.TestCase):
class TestPrefix(unittest.TestCase):
def test_should_return_domain_id_value_when_domain_id_property_is_called(
self
):
self):
# GIVEN
domain_id = any_domain_id()
@@ -445,7 +431,7 @@ class TestPrefix(unittest.TestCase):
self.assertEqual(domain_id, actual_domain_id)
def test_should_return_prefix_length_value_when_prefix_length_property_is_called(
self):
self):
# GIVEN
prefix_length = any_prefix_length()
@@ -482,8 +468,7 @@ class TestPrefix(unittest.TestCase):
self.assertEqual(prefix, actual_prefix)
def test_should_return_sub_tlvs_value_when_sub_tlvs_property_is_called(
self
):
self):
# GIVEN
sub_tlvs = any_prefix_sub_tlvs()
@@ -521,15 +506,14 @@ class TestPrefix(unittest.TestCase):
class TestPrefixSubTlvsFactory(unittest.TestCase):
def test_should_create_SubTlvs_from_bytearray_when_parse_method_is_called(
self
):
self):
# GIVEN
sub_tlvs = any_prefix_sub_tlvs()
factory = network_data.PrefixSubTlvsFactory(
config.create_default_network_data_prefix_sub_tlvs_factories()
)
config.create_default_network_data_prefix_sub_tlvs_factories())
data = convert_prefix_sub_tlvs_to_bytearray(sub_tlvs)
@@ -542,9 +526,9 @@ class TestPrefixSubTlvsFactory(unittest.TestCase):
class TestPrefixFactory(unittest.TestCase):
def test_should_create_Prefix_from_bytearray_when_parse_method_is_called(
self
):
self):
# GIVEN
domain_id = any_domain_id()
prefix_length = any_prefix_length()
@@ -552,14 +536,10 @@ class TestPrefixFactory(unittest.TestCase):
sub_tlvs = any_prefix_sub_tlvs()
factory = network_data.PrefixFactory(
config.create_default_network_data_prefix_sub_tlvs_factory()
)
config.create_default_network_data_prefix_sub_tlvs_factory())
data = (
bytearray([domain_id, prefix_length])
+ prefix
+ convert_prefix_sub_tlvs_to_bytearray(sub_tlvs)
)
data = (bytearray([domain_id, prefix_length]) + prefix +
convert_prefix_sub_tlvs_to_bytearray(sub_tlvs))
message_info = common.MessageInfo()
@@ -575,8 +555,9 @@ class TestPrefixFactory(unittest.TestCase):
class TestBorderRouter(unittest.TestCase):
def test_should_return_border_router_16_value_when_border_router_16_property_is_called(
self):
self):
# GIVEN
border_router_16 = any_border_router_16()
@@ -808,8 +789,9 @@ class TestBorderRouter(unittest.TestCase):
class TestBorderRouterFactory(unittest.TestCase):
def test_should_create_BorderRouter_from_bytearray_when_parse_method_is_called(
self):
self):
# GIVEN
border_router_16 = any_border_router_16()
prf = any_prf()
@@ -825,10 +807,8 @@ class TestBorderRouterFactory(unittest.TestCase):
factory = network_data.BorderRouterFactory()
data = convert_border_router_to_bytearray(
network_data.BorderRouter(
border_router_16, prf, p, s, d, c, r, o, n, stable
)
)
network_data.BorderRouter(border_router_16, prf, p, s, d, c, r, o,
n, stable))
message_info = common.MessageInfo()
message_info.stable = stable
@@ -838,11 +818,9 @@ class TestBorderRouterFactory(unittest.TestCase):
# THEN
self.assertTrue(
isinstance(actual_border_router, network_data.BorderRouter)
)
self.assertEqual(
border_router_16, actual_border_router.border_router_16
)
isinstance(actual_border_router, network_data.BorderRouter))
self.assertEqual(border_router_16,
actual_border_router.border_router_16)
self.assertEqual(prf, actual_border_router.prf)
self.assertEqual(p, actual_border_router.p)
self.assertEqual(s, actual_border_router.s)
@@ -855,13 +833,13 @@ class TestBorderRouterFactory(unittest.TestCase):
class TestLowpanId(unittest.TestCase):
def test_should_return_c_value_when_c_property_is_called(self):
# GIVEN
c = any_c()
lowpan_id = network_data.LowpanId(
c, any_cid(), any_context_length(), any_stable()
)
lowpan_id = network_data.LowpanId(c, any_cid(), any_context_length(),
any_stable())
# WHEN
actual_c = lowpan_id.c
@@ -873,9 +851,8 @@ class TestLowpanId(unittest.TestCase):
# GIVEN
cid = any_cid()
lowpan_id = network_data.LowpanId(
any_c(), cid, any_context_length(), any_stable()
)
lowpan_id = network_data.LowpanId(any_c(), cid, any_context_length(),
any_stable())
# WHEN
actual_cid = lowpan_id.cid
@@ -884,13 +861,12 @@ class TestLowpanId(unittest.TestCase):
self.assertEqual(cid, actual_cid)
def test_should_return_context_length_value_when_context_length_property_is_called(
self):
self):
# GIVEN
context_length = any_context_length()
lowpan_id = network_data.LowpanId(
any_c(), any_cid(), context_length, any_stable()
)
lowpan_id = network_data.LowpanId(any_c(), any_cid(), context_length,
any_stable())
# WHEN
actual_context_length = lowpan_id.context_length
@@ -902,9 +878,8 @@ class TestLowpanId(unittest.TestCase):
# GIVEN
stable = any_stable()
lowpan_id = network_data.LowpanId(
any_c(), any_cid(), any_context_length(), stable
)
lowpan_id = network_data.LowpanId(any_c(), any_cid(),
any_context_length(), stable)
# WHEN
actual_stable = lowpan_id.stable
@@ -914,9 +889,9 @@ class TestLowpanId(unittest.TestCase):
class TestLowpanIdFactory(unittest.TestCase):
def test_should_create_LowpanId_from_bytearray_when_parse_method_is_called(
self
):
self):
# GIVEN
c = any_c()
cid = any_cid()
@@ -926,8 +901,7 @@ class TestLowpanIdFactory(unittest.TestCase):
factory = network_data.LowpanIdFactory()
data = convert_lowpan_id_to_bytearray(
network_data.LowpanId(c, cid, context_length, stable)
)
network_data.LowpanId(c, cid, context_length, stable))
message_info = common.MessageInfo()
message_info.stable = stable
@@ -943,6 +917,7 @@ class TestLowpanIdFactory(unittest.TestCase):
class TestService(unittest.TestCase):
def test_should_return_t_value_when_t_property_is_called(self):
# GIVEN
t = any_t()
@@ -984,7 +959,7 @@ class TestService(unittest.TestCase):
self.assertEqual(_id, actual_id)
def test_should_return_enterprise_number_value_when_enterprise_number_property_is_called(
self):
self):
# GIVEN
enterprise_number = any_enterprise_number()
@@ -1005,7 +980,7 @@ class TestService(unittest.TestCase):
self.assertEqual(enterprise_number, actual_enterprise_number)
def test_should_return_service_data_length_value_when_service_data_length_property_is_called(
self):
self):
# GIVEN
service_data_length = any_service_data_length()
@@ -1026,7 +1001,7 @@ class TestService(unittest.TestCase):
self.assertEqual(service_data_length, actual_service_data_length)
def test_should_return_service_data_value_when_service_data_property_is_called(
self):
self):
# GIVEN
service_data = any_service_data()
@@ -1047,8 +1022,7 @@ class TestService(unittest.TestCase):
self.assertEqual(service_data, actual_service_data)
def test_should_return_sub_tlvs_value_when_sub_tlvs_property_is_called(
self
):
self):
# GIVEN
sub_tlvs = any_service_sub_tlvs()
@@ -1090,15 +1064,14 @@ class TestService(unittest.TestCase):
class TestServiceSubTlvsFactory(unittest.TestCase):
def test_should_create_SubTlvs_from_bytearray_when_parse_method_is_called(
self
):
self):
# GIVEN
sub_tlvs = any_service_sub_tlvs()
factory = network_data.ServiceSubTlvsFactory(
config.create_default_network_data_service_sub_tlvs_factories()
)
config.create_default_network_data_service_sub_tlvs_factories())
data = convert_service_sub_tlvs_to_bytearray(sub_tlvs)
@@ -1111,9 +1084,9 @@ class TestServiceSubTlvsFactory(unittest.TestCase):
class TestServiceFactory(unittest.TestCase):
def test_should_create_Service_from_bytearray_when_parse_method_is_called(
self
):
self):
# GIVEN
t = any_t()
_id = any_id()
@@ -1124,8 +1097,7 @@ class TestServiceFactory(unittest.TestCase):
stable = any_stable()
factory = network_data.ServiceFactory(
config.create_default_network_data_service_sub_tlvs_factory()
)
config.create_default_network_data_service_sub_tlvs_factory())
data = convert_service_to_bytearray(
network_data.Service(
@@ -1136,8 +1108,7 @@ class TestServiceFactory(unittest.TestCase):
service_data,
sub_tlvs,
stable,
)
)
))
message_info = common.MessageInfo()
message_info.stable = stable
@@ -1150,23 +1121,20 @@ class TestServiceFactory(unittest.TestCase):
self.assertEqual(t, actual_service.t)
self.assertEqual(_id, actual_service.id)
self.assertEqual(enterprise_number, actual_service.enterprise_number)
self.assertEqual(
service_data_length, actual_service.service_data_length
)
self.assertEqual(service_data_length,
actual_service.service_data_length)
self.assertEqual(service_data, actual_service.service_data)
self.assertEqual(sub_tlvs, actual_service.sub_tlvs)
class TestServer(unittest.TestCase):
def test_should_return_server_16_value_when_server_16_property_is_called(
self
):
self):
# GIVEN
server_16 = any_server_16()
server = network_data.Server(
server_16, any_server_data(), any_stable()
)
server = network_data.Server(server_16, any_server_data(), any_stable())
# WHEN
actual_server_16 = server.server_16
@@ -1175,13 +1143,11 @@ class TestServer(unittest.TestCase):
self.assertEqual(server_16, actual_server_16)
def test_should_return_server_data_value_when_server_data_property_is_called(
self):
self):
# GIVEN
server_data = any_server_data()
server = network_data.Server(
any_server_16(), server_data, any_stable()
)
server = network_data.Server(any_server_16(), server_data, any_stable())
# WHEN
actual_server_data = server.server_data
@@ -1193,9 +1159,7 @@ class TestServer(unittest.TestCase):
# GIVEN
stable = any_stable()
server = network_data.Server(
any_server_16(), any_server_data(), stable
)
server = network_data.Server(any_server_16(), any_server_data(), stable)
# WHEN
actual_stable = server.stable
@@ -1205,9 +1169,9 @@ class TestServer(unittest.TestCase):
class TestServerFactory(unittest.TestCase):
def test_should_create_Server_from_bytearray_when_parse_method_is_called(
self
):
self):
# GIVEN
server_16 = any_server_16()
server_data = any_server_data()
@@ -1216,8 +1180,7 @@ class TestServerFactory(unittest.TestCase):
factory = network_data.ServerFactory()
data = convert_server_to_bytearray(
network_data.Server(server_16, server_data, stable)
)
network_data.Server(server_16, server_data, stable))
message_info = common.MessageInfo()
message_info.stable = stable