Skip to content

Commit 50afb33

Browse files
authored
Enable tests for 'char' type fields (#91)
* Enable tests for 'char' type fields For legacy interfaces (ie. .msg, .srv, and .action), the 'char' is mapped to a 'uint8' type. Signed-off-by: Jacob Perron <[email protected]> * Mention .msg in comments Signed-off-by: Jacob Perron <[email protected]>
1 parent b93c7bf commit 50afb33

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

rosidl_generator_py/test/test_interfaces.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ def test_basic_types():
4343
assert isinstance(msg.bool_value, bool)
4444
assert isinstance(msg.byte_value, bytes)
4545
assert 1 == len(msg.byte_value)
46-
# TODO(jacobperron): char_value is an int, but shouldn't it be a string?
47-
# assert isinstance(msg.char_value, str)
48-
# assert 1 == len(msg.char_value)
46+
# for legacy reasons, 'char' from a .msg interface maps to 'uint8'
47+
assert isinstance(msg.char_value, int)
4948
assert isinstance(msg.float32_value, float)
5049
assert isinstance(msg.float64_value, float)
5150
assert isinstance(msg.int8_value, int)
@@ -60,8 +59,7 @@ def test_basic_types():
6059
# default values
6160
assert msg.bool_value is False
6261
assert bytes([0]) == msg.byte_value
63-
# TODO(jacobperron): This test fails as 'char_value' has type int
64-
# assert '' == msg.char_value
62+
assert 0 == msg.char_value
6563
assert 0.0 == msg.float32_value
6664
assert 0.0 == msg.float64_value
6765
assert 0 == msg.int8_value
@@ -78,8 +76,8 @@ def test_basic_types():
7876
assert msg.bool_value is True
7977
msg.byte_value = b'2'
8078
assert bytes([50]) == msg.byte_value
81-
# msg.char_value = 'R'
82-
# assert 'R' == msg.char_value
79+
msg.char_value = 42
80+
assert 42 == msg.char_value
8381
msg.float32_value = 1.125
8482
assert 1.125 == msg.float32_value
8583
msg.float64_value = 1.125
@@ -288,7 +286,8 @@ def test_arrays():
288286
# types
289287
assert isinstance(msg.bool_values, list)
290288
assert isinstance(msg.byte_values, list)
291-
# assert isinstance(msg.char_values, list)
289+
# for legacy reasons, 'char' from a .msg interface maps to 'uint8'
290+
assert isinstance(msg.char_values, numpy.ndarray)
292291
assert isinstance(msg.float32_values, numpy.ndarray)
293292
assert isinstance(msg.float64_values, numpy.ndarray)
294293
assert isinstance(msg.int8_values, numpy.ndarray)
@@ -324,9 +323,9 @@ def test_arrays():
324323
list_of_byte = [b'a', b'b', b'c']
325324
msg.byte_values = list_of_byte
326325
assert list_of_byte == msg.byte_values
327-
# list_of_char = ['a', 'b', 'c']
328-
# msg.char_values = list_of_char
329-
# assert list_of_char == msg.char_values
326+
list_of_char = [0, 1, 255]
327+
msg.char_values = list_of_char
328+
assert numpy.array_equal(list_of_char, msg.char_values)
330329
list_of_float32 = [0.0, -1.125, 1.125]
331330
msg.float32_values = list_of_float32
332331
assert numpy.allclose(list_of_float32, msg.float32_values)
@@ -363,8 +362,8 @@ def test_arrays():
363362
msg.bool_values = [True]
364363
with pytest.raises(AssertionError):
365364
msg.byte_values = [b'd']
366-
# with pytest.raises(AssertionError):
367-
# msg.char_values = ['d']
365+
with pytest.raises(AssertionError):
366+
msg.char_values = [0]
368367
with pytest.raises(AssertionError):
369368
msg.float32_values = [1.125]
370369
with pytest.raises(AssertionError):
@@ -391,8 +390,8 @@ def test_arrays():
391390
msg.bool_values = ['not', 'a', 'bool']
392391
with pytest.raises(AssertionError):
393392
msg.byte_values = ['not', 'a', 'byte']
394-
# with pytest.raises(AssertionError):
395-
# msg.char_values = ['not', 'a', 'char']
393+
with pytest.raises(AssertionError):
394+
msg.char_values = ['not', 'a', 'char']
396395
with pytest.raises(AssertionError):
397396
msg.float32_values = ['not', 'a', 'float32']
398397
with pytest.raises(AssertionError):
@@ -415,6 +414,10 @@ def test_arrays():
415414
msg.uint64_values = ['not', 'a', 'uint64']
416415

417416
# out of range
417+
with pytest.raises(AssertionError):
418+
setattr(msg, 'char_values', [2**8, 1, 2])
419+
with pytest.raises(AssertionError):
420+
setattr(msg, 'char_values', [-1, 1, 2])
418421
with pytest.raises(AssertionError):
419422
setattr(msg, 'int8_values', [2**8, 1, 2])
420423
with pytest.raises(AssertionError):
@@ -455,7 +458,8 @@ def test_bounded_sequences():
455458
# types
456459
assert isinstance(msg.bool_values, list)
457460
assert isinstance(msg.byte_values, list)
458-
# assert isinstance(msg.char_values, list)
461+
# for legacy reasons, 'char' from a .msg interface maps to 'uint8'
462+
assert isinstance(msg.char_values, array.array)
459463
assert isinstance(msg.float32_values, array.array)
460464
assert isinstance(msg.float64_values, array.array)
461465
assert isinstance(msg.int8_values, array.array)
@@ -472,7 +476,7 @@ def test_bounded_sequences():
472476
# defaults
473477
assert [] == msg.bool_values
474478
assert [] == msg.byte_values
475-
# assert [] == msg.char_values
479+
assert array.array('B') == msg.char_values
476480
assert array.array('f') == msg.float32_values
477481
assert array.array('d') == msg.float64_values
478482
assert array.array('b') == msg.int8_values
@@ -497,12 +501,12 @@ def test_bounded_sequences():
497501
assert list_of_byte == msg.byte_values
498502
msg.byte_values = short_list_of_byte
499503
assert short_list_of_byte == msg.byte_values
500-
# list_of_char = ['a', 'b', 'c']
501-
# short_list_of_char = ['d']
502-
# msg.char_values = list_of_char
503-
# assert list_of_char == msg.char_values
504-
# msg.char_values = short_list_of_char
505-
# assert short_list_of_char == msg.char_values
504+
list_of_char = [0, 1, 255]
505+
short_list_of_char = [0]
506+
msg.char_values = list_of_char
507+
assert array.array('B', list_of_char) == msg.char_values
508+
msg.char_values = short_list_of_char
509+
assert array.array('B', short_list_of_char) == msg.char_values
506510
list_of_float32 = [0.1, 2.3, -3.14]
507511
short_list_of_float32 = [1.125]
508512
msg.float32_values = list_of_float32
@@ -569,8 +573,8 @@ def test_bounded_sequences():
569573
msg.bool_values = [True, False, True, False]
570574
with pytest.raises(AssertionError):
571575
msg.byte_values = [b'a', b'b', b'c', b'd']
572-
# with pytest.raises(AssertionError):
573-
# msg.char_values = ['a', 'b', 'c', 'd']
576+
with pytest.raises(AssertionError):
577+
msg.char_values = [1, 2, 3, 4]
574578
with pytest.raises(AssertionError):
575579
msg.float32_values = [1.0, 2.0, 3.0, 4.0]
576580
with pytest.raises(AssertionError):
@@ -597,8 +601,8 @@ def test_bounded_sequences():
597601
msg.bool_values = ['not', 'a', 'bool']
598602
with pytest.raises(AssertionError):
599603
msg.byte_values = ['not', 'a', 'byte']
600-
# with pytest.raises(AssertionError):
601-
# msg.char_values = ['not', 'a', 'char']
604+
with pytest.raises(AssertionError):
605+
msg.char_values = ['not', 'a', 'char']
602606
with pytest.raises(AssertionError):
603607
msg.float32_values = ['not', 'a', 'float32']
604608
with pytest.raises(AssertionError):
@@ -621,6 +625,10 @@ def test_bounded_sequences():
621625
msg.uint64_values = ['not', 'a', 'uint64']
622626

623627
# out of range
628+
with pytest.raises(AssertionError):
629+
setattr(msg, 'char_values', [2**8, 1, 2])
630+
with pytest.raises(AssertionError):
631+
setattr(msg, 'char_values', [-1, 1, 2])
624632
with pytest.raises(AssertionError):
625633
setattr(msg, 'int8_values', [2**8, 1, 2])
626634
with pytest.raises(AssertionError):
@@ -660,7 +668,8 @@ def test_unbounded_sequences():
660668

661669
# types
662670
assert isinstance(msg.byte_values, list)
663-
# assert isinstance(msg.char_values, list)
671+
# for legacy reasons, 'char' from a .msg interface maps to 'uint8'
672+
assert isinstance(msg.char_values, array.array)
664673
assert isinstance(msg.float32_values, array.array)
665674
assert isinstance(msg.float64_values, array.array)
666675
assert isinstance(msg.int8_values, array.array)
@@ -677,7 +686,7 @@ def test_unbounded_sequences():
677686
# defaults
678687
assert [] == msg.bool_values
679688
assert [] == msg.byte_values
680-
# assert [] == msg.char_values
689+
assert array.array('B') == msg.char_values
681690
assert array.array('f') == msg.float32_values
682691
assert array.array('d') == msg.float64_values
683692
assert array.array('b') == msg.int8_values
@@ -696,8 +705,9 @@ def test_unbounded_sequences():
696705
list_of_byte = [b'a', b'b', b'c']
697706
msg.byte_values = list_of_byte
698707
assert list_of_byte == msg.byte_values
699-
# list_of_char = ['a', 'b', 'c']
700-
# msg.char_values = list_of_char
708+
list_of_char = [0, 1, 255]
709+
msg.char_values = list_of_char
710+
assert array.array('B', list_of_char) == msg.char_values
701711
list_of_float32 = [0.1, 2.3, -3.14]
702712
msg.float32_values = list_of_float32
703713
assert array.array('f', list_of_float32) == msg.float32_values
@@ -734,8 +744,8 @@ def test_unbounded_sequences():
734744
msg.bool_values = ['not', 'a', 'bool']
735745
with pytest.raises(AssertionError):
736746
msg.byte_values = ['not', 'a', 'byte']
737-
# with pytest.raises(AssertionError):
738-
# msg.char_values = ['not', 'a', 'char']
747+
with pytest.raises(AssertionError):
748+
msg.char_values = ['not', 'a', 'char']
739749
with pytest.raises(AssertionError):
740750
msg.float32_values = ['not', 'a', 'float32']
741751
with pytest.raises(AssertionError):

0 commit comments

Comments
 (0)