|
| 1 | +# Copyright 2017-2019 Open Source Robotics Foundation, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from collections import OrderedDict |
| 16 | + |
| 17 | +from rosidl_runtime_py import message_to_csv |
| 18 | +from rosidl_runtime_py import message_to_ordereddict |
| 19 | +from rosidl_runtime_py import message_to_yaml |
| 20 | +from rosidl_runtime_py.convert import _convert_value |
| 21 | + |
| 22 | +from test_msgs import message_fixtures |
| 23 | + |
| 24 | + |
| 25 | +def test_primitives(): |
| 26 | + # Smoke-test the formatters on a bunch of messages |
| 27 | + msgs = [] |
| 28 | + msgs.extend(message_fixtures.get_msg_bounded_array_nested()) |
| 29 | + msgs.extend(message_fixtures.get_msg_bounded_array_primitives()) |
| 30 | + msgs.extend(message_fixtures.get_msg_builtins()) |
| 31 | + msgs.extend(message_fixtures.get_msg_dynamic_array_nested()) |
| 32 | + msgs.extend(message_fixtures.get_msg_dynamic_array_primitives()) |
| 33 | + msgs.extend(message_fixtures.get_msg_dynamic_array_primitives_nested()) |
| 34 | + msgs.extend(message_fixtures.get_msg_empty()) |
| 35 | + msgs.extend(message_fixtures.get_msg_nested()) |
| 36 | + msgs.extend(message_fixtures.get_msg_primitives()) |
| 37 | + msgs.extend(message_fixtures.get_msg_static_array_nested()) |
| 38 | + msgs.extend(message_fixtures.get_msg_static_array_primitives()) |
| 39 | + for m in msgs: |
| 40 | + message_to_csv(m, 100) |
| 41 | + message_to_csv(m, None) |
| 42 | + message_to_ordereddict(m, 100) |
| 43 | + message_to_ordereddict(m, None) |
| 44 | + message_to_yaml(m, 100) |
| 45 | + message_to_yaml(m, None) |
| 46 | + |
| 47 | + |
| 48 | +def test_convert_primitives(): |
| 49 | + assert 5 == _convert_value(5) |
| 50 | + assert 5 == _convert_value(5, truncate_length=0) |
| 51 | + assert 5 == _convert_value(5, truncate_length=1) |
| 52 | + assert 5 == _convert_value(5, truncate_length=10000) |
| 53 | + assert 42.0 == _convert_value(42.0) |
| 54 | + assert 42.0 == _convert_value(42.0, truncate_length=0) |
| 55 | + assert 42.0 == _convert_value(42.0, truncate_length=1) |
| 56 | + assert 42.0 == _convert_value(42.0, truncate_length=10000) |
| 57 | + assert True is _convert_value(True) |
| 58 | + assert True is _convert_value(True, truncate_length=0) |
| 59 | + assert True is _convert_value(True, truncate_length=1) |
| 60 | + assert True is _convert_value(True, truncate_length=10000) |
| 61 | + assert False is _convert_value(False) |
| 62 | + assert False is _convert_value(False, truncate_length=0) |
| 63 | + assert False is _convert_value(False, truncate_length=1) |
| 64 | + assert False is _convert_value(False, truncate_length=10000) |
| 65 | + |
| 66 | + |
| 67 | +def test_convert_tuple(): |
| 68 | + assert (1, 2, 3) == _convert_value((1, 2, 3)) |
| 69 | + assert ('...',) == _convert_value((1, 2, 3), truncate_length=0) |
| 70 | + assert (1, 2, '...') == _convert_value((1, 2, 3), truncate_length=2) |
| 71 | + assert ('123', '456', '789') == _convert_value(('123', '456', '789')) |
| 72 | + assert ('12...', '45...', '...') == _convert_value(('123', '456', '789'), truncate_length=2) |
| 73 | + assert ('123', '456', '789') == _convert_value(('123', '456', '789'), truncate_length=5) |
| 74 | + |
| 75 | + |
| 76 | +def test_convert_list(): |
| 77 | + assert [1, 2, 3] == _convert_value([1, 2, 3]) |
| 78 | + assert ['...'] == _convert_value([1, 2, 3], truncate_length=0) |
| 79 | + assert [1, 2, '...'] == _convert_value([1, 2, 3], truncate_length=2) |
| 80 | + assert ['123', '456', '789'] == _convert_value(['123', '456', '789']) |
| 81 | + assert ['12...', '45...', '...'] == _convert_value(['123', '456', '789'], truncate_length=2) |
| 82 | + assert ['123', '456', '789'] == _convert_value(['123', '456', '789'], truncate_length=5) |
| 83 | + |
| 84 | + |
| 85 | +def test_convert_str(): |
| 86 | + assert 'hello world' == _convert_value('hello world') |
| 87 | + assert 'hello...' == _convert_value('hello world', truncate_length=5) |
| 88 | + assert 'hello world' == _convert_value('hello world', truncate_length=1000) |
| 89 | + |
| 90 | + |
| 91 | +def test_convert_bytes(): |
| 92 | + assert 'hello world' == _convert_value(b'hello world') |
| 93 | + assert 'hello...' == _convert_value(b'hello world', truncate_length=5) |
| 94 | + assert 'hello world' == _convert_value(b'hello world', truncate_length=1000) |
| 95 | + |
| 96 | + |
| 97 | +def test_convert_ordered_dict(): |
| 98 | + assert OrderedDict([(1, 'a'), ('2', 'b')]) == _convert_value( |
| 99 | + OrderedDict([(1, 'a'), ('2', 'b')])) |
| 100 | + assert OrderedDict([(1, 'a'), ('2', 'b')]) == _convert_value( |
| 101 | + OrderedDict([(1, 'a'), ('2', 'b')]), truncate_length=1) |
| 102 | + assert OrderedDict([(1, 'a'), ('2', 'b')]) == _convert_value( |
| 103 | + OrderedDict([(1, 'a'), ('2', 'b')]), truncate_length=1000) |
| 104 | + assert OrderedDict([(1, 'a...'), ('234', 'b...')]) == _convert_value( |
| 105 | + OrderedDict([(1, 'abc'), ('234', 'bcd')]), truncate_length=1) |
| 106 | + |
| 107 | + |
| 108 | +def test_convert_dict(): |
| 109 | + assert {1: 'a', '2': 'b'} == _convert_value({1: 'a', '2': 'b'}) |
| 110 | + assert {1: 'a', '2': 'b'} == _convert_value({1: 'a', '2': 'b'}, truncate_length=1) |
| 111 | + assert {1: 'a', '2': 'b'} == _convert_value({1: 'a', '2': 'b'}, truncate_length=1000) |
| 112 | + assert {1: 'a...', '234': 'b...'} == _convert_value( |
| 113 | + {1: 'abc', '234': 'bcd'}, truncate_length=1) |
0 commit comments