Sync with 5.4.0
[deliverable/titan.core.git] / regression_test / XML / EXER-whitepaper / UseOrder.ttcnpp
CommitLineData
970ed795 1/******************************************************************************
3abe9331 2 * Copyright (c) 2000-2015 Ericsson Telecom AB
970ed795
EL
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 ******************************************************************************/
8module UseOrder {
9modulepar boolean UseOrder_verbose := false;
10#define verbose UseOrder_verbose
11#include "../macros.ttcnin"
12
13type component UO {}
14
15// "Plain" USE-ORDER
16type record UOProduct {
17 record of enumerated { id, name, price, color } order,
18
19 integer id,
20 charstring name,
21 float price,
22 charstring color
23}
24with {
25 variant "element";
26 variant "useOrder";
27 variant "namespace as 'http://www.example.com' prefix 'exm'";
28}
29
30
31DECLARE_XER_ENCODERS(UOProduct, uo);
32DECLARE_EXER_ENCODERS(UOProduct, uo);
33
34const UOProduct uopval := {
35 order := { id, color, price, name },
36
37 id := 100,
38 name := "shirt",
39 price := 12.23,
40 color := "red"
41}
42
43const universal charstring str_uo_e :=
44"<exm:UOProduct xmlns:exm=\'http://www.example.com\'>\n" &
45"\t<id>100</id>\n" &
46"\t<color>red</color>\n" &
47"\t<price>12.230000</price>\n" &
48"\t<name>shirt</name>\n" &
49"</exm:UOProduct>\n" &
50"\n";
51
52const universal charstring str_uo_b :=
53"<UOProduct>\n" &
54"\t<order>\n" &
55"\t\t<id/><color/><price/><name/>\n" &
56"\t</order>\n" &
57
58"\t<id>100</id>\n" &
59"\t<name>shirt</name>\n" &
60"\t<price>12.230000</price>\n" &
61"\t<color>red</color>\n" &
62"</UOProduct>\n" &
63"\n";
64
65testcase encode_uo() runs on UO
66{
67 CHECK_METHOD(bxer_enc_uo, uopval, str_uo_b);
68 CHECK_METHOD(exer_enc_uo, uopval, str_uo_e);
69}
70
71testcase decode_uo() runs on UO
72{
73 CHECK_DECODE(bxer_dec_uo, str_uo_b, UOProduct, uopval);
74 CHECK_DECODE(exer_dec_uo, str_uo_e, UOProduct, uopval);
75}
76
77
78external function exer_enc_uo_w(in UOProduct par) return octetstring
79with { extension "prototype(convert) encode (XER:XER_EXTENDED) errorbehavior(ALL:WARNING)" }
80
81external function exer_enc_uo2(in UOProduct par, out octetstring o)
82with { extension "prototype(fast) encode (XER:XER_EXTENDED) errorbehavior(ALL:WARNING)" }
83
84external function exer_dec_uo2(in octetstring par, out UOProduct o) return integer
85with { extension "prototype(backtrack) decode (XER:XER_EXTENDED) errorbehavior(ALL:WARNING)" };
86
87testcase encdec_uo() runs on UO
88{
89 // This is what the encoder returns on error (no fields encoded).
90 const universal charstring panic := "<exm:UOProduct xmlns:exm='http://www.example.com'/>\n\n";
91
92 const UOProduct c_uo := {
93 /* There is no way to test incorrect values like { id, id, id, id } or
94 { id, name }, without a dynamic testcase error */
95 order := { id, name, color, price },
96 id := 100,
97 name := "shirt",
98 price := 12.23,
99 color := "red"
100 }
101 var integer rez;
102 var octetstring encoded;
103 exer_enc_uo2(c_uo, encoded);
104
105 {
106 var UOProduct v_uo;
107 rez := exer_dec_uo2(encoded, v_uo);
108 COMPARE(v_uo, c_uo);
109 }
110
111 { // missing field
112 const UOProduct unidentified :=
113 {
114 order := { name, color, price },
115 id := 100,
116 name := "shirt",
117 price := 12.23,
118 color := "red"
119 };
120 CHECK_METHOD(exer_enc_uo_w, unidentified, panic);
121 }
122
123 { // the right number, but some (!) duplication
124 const UOProduct deja_vu :=
125 {
126 order := { name, name, name, name },
127 id := 100,
128 name := "shirt",
129 price := 12.23,
130 color := "red"
131 };
132 CHECK_METHOD(exer_enc_uo_w, deja_vu, panic);
133 }
134
135 // We can test errors during decoding with prototype(backtrack)
136 {
137 const universal charstring priceless := "<exm:UOProduct xmlns:exm='http://www.example.com'>\n\t<id>100</id>\n\t<name>shirt</name>\n\t<color>red</color>\n</exm:UOProduct>\n\n";
138 // Note no price
139 rez := u2o("UTF-8", priceless, encoded);
140 if (rez!=0) { setverdict(fail, "Conversion failed: ", rez) }
141
142 var UOProduct v_uo;
143 rez := exer_dec_uo2(encoded, v_uo);
144 COMPARE(rez, 1); // 1 means error
145 }
146
147 {
148 const universal charstring id2 := "<exm:UOProduct xmlns:exm='http://www.example.com'>\n\t<id>100</id>\n\t<id>234</id>\n\t<name>shirt</name>\n\t<color>red</color>\n</exm:UOProduct>\n\n";
149 // Still no price but 4 child elements
150 rez := u2o("UTF-8", id2, encoded);
151 if (rez!=0) { setverdict(fail, "Conversion failed: ", rez) }
152
153 var UOProduct v_uo;
154 rez := exer_dec_uo2(encoded, v_uo);
155 COMPARE(rez, 1); // 1 means error
156 }
157
158 {
159 const universal charstring toomuch := "<exm:UOProduct xmlns:exm='http://www.example.com'>\n\t<price>12.230000</price>\n\t<id>100</id>\n\t<id>234</id>\n\t<name>shirt</name>\n\t<color>red</color>\n</exm:UOProduct>\n\n";
160 // too many child elements
161 rez := u2o("UTF-8", toomuch, encoded);
162 if (rez!=0) { setverdict(fail, "Conversion failed: ", rez) }
163
164 var UOProduct v_uo;
165 rez := exer_dec_uo2(encoded, v_uo);
166 COMPARE(rez, 1); // 1 means error
167 }
168}
169
170/* *** *** * *** * * *** * *** *** *** * *** *** * * *** *
171*** *** *** * *** * * * * *** *** *** *** *** * *** * * * *
172* * *** * *** *** * *** * *** * *** *** *** *** *** * *** *** *
173* *** * * * *** * * *** * * *** * *** *** *** * * * * * * *** *
174* * *** * * * *** * * *** * ***/
175
176// USE-ORDER with optional fields (regression test for HL22039)
177type record Team {
178 record of enumerated { goalie, defender, midfield, forward } order,
179 universal charstring goalie optional,
180 universal charstring defender optional,
181 universal charstring midfield optional,
182 universal charstring forward optional
183}
184with {
185 variant "useOrder"
186}
187
188DECLARE_XER_ENCODERS(Team, tm);
189DECLARE_EXER_ENCODERS(Team, tm);
190
191const Team arany := {
192 { goalie, defender, midfield, forward },
193 "Grosics",
194 "Buzanski Lorant Lantos",
195 "Bozsik Zakarias",
196 "Czibor Kocsis Hidegkuti Puskas Budai"
197}
198
199const Team ezust := {
200 { defender, midfield, forward },
201 omit,
202 "Buzanski Lorant Lantos",
203 "Bozsik Zakarias",
204 "Czibor Kocsis Hidegkuti Puskas Budai"
205}
206
207const Team attack := {
208 order := { forward, goalie },
209 goalie := "Gilmar",
210 defender := omit,
211 midfield := omit,
212 forward := "Garrincha Didi Vav" & char(0,0,0,225) & " Pel" & char(0,0,0,233) & " Zagalo"
213}
214
215const Team senki := {
216 {},
217 omit, omit, omit, omit
218}
219
220const universal charstring str_arany_e :=
221"<Team>\n" &
222"\t<goalie>Grosics</goalie>\n" &
223"\t<defender>Buzanski Lorant Lantos</defender>\n" &
224"\t<midfield>Bozsik Zakarias</midfield>\n" &
225"\t<forward>Czibor Kocsis Hidegkuti Puskas Budai</forward>\n" &
226"</Team>\n\n";
227
228const universal charstring str_arany_b :=
229"<Team>\n" &
230"\t<order>\n" &
231"\t\t<goalie/><defender/><midfield/><forward/>\n" &
232"\t</order>\n" &
233"\t<goalie>Grosics</goalie>\n" &
234"\t<defender>Buzanski Lorant Lantos</defender>\n" &
235"\t<midfield>Bozsik Zakarias</midfield>\n" &
236"\t<forward>Czibor Kocsis Hidegkuti Puskas Budai</forward>\n" &
237"</Team>\n\n";
238
239const universal charstring str_ezust_e :=
240"<Team>\n" &
241"\t<defender>Buzanski Lorant Lantos</defender>\n" &
242"\t<midfield>Bozsik Zakarias</midfield>\n" &
243"\t<forward>Czibor Kocsis Hidegkuti Puskas Budai</forward>\n" &
244"</Team>\n\n";
245
246const universal charstring str_ezust_b :=
247"<Team>\n" &
248"\t<order>\n" &
249"\t\t<defender/><midfield/><forward/>\n" &
250"\t</order>\n" &
251"\t<defender>Buzanski Lorant Lantos</defender>\n" &
252"\t<midfield>Bozsik Zakarias</midfield>\n" &
253"\t<forward>Czibor Kocsis Hidegkuti Puskas Budai</forward>\n" &
254"</Team>\n\n";
255
256const universal charstring str_attack_e :=
257"<Team>\n" &