|
1 | 1 | """Unit test for KNX string object."""
|
2 | 2 | import pytest
|
3 | 3 |
|
4 |
| -from xknx.dpt import DPTString |
| 4 | +from xknx.dpt import DPTLatin1, DPTString |
5 | 5 | from xknx.exceptions import ConversionError
|
6 | 6 |
|
7 | 7 |
|
8 | 8 | class TestDPTString:
|
9 |
| - """Test class for KNX float object.""" |
| 9 | + """Test class for KNX ASCII string object.""" |
10 | 10 |
|
11 |
| - def test_value_from_documentation(self): |
12 |
| - """Test parsing and streaming Example from documentation.""" |
13 |
| - raw = ( |
14 |
| - 0x4B, |
15 |
| - 0x4E, |
16 |
| - 0x58, |
17 |
| - 0x20, |
18 |
| - 0x69, |
19 |
| - 0x73, |
20 |
| - 0x20, |
21 |
| - 0x4F, |
22 |
| - 0x4B, |
23 |
| - 0x00, |
24 |
| - 0x00, |
25 |
| - 0x00, |
26 |
| - 0x00, |
27 |
| - 0x00, |
28 |
| - ) |
29 |
| - string = "KNX is OK" |
30 |
| - assert DPTString.to_knx(string) == raw |
31 |
| - assert DPTString.from_knx(raw) == string |
32 |
| - |
33 |
| - def test_value_empty_string(self): |
34 |
| - """Test parsing and streaming empty string.""" |
35 |
| - raw = ( |
36 |
| - 0x00, |
37 |
| - 0x00, |
38 |
| - 0x00, |
39 |
| - 0x00, |
40 |
| - 0x00, |
41 |
| - 0x00, |
42 |
| - 0x00, |
43 |
| - 0x00, |
44 |
| - 0x00, |
45 |
| - 0x00, |
46 |
| - 0x00, |
47 |
| - 0x00, |
48 |
| - 0x00, |
49 |
| - 0x00, |
50 |
| - ) |
51 |
| - string = "" |
52 |
| - assert DPTString.to_knx(string) == raw |
53 |
| - assert DPTString.from_knx(raw) == string |
| 11 | + @pytest.mark.parametrize( |
| 12 | + "string,raw", |
| 13 | + [ |
| 14 | + ( |
| 15 | + "KNX is OK", |
| 16 | + (75, 78, 88, 32, 105, 115, 32, 79, 75, 0, 0, 0, 0, 0), |
| 17 | + ), |
| 18 | + ( |
| 19 | + "", |
| 20 | + (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
| 21 | + ), |
| 22 | + ( |
| 23 | + "AbCdEfGhIjKlMn", |
| 24 | + (65, 98, 67, 100, 69, 102, 71, 104, 73, 106, 75, 108, 77, 110), |
| 25 | + ), |
| 26 | + ( |
| 27 | + ".,:;-_!?$@&#%/", |
| 28 | + (46, 44, 58, 59, 45, 95, 33, 63, 36, 64, 38, 35, 37, 47), |
| 29 | + ), |
| 30 | + ], |
| 31 | + ) |
| 32 | + @pytest.mark.parametrize("test_dpt", [DPTString, DPTLatin1]) |
| 33 | + def test_values(self, string, raw, test_dpt): |
| 34 | + """Test parsing and streaming strings.""" |
| 35 | + assert test_dpt.to_knx(string) == raw |
| 36 | + assert test_dpt.from_knx(raw) == string |
54 | 37 |
|
55 |
| - def test_value_max_string(self): |
56 |
| - """Test parsing and streaming large string.""" |
57 |
| - raw = ( |
58 |
| - 0x41, |
59 |
| - 0x41, |
60 |
| - 0x41, |
61 |
| - 0x41, |
62 |
| - 0x41, |
63 |
| - 0x42, |
64 |
| - 0x42, |
65 |
| - 0x42, |
66 |
| - 0x42, |
67 |
| - 0x42, |
68 |
| - 0x43, |
69 |
| - 0x43, |
70 |
| - 0x43, |
71 |
| - 0x43, |
72 |
| - ) |
73 |
| - string = "AAAAABBBBBCCCC" |
74 |
| - assert DPTString.to_knx(string) == raw |
75 |
| - assert DPTString.from_knx(raw) == string |
76 |
| - |
77 |
| - def test_value_special_chars(self): |
78 |
| - """Test parsing and streaming string with special chars.""" |
79 |
| - raw = ( |
80 |
| - 0x48, |
81 |
| - 0x65, |
82 |
| - 0x79, |
83 |
| - 0x21, |
84 |
| - 0x3F, |
85 |
| - 0x24, |
86 |
| - 0x20, |
87 |
| - 0xC4, |
88 |
| - 0xD6, |
89 |
| - 0xDC, |
90 |
| - 0xE4, |
91 |
| - 0xF6, |
92 |
| - 0xFC, |
93 |
| - 0xDF, |
94 |
| - ) |
95 |
| - string = "Hey!?$ ÄÖÜäöüß" |
96 |
| - assert DPTString.to_knx(string) == raw |
97 |
| - assert DPTString.from_knx(raw) == string |
98 |
| - |
99 |
| - def test_to_knx_invalid_chars(self): |
100 |
| - """Test streaming string with invalid chars.""" |
101 |
| - raw = ( |
102 |
| - 0x4D, |
103 |
| - 0x61, |
104 |
| - 0x74, |
105 |
| - 0x6F, |
106 |
| - 0x75, |
107 |
| - 0x3F, |
108 |
| - 0x00, |
109 |
| - 0x00, |
110 |
| - 0x00, |
111 |
| - 0x00, |
112 |
| - 0x00, |
113 |
| - 0x00, |
114 |
| - 0x00, |
115 |
| - 0x00, |
116 |
| - ) |
117 |
| - string = "Matouš" |
118 |
| - knx_string = "Matou?" |
| 38 | + @pytest.mark.parametrize( |
| 39 | + "string,knx_string,raw", |
| 40 | + [ |
| 41 | + ( |
| 42 | + "Matouš", |
| 43 | + "Matou?", |
| 44 | + (77, 97, 116, 111, 117, 63, 0, 0, 0, 0, 0, 0, 0, 0), |
| 45 | + ), |
| 46 | + ( |
| 47 | + "Gänsefüßchen", |
| 48 | + "G?nsef??chen", |
| 49 | + (71, 63, 110, 115, 101, 102, 63, 63, 99, 104, 101, 110, 0, 0), |
| 50 | + ), |
| 51 | + ], |
| 52 | + ) |
| 53 | + def test_to_knx_ascii_invalid_chars(self, string, knx_string, raw): |
| 54 | + """Test streaming ASCII string with invalid chars.""" |
119 | 55 | assert DPTString.to_knx(string) == raw
|
120 | 56 | assert DPTString.from_knx(raw) == knx_string
|
121 | 57 |
|
| 58 | + @pytest.mark.parametrize( |
| 59 | + "string,raw", |
| 60 | + [ |
| 61 | + ( |
| 62 | + "Gänsefüßchen", |
| 63 | + (71, 228, 110, 115, 101, 102, 252, 223, 99, 104, 101, 110, 0, 0), |
| 64 | + ), |
| 65 | + ( |
| 66 | + "àáâãåæçèéêëìíî", |
| 67 | + (224, 225, 226, 227, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238), |
| 68 | + ), |
| 69 | + ], |
| 70 | + ) |
| 71 | + def test_to_knx_latin_1(self, string, raw): |
| 72 | + """Test streaming Latin-1 strings.""" |
| 73 | + assert DPTLatin1.to_knx(string) == raw |
| 74 | + assert DPTLatin1.from_knx(raw) == string |
| 75 | + |
122 | 76 | def test_to_knx_too_long(self):
|
123 | 77 | """Test serializing DPTString to KNX with wrong value (to long)."""
|
124 | 78 | with pytest.raises(ConversionError):
|
125 | 79 | DPTString.to_knx("AAAAABBBBBCCCCx")
|
126 | 80 |
|
127 |
| - def test_from_knx_wrong_parameter_too_large(self): |
128 |
| - """Test parsing of KNX string with too many elements.""" |
129 |
| - raw = ( |
130 |
| - 0x00, |
131 |
| - 0x00, |
132 |
| - 0x00, |
133 |
| - 0x00, |
134 |
| - 0x00, |
135 |
| - 0x00, |
136 |
| - 0x00, |
137 |
| - 0x00, |
138 |
| - 0x00, |
139 |
| - 0x00, |
140 |
| - 0x00, |
141 |
| - 0x00, |
142 |
| - 0x00, |
143 |
| - 0x00, |
144 |
| - 0x00, |
145 |
| - ) |
146 |
| - with pytest.raises(ConversionError): |
147 |
| - DPTString.from_knx(raw) |
148 |
| - |
149 |
| - def test_from_knx_wrong_parameter_too_small(self): |
150 |
| - """Test parsing of KNX string with too less elements.""" |
151 |
| - raw = ( |
152 |
| - 0x00, |
153 |
| - 0x00, |
154 |
| - 0x00, |
155 |
| - 0x00, |
156 |
| - 0x00, |
157 |
| - 0x00, |
158 |
| - 0x00, |
159 |
| - 0x00, |
160 |
| - 0x00, |
161 |
| - 0x00, |
162 |
| - 0x00, |
163 |
| - 0x00, |
164 |
| - 0x00, |
165 |
| - ) |
| 81 | + @pytest.mark.parametrize( |
| 82 | + "raw", |
| 83 | + [ |
| 84 | + ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),), |
| 85 | + ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),), |
| 86 | + ], |
| 87 | + ) |
| 88 | + def test_from_knx_wrong_parameter_length(self, raw): |
| 89 | + """Test parsing of KNX string with wrong elements length.""" |
166 | 90 | with pytest.raises(ConversionError):
|
167 | 91 | DPTString.from_knx(raw)
|
0 commit comments