781d9299cf4d432b38e816918a51da83232f1139
[deliverable/titan.core.git] / regression_test / XML / EXER-whitepaper / UseNil.ttcnpp
1 /******************************************************************************
2 * Copyright (c) 2000-2014 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 ******************************************************************************/
8 module UseNil {
9 modulepar boolean UseNil_verbose := false;
10 #define verbose UseNil_verbose
11 #include "../macros.ttcnin"
12
13 type component Nil {}
14
15 type enumerated ProductColor0 { red(0), green(1), blue(2) }
16
17 // useNil on the type (record)
18 type record Size {
19 integer sizeval optional
20 }
21 with { variant "useNil" }
22
23 type record NilProduct {
24 charstring name,
25 ProductColor0 color,
26 Size size
27 }
28
29 DECLARE_XER_ENCODERS(NilProduct, nilp);
30 DECLARE_EXER_ENCODERS(NilProduct, nilp);
31
32 DECLARE_XER_ENCODERS(Size, sz);
33 DECLARE_EXER_ENCODERS(Size, sz);
34
35 const NilProduct absent := {
36 name := "no shirt",
37 color := red,
38 size := { omit }
39 }
40
41 const NilProduct present_ := {
42 name := "shirt",
43 color := red,
44 size := { 10 }
45 }
46
47
48 const universal charstring str_absent_e_correct :=
49 "<NilProduct xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'>\n" &
50 "\t<name>no shirt</name>\n" &
51 "\t<color>red</color>\n" &
52 "\t<size xsi:nil=\'true\'/>\n" &
53 "</NilProduct>\n" &
54 "\n";
55
56 const universal charstring str_absent_e :=
57 "<NilProduct xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'>\n" &
58 "\t<name>no shirt</name>\n" &
59 "\t<color>red</color>\n" &
60 "\t<size xsi:nil=\'true\'/>\n" &
61 "</NilProduct>\n" &
62 "\n";
63
64 const universal charstring str_absent_b :=
65 "<NilProduct>\n" &
66 "\t<name>no shirt</name>\n" &
67 "\t<color><red/></color>\n" &
68 "\t<size/>\n" &
69 "</NilProduct>\n" &
70 "\n";
71
72 const universal charstring str_present_e_correct :=
73 "<NilProduct xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'>\n" &
74 "\t<name>shirt</name>\n" &
75 "\t<color>red</color>\n" &
76 "\t<size xsi:nil='false'>10</size>\n" &
77 "</NilProduct>\n" &
78 "\n";
79
80 const universal charstring str_present_e :=
81 "<NilProduct>\n" &
82 "\t<name>shirt</name>\n" &
83 "\t<color>red</color>\n" &
84 "\t<size>10</size>\n" & // HO37920: no \t after 10
85 "</NilProduct>\n" &
86 "\n";
87
88 const universal charstring str_present_b :=
89 "<NilProduct>\n" &
90 "\t<name>shirt</name>\n" &
91 "\t<color><red/></color>\n" &
92 "\t<size>\n" &
93 "\t\t<sizeval>10</sizeval>\n" &
94 "\t</size>\n" &
95 "</NilProduct>\n" &
96 "\n";
97
98 testcase encode_nil_absent() runs on Nil
99 {
100 CHECK_METHOD(bxer_enc_nilp, absent, str_absent_b);
101 CHECK_METHOD(exer_enc_nilp, absent, str_absent_e);
102 }
103
104 testcase encode_nil_present() runs on Nil
105 {
106 CHECK_METHOD(bxer_enc_nilp, present_, str_present_b);
107 CHECK_METHOD(exer_enc_nilp, present_, str_present_e);
108 }
109
110 testcase decode_nil_absent() runs on Nil
111 {
112 CHECK_DECODE(bxer_dec_nilp, str_absent_b, NilProduct, absent);
113 CHECK_DECODE(exer_dec_nilp, str_absent_e, NilProduct, absent);
114 }
115
116 testcase decode_nil_present() runs on Nil
117 {
118 CHECK_DECODE(bxer_dec_nilp, str_present_b, NilProduct, present_);
119 CHECK_DECODE(exer_dec_nilp, str_present_e, NilProduct, present_);
120 }
121
122 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
123
124 const universal charstring str_sz_absent_e :=
125 "<Size xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' xsi:nil=\'true\'/>\n\n";
126
127 const universal charstring str_sz_absent_b :=
128 "<Size/>\n\n";
129
130 const universal charstring str_sz_present_e :=
131 "<Size>10</Size>\n\n";
132
133 const universal charstring str_sz_present_b :=
134 "<Size>\n" &
135 "\t<sizeval>10</sizeval>\n" &
136 "</Size>\n\n";
137
138 testcase encode_size_absent() runs on Nil
139 {
140 CHECK_METHOD(bxer_enc_sz, absent.size, str_sz_absent_b);
141 CHECK_METHOD(exer_enc_sz, absent.size, str_sz_absent_e);
142 }
143
144 testcase encode_size_present() runs on Nil
145 {
146 CHECK_METHOD(bxer_enc_sz, present_.size, str_sz_present_b);
147 CHECK_METHOD(exer_enc_sz, present_.size, str_sz_present_e);
148 }
149
150 testcase decode_size_absent() runs on Nil
151 {
152 CHECK_DECODE(bxer_dec_sz, str_sz_absent_b, Size, absent.size);
153 CHECK_DECODE(exer_dec_sz, str_sz_absent_e, Size, absent.size);
154 }
155
156 testcase decode_size_present() runs on Nil
157 {
158 CHECK_DECODE(bxer_dec_sz, str_sz_present_b, Size, present_.size);
159 CHECK_DECODE(exer_dec_sz, str_sz_present_e, Size, present_.size);
160 }
161
162 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
163
164 type record Size2 {
165 integer sizeval optional
166 }
167
168 type record NilProduct2 {
169 charstring name,
170 ProductColor0 color,
171 Size2 size // useNil on the field (record)
172 }
173 with { variant (size) "useNil" }
174
175 DECLARE_XER_ENCODERS(NilProduct2, np2);
176 DECLARE_EXER_ENCODERS(NilProduct2, np2);
177
178 const NilProduct2 absent2 := {
179 name := "no shirt",
180 color := red,
181 size := { omit }
182 }
183
184 const NilProduct2 present2 := {
185 name := "shirt",
186 color := red,
187 size := { 10 }
188 }
189
190 const universal charstring str_absent2_b :=
191 "<NilProduct2>\n" &
192 "\t<name>no shirt</name>\n" &
193 "\t<color><red/></color>\n" &
194 "\t<size/>\n" &
195 "</NilProduct2>\n" &
196 "\n";
197
198 const universal charstring str_absent2_e :=
199 "<NilProduct2 xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'>\n" &
200 "\t<name>no shirt</name>\n" &
201 "\t<color>red</color>\n" &
202 "\t<size xsi:nil=\'true\'/>\n" &
203 "</NilProduct2>\n" &
204 "\n";
205
206
207 const universal charstring str_present2_b :=
208 "<NilProduct2>\n" &
209 "\t<name>shirt</name>\n" &
210 "\t<color><red/></color>\n" &
211 "\t<size>\n" &
212 "\t\t<sizeval>10</sizeval>\n" &
213 "\t</size>\n" &
214 "</NilProduct2>\n" &
215 "\n";
216
217 const universal charstring str_present2_e :=
218 "<NilProduct2>\n" &
219 "\t<name>shirt</name>\n" &
220 "\t<color>red</color>\n" &
221 "\t<size>10</size>\n" & // HO37920: no \t after 10
222 "</NilProduct2>\n" &
223 "\n";
224
225
226 testcase encode_np2_absent() runs on Nil
227 {
228 CHECK_METHOD(bxer_enc_np2, absent2, str_absent2_b);
229 CHECK_METHOD(exer_enc_np2, absent2, str_absent2_e);
230 }
231
232 testcase encode_np2_present() runs on Nil
233 {
234 CHECK_METHOD(bxer_enc_np2, present2, str_present2_b);
235 CHECK_METHOD(exer_enc_np2, present2, str_present2_e);
236 }
237
238 testcase decode_np2_absent() runs on Nil
239 {
240 CHECK_DECODE(bxer_dec_np2, str_absent2_b, NilProduct2, absent2);
241 CHECK_DECODE(exer_dec_np2, str_absent2_e, NilProduct2, absent2);
242 }
243
244 testcase decode_np2_present() runs on Nil
245 {
246 CHECK_DECODE(bxer_dec_np2, str_present2_b, NilProduct2, present2);
247 CHECK_DECODE(exer_dec_np2, str_present2_e, NilProduct2, present2);
248 }
249
250 /* * * * * * * Complex type as the optional member * * * * * * */
251
252 type record NilProduct3 {
253 record {
254 charstring name,
255 record {
256 integer sizeval,
257 charstring unit
258 } size optional
259 } container
260 }
261 with {
262 variant (container) "useNil";
263 variant (container.name) "attribute";
264 }
265
266
267 DECLARE_XER_ENCODERS(NilProduct3, np3);
268 DECLARE_EXER_ENCODERS(NilProduct3, np3);
269
270 const NilProduct3 absent3 := {{
271 name := "no shirt",
272 size := omit
273 }}
274
275 const NilProduct3 present3 := {{
276 name := "shirt",
277 size := { 10, "inches" }
278 }}
279
280 const universal charstring str_absent3_b :=
281 "<NilProduct3>\n" &
282 "\t<container>\n" &
283 "\t\t<name>no shirt</name>\n" &
284 "\t</container>\n" &
285 "</NilProduct3>\n" &
286 "\n";
287
288 const universal charstring str_absent3_e :=
289 "<NilProduct3 xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'>\n" &
290 "\t<container name='no shirt' xsi:nil='true'/>\n" &
291 "</NilProduct3>\n\n";
292
293
294 const universal charstring str_present3_b :=
295 "<NilProduct3>\n" &
296 "\t<container>\n" &
297 "\t\t<name>shirt</name>\n" &
298 "\t\t<size>\n" &
299 "\t\t\t<sizeval>10</sizeval>\n" &
300 "\t\t\t<unit>inches</unit>\n" &
301 "\t\t</size>\n" &
302 "\t</container>\n" &
303 "</NilProduct3>\n" &
304 "\n";
305
306 const universal charstring str_present3_e :=
307 "<NilProduct3>\n" &
308 "\t<container name='shirt'>\n" &
309 // no <size>
310 "\t\t<sizeval>10</sizeval>\n" &
311 "\t\t<unit>inches</unit>\n" &
312 // no </size>
313 "\t</container>\n" & // HO37920: a single tab is needed
314 "</NilProduct3>\n" &
315 "\n";
316
317
318 testcase encode_np3_absent() runs on Nil
319 {
320 CHECK_METHOD(bxer_enc_np3, absent3, str_absent3_b);
321 CHECK_METHOD(exer_enc_np3, absent3, str_absent3_e);
322 }
323
324 testcase encode_np3_present() runs on Nil
325 {
326 CHECK_METHOD(bxer_enc_np3, present3, str_present3_b);
327 CHECK_METHOD(exer_enc_np3, present3, str_present3_e);
328 }
329
330 testcase decode_np3_absent() runs on Nil
331 {
332 CHECK_DECODE(bxer_dec_np3, str_absent3_b, NilProduct3, absent3);
333 CHECK_DECODE(exer_dec_np3, str_absent3_e, NilProduct3, absent3);
334 }
335
336 testcase decode_np3_present() runs on Nil
337 {
338 CHECK_DECODE(bxer_dec_np3, str_present3_b, NilProduct3, present3);
339 CHECK_DECODE(exer_dec_np3, str_present3_e, NilProduct3, present3);
340 }
341
342 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
343 Regression test for HO85831.
344 */
345
346 type enumerated szunet {
347 O,
348 IO,
349 CIO,
350 ACIO,
351 KACIO,
352 AKACIO,
353 VAKACIO
354 }
355
356 type record of integer intlist;
357
358 type union ximsa_ssp_isp_content {
359 record of record {
360 boolean b optional
361 } bool_list,
362 record of record {
363 integer i optional
364 } integer_list,
365 record of record {
366 charstring s optional
367 } charstring_list,
368 record of record {
369 universal charstring s optional
370 } ustring_list,
371 record of record {
372 szunet v optional
373 } enum_list
374 // float is always written with six decimal places, hence min. 8 characters
375 // bitstring and hexstring can not be character-encodable
376 // octetstring can be character-encodable (with base64), but it must have
377 // an even number of hex digits
378 }
379 with {
380 variant (integer_list[-]) "useNil";
381 variant (charstring_list[-]) "useNil";
382 variant (ustring_list[-]) "useNil";
383 variant (bool_list[-]) "useNil";
384 variant (bool_list[-].b) "text"; // needed for boolean to produce one-character encoding: "1" or "0"
385 variant (enum_list[-]) "useNil";
386 };
387
388 const ximsa_ssp_isp_content imsar := {
389 integer_list := {
390 { 2 },
391 { 42 }
392 }
393 }
394
395 const universal charstring str_imsar_e :=
396 "<ximsa_ssp_isp_content>\n" &
397 "\t<integer_list>\n" &
398
399 "\t\t<SEQUENCE>2</SEQUENCE>\n" &
400 //bad: "\t\t<SEQUENCE/>\n" &
401
402 "\t\t<SEQUENCE>42</SEQUENCE>\n" &
403 "\t</integer_list>\n" &
404 "</ximsa_ssp_isp_content>\n\n" ;
405
406 const ximsa_ssp_isp_content imsastr := {
407 charstring_list := {
408 { "two" },
409 { "!" }
410 }
411 }
412
413 const universal charstring str_imsastr_e :=
414 "<ximsa_ssp_isp_content>\n" &
415 "\t<charstring_list>\n" &
416 "\t\t<SEQUENCE>two</SEQUENCE>\n" &
417
418 "\t\t<SEQUENCE>!</SEQUENCE>\n" &
419 //bad: "\t\t<SEQUENCE/>\n" &
420
421 "\t</charstring_list>\n" &
422 "</ximsa_ssp_isp_content>\n\n" ;
423
424 const ximsa_ssp_isp_content imsaustr := {
425 ustring_list := {
426 { "|_|" },
427 { "u" }
428 }
429 }
430
431 const universal charstring str_imsaustr_e :=
432 "<ximsa_ssp_isp_content>\n" &
433 "\t<ustring_list>\n" &
434 "\t\t<SEQUENCE>|_|</SEQUENCE>\n" &
435
436 "\t\t<SEQUENCE>u</SEQUENCE>\n" &
437 //bad: "\t\t<SEQUENCE/>\n" &
438
439 "\t</ustring_list>\n" &
440 "</ximsa_ssp_isp_content>\n\n" ;
441
442
443 const boolean file_not_found := false;
444 const ximsa_ssp_isp_content bimsar := {
445 bool_list := {
446 { true },
447 { false },
448 { file_not_found }
449 }
450 }
451
452 const universal charstring str_bimsar_e :=
453 "<ximsa_ssp_isp_content>\n" &
454 "\t<bool_list>\n" &
455 "\t\t<SEQUENCE>1</SEQUENCE>\n" &
456 "\t\t<SEQUENCE>0</SEQUENCE>\n" &
457 "\t\t<SEQUENCE>0</SEQUENCE>\n" &
458 "\t</bool_list>\n" &
459 "</ximsa_ssp_isp_content>\n\n" ;
460
461 const ximsa_ssp_isp_content eimsar := {
462 enum_list := {
463 { O },
464 { ACIO }
465 }
466 }
467
468 const universal charstring str_eimsar_e :=
469 "<ximsa_ssp_isp_content>\n" &
470 "\t<enum_list>\n" &
471 "\t\t<SEQUENCE>O</SEQUENCE>\n" &
472 "\t\t<SEQUENCE>ACIO</SEQUENCE>\n" &
473 "\t</enum_list>\n" &
474 "</ximsa_ssp_isp_content>\n\n" ;
475
476
477 DECLARE_EXER_ENCODERS(ximsa_ssp_isp_content, x);
478
479 testcase encode_usenil_recof() runs on Nil
480 {
481 CHECK_METHOD(exer_enc_x, imsar, str_imsar_e);
482 CHECK_METHOD(exer_enc_x, imsastr, str_imsastr_e);
483 CHECK_METHOD(exer_enc_x, imsaustr,str_imsaustr_e);
484 CHECK_METHOD(exer_enc_x, bimsar, str_bimsar_e);
485 CHECK_METHOD(exer_enc_x, eimsar, str_eimsar_e);
486 }
487
488
489 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
490
491 // Encoding nillable multi-level structures (if present, these will contain child elements)
492
493 type record MultiNil {
494 boolean available,
495 record {
496 record {
497 boolean activated
498 } inner optional,
499 charstring str optional
500 } val optional
501 } with {
502 variant "useNil";
503 variant(available) "attribute";
504 variant(val.inner.activated) "attribute";
505 }
506
507 const MultiNil c_absent := { false, omit };
508
509 const universal charstring str_absent :=
510 "<MultiNil xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' available='false' xsi:nil='true'/>\n\n";
511
512 const MultiNil c_present := { true, { { true }, omit } };
513
514 const universal charstring str_present :=
515 "<MultiNil available='true'>\n" &
516 "\t<inner activated='true'/>\n" &
517 "</MultiNil>\n\n";
518
519 const universal charstring str_present2 :=
520 "<MultiNil xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' available='true' xsi:nil='false'>\n" &
521 "\t<inner activated='true'/>\n" &
522 "</MultiNil>\n\n";
523
524 DECLARE_EXER_ENCODERS(MultiNil, multi);
525
526 testcase encode_multi_nil() runs on Nil {
527 CHECK_METHOD(exer_enc_multi, c_absent, str_absent);
528 CHECK_METHOD(exer_enc_multi, c_present, str_present);
529 }
530
531 testcase decode_multi_nil() runs on Nil {
532 CHECK_DECODE(exer_dec_multi, str_absent, MultiNil, c_absent);
533 CHECK_DECODE(exer_dec_multi, str_present, MultiNil, c_present);
534 CHECK_DECODE(exer_dec_multi, str_present2, MultiNil, c_present);
535 }
536
537 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
538
539 control {
540 execute(encode_nil_absent());
541 execute(encode_nil_present());
542 execute(decode_nil_absent());
543 execute(decode_nil_present());
544
545 execute(encode_size_absent());
546 execute(encode_size_present());
547 execute(decode_size_absent());
548 execute(decode_size_present());
549
550 execute(encode_np2_absent());
551 execute(encode_np2_present());
552 execute(decode_np2_absent());
553 execute(decode_np2_present());
554
555 execute(encode_np3_absent());
556 execute(encode_np3_present());
557 execute(decode_np3_absent());
558 execute(decode_np3_present());
559
560 execute(encode_usenil_recof());
561
562 execute(encode_multi_nil());
563 execute(decode_multi_nil());
564 }
565
566 }
567 with {
568 encode "XML";
569 variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'"
570 variant "elementFormQualified";
571 variant "attributeFormQualified";
572 }
This page took 0.044162 seconds and 5 git commands to generate.