]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/pxp/netstring/tests/test_mimestring.ml
Initial revision
[helm.git] / helm / DEVEL / pxp / netstring / tests / test_mimestring.ml
1 #require "str";;
2 #directory "..";;
3 #load "netstring.cma";;
4
5 open Mimestring;;
6
7 (**********************************************************************)
8 (* scan_structured_value                                              *)
9 (**********************************************************************)
10
11 let t001() =
12   let r = scan_structured_value "user@domain.com" [ '@'; '.' ] [] in
13   r = [ Atom "user"; Special '@'; Atom "domain"; Special '.'; Atom "com" ]
14 ;;
15
16
17 let t002() =
18   let r = scan_structured_value "user @ domain . com" [ '@'; '.' ]  [] in
19   r = [ Atom "user"; Special '@'; Atom "domain"; Special '.'; Atom "com" ]
20 ;;
21
22
23 let t003() =
24   let r = scan_structured_value "user(Do you know him?)@domain.com" [ '@'; '.' ]
25  []  in
26   r = [ Atom "user"; Special '@'; Atom "domain"; Special '.'; Atom "com" ]
27 ;;
28
29
30 let t004() =
31   let r = scan_structured_value "user @ domain . com" [ '@'; '.'; ' ' ] []  in
32   r = [ Atom "user"; Special ' '; Special '@'; Special ' '; Atom "domain";
33         Special ' '; Special '.'; Special ' '; Atom "com" ]
34 ;;
35
36
37 let t005() =
38   let r = scan_structured_value "user(Do you know him?)@domain.com"
39                                 ['@'; '.'; '(']  [] in
40   r = [ Atom "user"; Special '('; Atom "Do"; Atom "you"; Atom "know";
41         Atom "him?)"; Special '@'; Atom "domain"; Special '.'; Atom "com" ]
42 ;;
43
44
45 let t006() =
46   let r = scan_structured_value "\"My.name\"@domain.com" [ '@'; '.' ]  [] in
47   r = [ QString "My.name"; Special '@'; Atom "domain"; Special '.';
48         Atom "com" ]
49 ;;
50
51
52 let t007() =
53   let r = scan_structured_value "\"\\\"()@. \"@domain.com" [ '@'; '.' ]  [] in
54   r = [ QString "\"()@. "; Special '@'; Atom "domain"; Special '.';
55         Atom "com" ]
56 ;;
57
58
59 let t008() =
60   let r = scan_structured_value "a(b(c(d)e)f)g" [] [] in
61   r = [ Atom "a"; Atom "g" ]
62 ;;
63
64
65 let t009() =
66   let r = scan_structured_value "a(b(c(d)e)f" [] [] in
67   r = [ Atom "a" ]
68 ;;
69
70
71 let t010() =
72   let r = scan_structured_value "a(b\\(c\\(d\\)e)f" [] [] in
73   r = [ Atom "a"; Atom "f" ]
74 ;;
75
76
77 let t011() =
78   let r = scan_structured_value "a(b(c(d)e)f\\" [] [] in
79   r = [ Atom "a" ]
80 ;;
81
82
83 let t012() =
84   let r = scan_structured_value "\"abc" [] [] in
85   r = [ QString "abc" ]
86 ;;
87
88
89 let t013() =
90   let r = scan_structured_value "\"abc\\" [] [] in
91   r = [ QString "abc\\" ]
92 ;;
93
94
95 (* New tests for netstring-0.9: *)
96
97 let t020() =
98   let r = scan_structured_value "user(Do you know him?)@domain.com" 
99             [ '@'; '.' ] [ Return_comments ] in
100   r = [ Atom "user"; Comment; Special '@'; Atom "domain"; Special '.'; 
101         Atom "com" ]
102 ;;
103
104 let t021() =
105   let r = scan_structured_value "user (Do you know him?) @ domain . com"
106             [ '@'; '.'; ' ' ] [] in
107   r = [ Atom "user"; Special ' '; Special ' '; Special ' '; Special '@';
108         Special ' '; Atom "domain";
109         Special ' '; Special '.'; Special ' '; Atom "com" ]
110 ;;
111
112 let t022() =
113   let r = scan_structured_value "user (Do you know him?) @ domain . com"
114             [ '@'; '.'; ' ' ] [ Return_comments ] in
115   r = [ Atom "user"; Special ' '; Comment; Special ' '; Special '@'; 
116         Special ' '; Atom "domain";
117         Special ' '; Special '.'; Special ' '; Atom "com" ]
118 ;;
119
120 let t023() =
121   let r = scan_structured_value "=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=" 
122             [] [] in
123   r = [ Atom "=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=" ]
124 ;;
125
126 let t024() =
127   let r = scan_structured_value "=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=" 
128             [ ] [ Recognize_encoded_words ] in
129   r = [ EncodedWord("ISO-8859-1", "Q", "Keld_J=F8rn_Simonsen") ]
130 ;;
131
132 let t025() =
133   let r = scan_structured_value 
134             "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?="
135             []
136             [ Recognize_encoded_words ] in
137   r = [ EncodedWord
138           ("ISO-8859-1", "B", "SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=");
139         EncodedWord
140           ("ISO-8859-2", "B", "dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==")
141       ]
142 ;;
143
144 (**********************************************************************)
145 (* s_extended_token                                                   *)
146 (**********************************************************************)
147
148 let scan specials options str =
149   let scn = create_mime_scanner specials options str in
150   scan_token_list scn;;
151
152 let t100() =
153   let r = scan [] [] "Two atoms" in
154   match r with
155       [ a1, Atom "Two"; a2, Atom "atoms" ] ->
156
157         (get_pos a1 = 0) &&
158         (get_line a1 = 1) &&
159         (get_column a1 = 0) &&
160         (get_length a1 = 3) &&
161         (separates_adjacent_encoded_words a1 = false) &&
162
163         (get_pos a2 = 4) &&
164         (get_line a2 = 1) &&
165         (get_column a2 = 4) &&
166         (get_length a2 = 5) &&
167         (separates_adjacent_encoded_words a2 = false)
168
169     | _ ->
170         false
171 ;;
172
173
174 let t101() =
175   let r = scan [] [] "  Two  atoms  " in
176   match r with
177       [ a1, Atom "Two"; a2, Atom "atoms" ] ->
178
179         (get_pos a1 = 2) &&
180         (get_line a1 = 1) &&
181         (get_column a1 = 2) &&
182         (get_length a1 = 3) &&
183         (separates_adjacent_encoded_words a1 = false) &&
184
185         (get_pos a2 = 7) &&
186         (get_line a2 = 1) &&
187         (get_column a2 = 7) &&
188         (get_length a2 = 5) &&
189         (separates_adjacent_encoded_words a2 = false)
190
191     | _ ->
192         false
193 ;;
194
195
196 let t102() =
197   let r = scan [] [] "  Two\n atoms  " in
198   match r with
199       [ a1, Atom "Two"; a2, Atom "atoms" ] ->
200
201         (get_pos a1 = 2) &&
202         (get_line a1 = 1) &&
203         (get_column a1 = 2) &&
204         (get_length a1 = 3) &&
205         (separates_adjacent_encoded_words a1 = false) &&
206
207         (get_pos a2 = 7) &&
208         (get_line a2 = 2) &&
209         (get_column a2 = 1) &&
210         (get_length a2 = 5) &&
211         (separates_adjacent_encoded_words a2 = false)
212
213     | _ ->
214         false
215 ;;
216
217 let t110() =
218   let r = scan [] [] "\"Two\" \"qstrings\"" in
219   match r with
220       [ a1, QString "Two"; a2, QString "qstrings" ] ->
221
222         (get_pos a1 = 0) &&
223         (get_line a1 = 1) &&
224         (get_column a1 = 0) &&
225         (get_length a1 = 5) &&
226         (separates_adjacent_encoded_words a1 = false) &&
227
228         (get_pos a2 = 6) &&
229         (get_line a2 = 1) &&
230         (get_column a2 = 6) &&
231         (get_length a2 = 10) &&
232         (separates_adjacent_encoded_words a2 = false)
233
234     | _ ->
235         false
236 ;;
237
238 let t111() =
239   let r = scan [] [] "  \"Two\"  \"qstrings\"  " in
240   match r with
241       [ a1, QString "Two"; a2, QString "qstrings" ] ->
242
243         (get_pos a1 = 2) &&
244         (get_line a1 = 1) &&
245         (get_column a1 = 2) &&
246         (get_length a1 = 5) &&
247         (separates_adjacent_encoded_words a1 = false) &&
248
249         (get_pos a2 = 9) &&
250         (get_line a2 = 1) &&
251         (get_column a2 = 9) &&
252         (get_length a2 = 10) &&
253         (separates_adjacent_encoded_words a2 = false)
254
255     | _ ->
256         false
257 ;;
258
259 let t112() =
260   let r = scan [] [] "  \"Two\nlines\"  \"and\nqstrings\"  " in
261   match r with
262       [ a1, QString "Two\nlines"; a2, QString "and\nqstrings" ] ->
263
264         (get_pos a1 = 2) &&
265         (get_line a1 = 1) &&
266         (get_column a1 = 2) &&
267         (get_length a1 = 11) &&
268         (separates_adjacent_encoded_words a1 = false) &&
269
270         (get_pos a2 = 15) &&
271         (get_line a2 = 2) &&
272         (get_column a2 = 8) &&
273         (get_length a2 = 14) &&
274         (separates_adjacent_encoded_words a2 = false)
275
276     | _ ->
277         false
278 ;;
279
280 let t113() =
281   let r = scan [] [] "  \"Two\\\nlines\"  \"and\\\nqstrings\"  " in
282   match r with
283       [ a1, QString "Two\nlines"; a2, QString "and\nqstrings" ] ->
284
285         (get_pos a1 = 2) &&
286         (get_line a1 = 1) &&
287         (get_column a1 = 2) &&
288         (get_length a1 = 12) &&
289         (separates_adjacent_encoded_words a1 = false) &&
290
291         (get_pos a2 = 16) &&
292         (get_line a2 = 2) &&
293         (get_column a2 = 8) &&
294         (get_length a2 = 15) &&
295         (separates_adjacent_encoded_words a2 = false)
296
297     | _ ->
298         false
299 ;;
300
301 let t120() =
302   (* Domain literals are implemented like quoted strings, so only the
303    * most complicated test case.
304    *)
305   let r = scan [] [] "  [Two\\\nlines]  [and\\\nliterals]  " in
306   match r with
307       [ a1, DomainLiteral "Two\nlines"; a2, DomainLiteral "and\nliterals" ] ->
308
309         (get_pos a1 = 2) &&
310         (get_line a1 = 1) &&
311         (get_column a1 = 2) &&
312         (get_length a1 = 12) &&
313         (separates_adjacent_encoded_words a1 = false) &&
314
315         (get_pos a2 = 16) &&
316         (get_line a2 = 2) &&
317         (get_column a2 = 8) &&
318         (get_length a2 = 15) &&
319         (separates_adjacent_encoded_words a2 = false)
320
321     | _ ->
322         false
323 ;;
324
325 let t130() =
326   let r = scan [] [ Return_comments ] "(Two) (comments)" in
327   match r with
328       [ a1, Comment; a2, Comment ] ->
329
330         (get_pos a1 = 0) &&
331         (get_line a1 = 1) &&
332         (get_column a1 = 0) &&
333         (get_length a1 = 5) &&
334         (separates_adjacent_encoded_words a1 = false) &&
335
336         (get_pos a2 = 6) &&
337         (get_line a2 = 1) &&
338         (get_column a2 = 6) &&
339         (get_length a2 = 10) &&
340         (separates_adjacent_encoded_words a2 = false)
341
342     | _ ->
343         false
344 ;;
345
346 let t131() =
347   let r = scan [] [ Return_comments ] "(Two\nlines) (and\ncomments)" in
348   match r with
349       [ a1, Comment; a2, Comment ] ->
350
351         (get_pos a1 = 0) &&
352         (get_line a1 = 1) &&
353         (get_column a1 = 0) &&
354         (get_length a1 = 11) &&
355         (separates_adjacent_encoded_words a1 = false) &&
356
357         (get_pos a2 = 12) &&
358         (get_line a2 = 2) &&
359         (get_column a2 = 7) &&
360         (get_length a2 = 14) &&
361         (separates_adjacent_encoded_words a2 = false)
362
363     | _ ->
364         false
365 ;;
366
367 let t132() =
368   let r = scan [] [ Return_comments ] "(Two\\\nlines) (and\\\ncomments)" in
369   match r with
370       [ a1, Comment; a2, Comment ] ->
371
372         (get_pos a1 = 0) &&
373         (get_line a1 = 1) &&
374         (get_column a1 = 0) &&
375         (get_length a1 = 12) &&
376         (separates_adjacent_encoded_words a1 = false) &&
377
378         (get_pos a2 = 13) &&
379         (get_line a2 = 2) &&
380         (get_column a2 = 7) &&
381         (get_length a2 = 15) &&
382         (separates_adjacent_encoded_words a2 = false)
383
384     | _ ->
385         false
386 ;;
387
388 let t133() =
389   let r = scan [] [ Return_comments ] "(a\n(b\nc)d\ne(f)) atom" in
390   match r with
391       [ a1, Comment; a2, Atom "atom" ] ->
392
393         (get_pos a1 = 0) &&
394         (get_line a1 = 1) &&
395         (get_column a1 = 0) &&
396         (get_length a1 = 15) &&
397         (separates_adjacent_encoded_words a1 = false) &&
398
399         (get_pos a2 = 16) &&
400         (get_line a2 = 4) &&
401         (get_column a2 = 6) &&
402         (get_length a2 = 4) &&
403         (separates_adjacent_encoded_words a2 = false)
404
405     | _ ->
406         false
407 ;;
408
409 let t140() = 
410   let r = scan [] [] "\031\031" in
411   match r with
412       [ a1, Control '\031'; a2, Control '\031' ] ->
413
414         (get_pos a1 = 0) &&
415         (get_line a1 = 1) &&
416         (get_column a1 = 0) &&
417         (get_length a1 = 1) &&
418         (separates_adjacent_encoded_words a1 = false) &&
419
420         (get_pos a2 = 1) &&
421         (get_line a2 = 1) &&
422         (get_column a2 = 1) &&
423         (get_length a2 = 1) &&
424         (separates_adjacent_encoded_words a2 = false)
425
426     | _ ->
427         false
428 ;;
429
430 let t150() = 
431   let r = scan [ '\t'; '\n' ] [] " \t\n  \n  \t" in
432   match r with
433       [ a1, Special '\t'; _, Special '\n'; _, Special '\n'; a2, Special '\t'] ->
434
435         (get_pos a1 = 1) &&
436         (get_line a1 = 1) &&
437         (get_column a1 = 1) &&
438         (get_length a1 = 1) &&
439         (separates_adjacent_encoded_words a1 = false) &&
440
441         (get_pos a2 = 8) &&
442         (get_line a2 = 3) &&
443         (get_column a2 = 2) &&
444         (get_length a2 = 1) &&
445         (separates_adjacent_encoded_words a2 = false)
446
447     | _ ->
448         false
449 ;;
450
451 let t160() =
452   let r = scan [] [ Recognize_encoded_words ] 
453             "=?iso8859-1?q?G=F6rd?= =?iso8859-1?q?G=F6rd?=" in
454   match r with
455       [ a1, EncodedWord("ISO8859-1", "Q", "G=F6rd"); 
456         a2, EncodedWord("ISO8859-1", "Q", "G=F6rd"); ] ->
457
458         (get_pos a1 = 0) &&
459         (get_line a1 = 1) &&
460         (get_column a1 = 0) &&
461         (get_length a1 = 22) &&
462         (separates_adjacent_encoded_words a1 = false) &&
463         (get_decoded_word a1 = "Görd") &&
464         (get_charset a1 = "ISO8859-1") &&
465
466         (get_pos a2 = 23) &&
467         (get_line a2 = 1) &&
468         (get_column a2 = 23) &&
469         (get_length a2 = 22) &&
470         (separates_adjacent_encoded_words a2 = false) &&
471         (get_decoded_word a2 = "Görd") &&
472         (get_charset a2 = "ISO8859-1")
473
474     | _ ->
475         false
476 ;;
477
478 let t161() =
479   let r = scan [ ' ' ] [ Recognize_encoded_words ] 
480             "=?iso8859-1?q?G=F6rd?= =?iso8859-1?q?G=F6rd?=" in
481   match r with
482       [ a1, EncodedWord("ISO8859-1", "Q", "G=F6rd"); 
483         sp, Special ' ';
484         a2, EncodedWord("ISO8859-1", "Q", "G=F6rd"); ] ->
485
486         (get_pos a1 = 0) &&
487         (get_line a1 = 1) &&
488         (get_column a1 = 0) &&
489         (get_length a1 = 22) &&
490         (separates_adjacent_encoded_words a1 = false) &&
491         (get_decoded_word a1 = "Görd") &&
492         (get_charset a1 = "ISO8859-1") &&
493
494         (get_pos a2 = 23) &&
495         (get_line a2 = 1) &&
496         (get_column a2 = 23) &&
497         (get_length a2 = 22) &&
498         (separates_adjacent_encoded_words a2 = false) &&
499         (get_decoded_word a2 = "Görd") &&
500         (get_charset a2 = "ISO8859-1") &&
501
502         (separates_adjacent_encoded_words sp = true)
503
504     | _ ->
505         false
506 ;;
507
508 let t162() =
509   let r = scan [ ' ' ] [ Recognize_encoded_words ] 
510             "=?iso8859-1?q?G=F6rd?=  =?iso8859-1?q?G=F6rd?=" in
511   match r with
512       [ a1, EncodedWord("ISO8859-1", "Q", "G=F6rd"); 
513         sp1, Special ' ';
514         sp2, Special ' ';
515         a2, EncodedWord("ISO8859-1", "Q", "G=F6rd"); ] ->
516
517         (get_pos a1 = 0) &&
518         (get_line a1 = 1) &&
519         (get_column a1 = 0) &&
520         (get_length a1 = 22) &&
521         (separates_adjacent_encoded_words a1 = false) &&
522         (get_decoded_word a1 = "Görd") &&
523         (get_charset a1 = "ISO8859-1") &&
524
525         (get_pos a2 = 24) &&
526         (get_line a2 = 1) &&
527         (get_column a2 = 24) &&
528         (get_length a2 = 22) &&
529         (separates_adjacent_encoded_words a2 = false) &&
530         (get_decoded_word a2 = "Görd") &&
531         (get_charset a2 = "ISO8859-1") &&
532
533         (separates_adjacent_encoded_words sp1 = true) &&
534         (separates_adjacent_encoded_words sp2 = true)
535
536     | _ ->
537         false
538 ;;
539
540
541
542 (**********************************************************************)
543
544 let test f n =
545   if f() then
546     print_endline ("Test " ^ n ^ " ok")
547   else
548     print_endline ("Test " ^ n ^ " FAILED!!!!");
549   flush stdout
550 ;;
551
552 test t001 "001";;
553 test t002 "002";;
554 test t003 "003";;
555 test t004 "004";;
556 test t005 "005";;
557 test t006 "006";;
558 test t007 "007";;
559 test t008 "008";;
560 test t009 "009";;
561 test t010 "010";;
562 test t011 "011";;
563 test t012 "012";;
564 test t013 "013";;
565
566 test t020 "020";;
567 test t021 "021";;
568 test t022 "022";;
569 test t023 "023";;
570 test t024 "024";;
571 test t025 "025";;
572
573 test t100 "100";;
574 test t101 "101";;
575 test t102 "102";;
576 test t110 "110";;
577 test t111 "111";;
578 test t112 "112";;
579 test t113 "113";;
580 test t120 "120";;
581 test t130 "130";;
582 test t131 "131";;
583 test t132 "132";;
584 test t133 "133";;
585 test t140 "140";;
586 test t150 "150";;
587 test t160 "160";;
588 test t161 "161";;
589 test t162 "162";;