1 // file      : tests/cxx/parser/validation/built-in/string/driver.cxx
2 // copyright : Copyright (c) 2006-2017 Code Synthesis Tools CC
3 // license   : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 
5 // Test the built-in string & friends types validation.
6 //
7 #include <string>
8 #include <cassert>
9 
10 #include <xsd/cxx/parser/validating/exceptions.hxx>
11 #include <xsd/cxx/parser/validating/xml-schema-pimpl.hxx>
12 
13 using namespace xsd::cxx::parser::validating;
14 
15 template <typename T>
16 bool
test_post_fail(T & p)17 test_post_fail (T& p)
18 {
19   try
20   {
21     p._post_impl (); // List implementation needs this to be post_impl.
22   }
23   catch (invalid_value<char> const&)
24   {
25     return true;
26   }
27 
28   return false;
29 }
30 
31 int
main()32 main ()
33 {
34   typedef xsd::cxx::parser::string_sequence<char> strings;
35 
36   // Good.
37   //
38 
39   // string
40   //
41   {
42     string_pimpl<char> p;
43     p.pre ();
44     p._pre ();
45     p._characters (" \n\t");
46     p._characters (" aaa ");
47     p._characters ("bbb");
48     p._characters (" ");
49     p._post ();
50     assert (p.post_string () == " \n\t aaa bbb ");
51   }
52 
53   // normalized_string
54   //
55   {
56     normalized_string_pimpl<char> p;
57     p.pre ();
58     p._pre ();
59     p._characters (" \n\t");
60     p._characters (" aaa \n\t ");
61     p._characters (" bbb");
62     p._characters ("  ");
63     p._post ();
64     assert (p.post_normalized_string () == "    aaa     bbb  ");
65   }
66 
67   // token
68   //
69   {
70     token_pimpl<char> p;
71     p.pre ();
72     p._pre ();
73     p._characters (" \n\t");
74     p._characters (" aaa \n\t ");
75     p._characters (" bbb \n\t");
76     p._characters ("  ");
77     p._post ();
78     assert (p.post_token () == "aaa bbb");
79   }
80 
81   // name
82   //
83   {
84     name_pimpl<char> p;
85     p.pre ();
86     p._pre ();
87     p._characters (" \n\t");
88     p._characters (" a:b-c_d123 ");
89     p._characters ("  ");
90     p._post ();
91     assert (p.post_name () == "a:b-c_d123");
92   }
93 
94   {
95     name_pimpl<char> p;
96     p.pre ();
97     p._pre ();
98     p._characters (" \n\t");
99     p._characters (" _12 ");
100     p._characters ("  ");
101     p._post ();
102     assert (p.post_name () == "_12");
103   }
104 
105   {
106     name_pimpl<char> p;
107     p.pre ();
108     p._pre ();
109     p._characters (" \n\t");
110     p._characters (" :12 ");
111     p._characters ("  ");
112     p._post ();
113     assert (p.post_name () == ":12");
114   }
115 
116   // nmtoken
117   //
118   {
119     nmtoken_pimpl<char> p;
120     p.pre ();
121     p._pre ();
122     p._characters (" \n\t");
123     p._characters (" 123a:b-c_d123 ");
124     p._characters (" \n\t");
125     p._characters ("  ");
126     p._post ();
127     assert (p.post_nmtoken () == "123a:b-c_d123");
128   }
129 
130   // nmtokens
131   //
132   {
133     strings s;
134     s.push_back ("123");
135     s.push_back ("abc");
136 
137     nmtokens_pimpl<char> p;
138     p.pre ();
139     p._pre ();
140     p._characters (" \n\t");
141     p._characters (" 123 ");
142     p._characters (" \n\t abc ");
143     p._characters ("  ");
144     p._post ();
145     assert (p.post_nmtokens () == s);
146   }
147 
148   // ncname
149   //
150   {
151     ncname_pimpl<char> p;
152     p.pre ();
153     p._pre ();
154     p._characters (" \n\t");
155     p._characters (" a.b-c_d123 ");
156     p._characters ("  ");
157     p._post ();
158     assert (p.post_ncname () == "a.b-c_d123");
159   }
160 
161   // id
162   //
163   {
164     id_pimpl<char> p;
165     p.pre ();
166     p._pre ();
167     p._characters (" \n\t");
168     p._characters (" a.b-c_d123 ");
169     p._characters ("  ");
170     p._post ();
171     assert (p.post_id () == "a.b-c_d123");
172   }
173 
174   // idref
175   //
176   {
177     idref_pimpl<char> p;
178     p.pre ();
179     p._pre ();
180     p._characters (" \n\t");
181     p._characters (" a.b-c_d123 ");
182     p._characters ("  ");
183     p._post ();
184     assert (p.post_idref () == "a.b-c_d123");
185   }
186 
187   // idrefs
188   //
189   {
190     strings s;
191     s.push_back ("a123");
192     s.push_back ("abc");
193 
194     idrefs_pimpl<char> p;
195     p.pre ();
196     p._pre ();
197     p._characters (" \n\t");
198     p._characters (" a123 ");
199     p._characters (" \n\t abc ");
200     p._characters ("  ");
201     p._post ();
202     assert (p.post_idrefs () == s);
203   }
204 
205   // language
206   //
207   {
208     language_pimpl<char> p;
209     p.pre ();
210     p._pre ();
211     p._characters (" x ");
212     p._post ();
213     assert (p.post_language () == "x");
214   }
215 
216   {
217     language_pimpl<char> p;
218     p.pre ();
219     p._pre ();
220     p._characters (" en ");
221     p._post ();
222     assert (p.post_language () == "en");
223   }
224 
225   {
226     language_pimpl<char> p;
227     p.pre ();
228     p._pre ();
229     p._characters (" en");
230     p._characters ("-us ");
231     p._post ();
232     assert (p.post_language () == "en-us");
233   }
234 
235   {
236     language_pimpl<char> p;
237     p.pre ();
238     p._pre ();
239     p._characters ("one-two-three-four44-seven77-eight888");
240     p._post ();
241     assert (p.post_language () == "one-two-three-four44-seven77-eight888");
242   }
243 
244 
245   // Bad
246   //
247 
248   // name
249   //
250   {
251     name_pimpl<char> p;
252     p.pre ();
253     p._pre ();
254     p._characters ("");
255     assert (test_post_fail (p));
256   }
257 
258   {
259     name_pimpl<char> p;
260     p.pre ();
261     p._pre ();
262     p._characters (".a");
263     assert (test_post_fail (p));
264   }
265 
266   {
267     name_pimpl<char> p;
268     p.pre ();
269     p._pre ();
270     p._characters ("-a");
271     assert (test_post_fail (p));
272   }
273 
274   {
275     name_pimpl<char> p;
276     p.pre ();
277     p._pre ();
278     p._characters ("1a");
279     assert (test_post_fail (p));
280   }
281 
282   {
283     name_pimpl<char> p;
284     p.pre ();
285     p._pre ();
286     p._characters ("a,b");
287     assert (test_post_fail (p));
288   }
289 
290   {
291     name_pimpl<char> p;
292     p.pre ();
293     p._pre ();
294     p._characters ("a b");
295     assert (test_post_fail (p));
296   }
297 
298   {
299     name_pimpl<char> p;
300     p.pre ();
301     p._pre ();
302     p._characters ("a<b");
303     assert (test_post_fail (p));
304   }
305 
306   // nmtoken
307   //
308   {
309     nmtoken_pimpl<char> p;
310     p.pre ();
311     p._pre ();
312     p._characters ("");
313     assert (test_post_fail (p));
314   }
315 
316   {
317     nmtoken_pimpl<char> p;
318     p.pre ();
319     p._pre ();
320     p._characters ("a,b");
321     assert (test_post_fail (p));
322   }
323 
324   {
325     nmtoken_pimpl<char> p;
326     p.pre ();
327     p._pre ();
328     p._characters ("a b");
329     assert (test_post_fail (p));
330   }
331 
332   {
333     nmtoken_pimpl<char> p;
334     p.pre ();
335     p._pre ();
336     p._characters ("a<b");
337     assert (test_post_fail (p));
338   }
339 
340   // nmtokens
341   //
342   {
343     nmtokens_pimpl<char> p;
344     p.pre ();
345     p._pre ();
346     p._characters (" ");
347     p._characters (" \t\n  ");
348     assert (test_post_fail (p));
349   }
350 
351   {
352     nmtokens_pimpl<char> p;
353     p.pre ();
354     p._pre ();
355     p._characters ("ab a,b");
356     assert (test_post_fail (p));
357   }
358 
359   // ncname
360   //
361   {
362     ncname_pimpl<char> p;
363     p.pre ();
364     p._pre ();
365     p._characters ("");
366     assert (test_post_fail (p));
367   }
368 
369   {
370     ncname_pimpl<char> p;
371     p.pre ();
372     p._pre ();
373     p._characters (".a");
374     assert (test_post_fail (p));
375   }
376 
377   {
378     ncname_pimpl<char> p;
379     p.pre ();
380     p._pre ();
381     p._characters ("-a");
382     assert (test_post_fail (p));
383   }
384 
385   {
386     ncname_pimpl<char> p;
387     p.pre ();
388     p._pre ();
389     p._characters (":a");
390     assert (test_post_fail (p));
391   }
392 
393   {
394     ncname_pimpl<char> p;
395     p.pre ();
396     p._pre ();
397     p._characters ("1a");
398     assert (test_post_fail (p));
399   }
400 
401   {
402     ncname_pimpl<char> p;
403     p.pre ();
404     p._pre ();
405     p._characters ("1:a");
406     assert (test_post_fail (p));
407   }
408 
409   {
410     ncname_pimpl<char> p;
411     p.pre ();
412     p._pre ();
413     p._characters ("a,b");
414     assert (test_post_fail (p));
415   }
416 
417   {
418     ncname_pimpl<char> p;
419     p.pre ();
420     p._pre ();
421     p._characters ("a b");
422     assert (test_post_fail (p));
423   }
424 
425   {
426     ncname_pimpl<char> p;
427     p.pre ();
428     p._pre ();
429     p._characters ("a<b");
430     assert (test_post_fail (p));
431   }
432 
433   // id
434   //
435   {
436     id_pimpl<char> p;
437     p.pre ();
438     p._pre ();
439     p._characters ("a b");
440     assert (test_post_fail (p));
441   }
442 
443   // idref
444   //
445   {
446     idref_pimpl<char> p;
447     p.pre ();
448     p._pre ();
449     p._characters ("a b");
450     assert (test_post_fail (p));
451   }
452 
453   // idrefs
454   //
455   {
456     idrefs_pimpl<char> p;
457     p.pre ();
458     p._pre ();
459     p._characters ("  ");
460     p._characters (" \t\n ");
461     assert (test_post_fail (p));
462   }
463 
464   {
465     idrefs_pimpl<char> p;
466     p.pre ();
467     p._pre ();
468     p._characters ("ab a<b");
469     assert (test_post_fail (p));
470   }
471 
472   // language
473   //
474   {
475     language_pimpl<char> p;
476     p.pre ();
477     p._pre ();
478     p._characters (" ");
479     assert (test_post_fail (p));
480   }
481 
482   {
483     language_pimpl<char> p;
484     p.pre ();
485     p._pre ();
486     p._characters ("en-");
487     assert (test_post_fail (p));
488   }
489 
490   {
491     language_pimpl<char> p;
492     p.pre ();
493     p._pre ();
494     p._characters ("a1");
495     assert (test_post_fail (p));
496   }
497 
498   {
499     language_pimpl<char> p;
500     p.pre ();
501     p._pre ();
502     p._characters ("en+us");
503     assert (test_post_fail (p));
504   }
505 
506   {
507     language_pimpl<char> p;
508     p.pre ();
509     p._pre ();
510     p._characters ("en-nine99999");
511     assert (test_post_fail (p));
512   }
513 }
514