File: test_detect.ml

package info (click to toggle)
ocaml-markup 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,340 kB
  • sloc: ml: 15,131; makefile: 89
file content (196 lines) | stat: -rw-r--r-- 8,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(* This file is part of Markup.ml, released under the MIT license. See
   LICENSE.md for details, or visit https://github.com/aantron/markup.ml. *)

open OUnit2
open Test_support

open Markup__Kstream
open Markup__Stream_io
open Markup__Detect

let ok = wrong_k "failed"

let _check_rewound chars s =
  if String.length s > 0 then
    next_option chars ok (assert_equal (Some s.[0]))
  else
    next_option chars ok (assert_equal None)

let _check_encoding_guess f s guess =
  let chars = string s in
  f chars ok (assert_equal guess);
  _check_rewound chars s

let tests = [
  ("detect.normalize_name" >:: fun _ ->
    normalize_name true "l2" |> assert_equal "iso-8859-2";
    normalize_name true "utf8" |> assert_equal "utf-8";
    normalize_name true "\t utf-8 " |> assert_equal "utf-8";
    normalize_name true "sjis" |> assert_equal "shift_jis";
    normalize_name true "foobar" |> assert_equal "foobar";
    normalize_name true " foobar " |> assert_equal "foobar");

  ("detect.guess_from_bom_html" >:: fun _ ->
    let check = _check_encoding_guess guess_from_bom_html in

    check "\xfe\xff\x00f\x00o\x00o" (Some "utf-16be");
    check "\xff\xfef\x00o\x00o\x00" (Some "utf-16le");
    check "\xef\xbb\xbffoo" (Some "utf-8");
    check "foo" None;
    check "\xfe\xff" (Some "utf-16be");
    check "\xff\xfe" (Some "utf-16le");
    check "\xef\xbb\xbf" (Some "utf-8");
    check "" None);

  ("detect.guess_from_bom_xml" >:: fun _ ->
    let check = _check_encoding_guess guess_from_bom_xml in

    check "\x00\x00\xfe\xff\x00\x00\x00f" (Some "ucs-4be");
    check "\x00\x00\xfe\xff" (Some "ucs-4be");
    check "\xff\xfe\x00\x00f\x00\x00\x00" (Some "ucs-4le");
    check "\xff\xfe\x00\x00" (Some "ucs-4le");
    check "\x00\x00\xff\xfe\x00\x00f\x00" (Some "ucs-4be-transposed");
    check "\x00\x00\xff\xfe" (Some "ucs-4be-transposed");
    check "\xfe\xff\x00\x00\x00f\x00\x00" (Some "ucs-4le-transposed");
    check "\xfe\xff\x00\x00" (Some "ucs-4le-transposed");
    check "\xfe\xff\x00f\x00o\x00o" (Some "utf-16be");
    check "\xff\xfef\x00o\x00o\x00" (Some "utf-16le");
    check "\xef\xbb\xbffoo" (Some "utf-8");
    check "foo" None;
    check "\xfe\xff" (Some "utf-16be");
    check "\xff\xfe" (Some "utf-16le");
    check "\xef\xbb\xbf" (Some "utf-8");
    check "" None);

  ("detect.guess_family_xml" >:: fun _ ->
    let check = _check_encoding_guess guess_family_xml in

    check "\x00\x00\x00\x3c" (Some "ucs-4be");
    check "\x3c\x00\x00\x00" (Some "ucs-4le");
    check "\x00\x00\x3c\x00" (Some "ucs-4be-transposed");
    check "\x00\x3c\x00\x00" (Some "ucs-4le-transposed");
    check "\x00\x3c\x00\x3f" (Some "utf-16be");
    check "\x3c\x00\x3f\x00" (Some "utf-16le");
    check "\x3c\x3f\x78\x6d" (Some "utf-8");
    check "\x4c\x6f\xa7\x94" (Some "ebcdic");
    check "foo" None;
    check "" None);

  ("detect.meta_tag_prescan" >:: fun _ ->
    let check ?supported ?limit s result =
      let chars = string s in
      meta_tag_prescan ?supported ?limit chars ok (assert_equal result);
      _check_rewound chars s
    in

    check "" None;
    check "foobar" None;
    check "   " None;

    check "<meta charset='shift_jis'>" (Some "shift_jis");
    check "<mEtA cHaRsEt='SHIFT_JIS'>" (Some "shift_jis");
    check "<meta http-equiv='content-type' content='charset=shift_jis'>"
      (Some "shift_jis");
    check "<meta content='charset=shift_jis' http-equiv='content-type'>"
      (Some "shift_jis");
    check "<meta content='charset=shift_jis'>" None;
    check "<meta http-equiv=' content-type' content='charset=shift_jis'>" None;
    check "<meta http-equiv= content-type content='charset=shift_jis'>"
      (Some "shift_jis");
    check "<meta http-equiv='content-type' content='charset = shift_jis'>"
      (Some "shift_jis");
    check "<meta http-equiv='content-type' content='foo charset = shift_jis'>"
      (Some "shift_jis");
    check "<meta http-equiv='content-type' content='cHaRsEt=SHIFT_JIS'>"
      (Some "shift_jis");
    check "<meta http-equiv='content-type' content='charset=shift_jis; foo'>"
      (Some "shift_jis");
    check "<meta http-equiv='content-type' content='charset=shift_jis foo'>"
      (Some "shift_jis");
    check
      ("<meta http-equiv='content-type' content='text/html' " ^
       "charset='iso-8859-15'>")
      (Some "iso-8859-15");
    check "<meta http-equiv='content-type' content='charset=\"\"'>" None;
    check "<meta>" None;
    check "<meta charset=\"shift_jis\">" (Some "shift_jis");
    check "<meta charset='sjis'>" (Some "shift_jis");
    check "<meta charset='x-user-defined'>" (Some "x-user-defined");
    check "<meta charset=' sjis   '>" (Some "shift_jis");
    check "<meta charset =  'shift_jis'>" (Some "shift_jis");
    check "<meta   charset='shift_jis'>" (Some "shift_jis");
    check "<metacharset='shift_jis'>" None;
    check "<meta charset='shift_jis'" (Some "shift_jis");
    check "<meta charset='shift_jis" None;
    check "<meta charset=shift_jis>" (Some "shift_jis");
    check "<meta charset=sHiFt_JiS>" (Some "shift_jis");
    check "<meta author='shift_jis'>" None;
    check "<meta author='shift_jis'><meta charset='shift_jis'>"
      (Some "shift_jis");
    check "<meta author='foo' charset='shift_jis'>" (Some "shift_jis");

    check "<meta charset='utf-8'><meta charset='shift_jis'>" (Some "utf-8");

    check "<!-- foo --><meta charset='shift_jis'>" (Some "shift_jis");
    check "<!-- <meta charset='utf-8'> --><meta charset='shift_jis'>"
      (Some "shift_jis");
    check "<!-- foo --><html><!-- bar --><meta charset='shift_jis'>"
      (Some "shift_jis");
    check "<!--><meta charset='shift_jis'>" (Some "shift_jis");

    check "<! foo ><meta charset='shift_jis'>" (Some "shift_jis");
    check "<!DOCTYPE html><meta charset='shift_jis'>" (Some "shift_jis");
    check "</   ><meta charset='shift_jis'>" (Some "shift_jis");
    check "<?xml ?><meta charset='shift_jis'>" (Some "shift_jis");

    check " <meta charset='shift_jis'>" (Some "shift_jis");
    check " foobar <meta charset='shift_jis'>" (Some "shift_jis");

    check "<a></a><meta charset='shift_jis'>" (Some "shift_jis");
    check "<a ></a><meta charset='shift_jis'>" (Some "shift_jis");
    check "<a href='foo'></a><meta charset='shift_jis'>" (Some "shift_jis");
    check "<a href='foo'></a><meta charset='shift_jis'>" (Some "shift_jis");
    check "<a href=foo><meta charset='shift_jis'>" (Some "shift_jis");
    check "<a   href = 'foo'  ><meta charset='shift_jis'>" (Some "shift_jis");
    check "<a href='foo' alt=hah><meta charset='shift_jis'>" (Some "shift_jis");
    check "<<meta charset='shift_jis'>" (Some "shift_jis");
    check "<a href><meta charset='shift_jis'>" (Some "shift_jis");
    check "<a href  ><meta charset='shift_jis'>" (Some "shift_jis");
    check "</a href><meta charset='shift_jis'>" (Some "shift_jis");

    check "<meta charset='utf-16'>" (Some "utf-8");

    let no_utf_8 =
      fun s k ->
        match s with
        | "utf-8" -> k false
        | _ -> k true
    in

    check ~supported:no_utf_8 "<meta charset='utf-8'>" None;
    check ~supported:no_utf_8 "<meta charset='utf8'>" None;
    check ~supported:no_utf_8 "<meta charset='shift_jis'>" (Some "shift_jis");
    check ~supported:no_utf_8 "<meta charset='utf-8'><meta charset='shift_jis'>"
      (Some "shift_jis");
    check ~supported:no_utf_8 "<meta charset='utf8'><meta charset='shift_jis'>"
      (Some "shift_jis");

    check ~limit:0 "<meta charset='shift_jis'>" None;
    check ~limit:16 "<meta charset='shift_jis'>" None;
    check ~limit:32 "<meta charset='shift_jis'>" (Some "shift_jis"));

  ("detect.read_xml_encoding_declaration" >:: fun _ ->
    let check family s result =
      let chars = string s in
      read_xml_encoding_declaration chars family ok (assert_equal result);
      _check_rewound chars s
    in

    let open Markup__Encoding in

    check utf_8 "<?xml encoding='utf-8'?>" (Some "utf-8");
    check utf_16be "<?xml encoding='utf-8'?>" None;
    check utf_8 "<?xml encoding='us-ascii'?>" (Some "us-ascii");
    check utf_8 "<!-- foo -->   <?xml encoding='utf-8'?>" (Some "utf-8");
    check utf_8 "<!-- foo --> a <?xml encoding='utf-8'?>" None)
]