File: test_xml_writer.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 (186 lines) | stat: -rw-r--r-- 6,550 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
(* 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__Common

let no_prefixes = fun _ -> None

let expect ?(prefix = no_prefixes) id signals strings =
  let report, iterate, ended = expect_strings id strings in

  signals
  |> Markup__Kstream.of_list
  |> Markup__Xml_writer.write report prefix
  |> iter iterate;

  ended ()

let tests = [
  ("xml.writer.empty" >:: fun _ ->
    expect "empty" [] []);

  ("xml.writer.text" >:: fun _ ->
    expect "text" [`Text ["foo"]] [S "foo"];
    expect "adjacent text" [`Text ["foo"]; `Text ["bar"]] [S "foo"; S "bar"];
    expect "empty text" [`Text [""]] []);

  ("xml.writer.text-escaping" >:: fun _ ->
    expect "text escaping" [`Text ["<foo&bar>"]] [S "&lt;foo&amp;bar&gt;"]);

  ("xml.writer.xml-declaration" >:: fun _ ->
    expect "version only"
      [`Xml {version = "1.0"; encoding = None; standalone = None}]
      [S "<?xml"; S " "; S "version"; S "=\""; S "1.0"; S "\""; S "?>"];

    expect "encoding"
      [`Xml {version = "1.0"; encoding = Some "utf-8"; standalone = None}]
      [S "<?xml"; S " "; S "version"; S "=\""; S "1.0"; S "\""; S " ";
       S "encoding"; S "=\""; S "utf-8"; S "\""; S "?>"];

    expect "standalone: yes"
      [`Xml {version = "1.0"; encoding = None; standalone = Some true}]
      [S "<?xml"; S " "; S "version"; S "=\""; S "1.0"; S "\""; S " ";
       S "standalone"; S "=\""; S "yes"; S "\""; S "?>"];

    expect "standalone: no"
      [`Xml {version = "1.0"; encoding = None; standalone = Some false}]
      [S "<?xml"; S " "; S "version"; S "=\""; S "1.0"; S "\""; S " ";
       S "standalone"; S "=\""; S "no"; S "\""; S "?>"];

    expect "encoding and standalone"
      [`Xml {version = "1.0"; encoding = Some "utf-8"; standalone = Some false}]
      [S "<?xml"; S " "; S "version"; S "=\""; S "1.0"; S "\""; S " ";
       S "encoding"; S "=\""; S "utf-8"; S "\""; S " ";
       S "standalone"; S "=\""; S "no"; S "\""; S "?>"]);

  ("xml.writer.doctype" >:: fun _ ->
    let doctype =
      {doctype_name      = None;
       public_identifier = None;
       system_identifier = None;
       raw_text          = Some "html";
       force_quirks      = false}
    in

    expect "doctype"
      [`Doctype doctype] [S "<!DOCTYPE "; S "html"; S ">"]);

  ("xml.writer.processing-instruction" >:: fun _ ->
    expect "processing instruction"
      [`PI ("foo", "bar")]
      [S "<?"; S "foo"; S " "; S "bar"; S "?>"]);

  ("xml.writer.comment" >:: fun _ ->
    expect "comment" [`Comment "foo"] [S "<!--"; S "foo"; S "-->"]);

  ("xml.writer.element" >:: fun _ ->
    expect "self-closing element"
      [`Start_element (("", "foo"), []); `End_element]
      [S "<"; S "foo"; S "/>"];

    expect "element with text"
      [`Start_element (("", "foo"), []); `Text ["bar"]; `End_element]
      [S "<"; S "foo"; S ">"; S "bar"; S "</"; S "foo"; S ">"];

    expect "nested elements"
      [`Start_element (("", "foo"), []);
       `Start_element (("", "bar"), []);
       `Start_element (("", "baz"), []);
       `End_element;
       `End_element;
       `End_element]
      [S "<"; S "foo"; S ">"; S "<"; S "bar"; S ">"; S "<"; S "baz"; S "/>";
       S "</"; S "bar"; S ">"; S "</"; S "foo"; S ">"]);

    ("xml.writer.attribute" >:: fun _ ->
      expect "attribute"
        [`Start_element (("", "foo"),
          [("", "bar"), "baz"; ("", "blah"), "lulz"]);
         `End_element]
        [S "<"; S "foo"; S " "; S "bar"; S "=\""; S "baz"; S "\""; S " ";
         S "blah"; S "=\""; S "lulz"; S "\""; S "/>"]);

    ("xml.writer.attribute-escaping" >:: fun _ ->
      expect "attribute escaping"
        [`Start_element (("", "foo"), [("", "bar"), "<baz>&'\""]);
         `End_element]
        [S "<"; S "foo"; S " "; S "bar"; S "=\"";
         S "&lt;baz&gt;&amp;&apos;&quot;"; S "\""; S "/>"]);

    ("xml.writer.namespace" >:: fun _ ->
      expect "default namespace"
        [`Start_element (("some_ns", "foo"),
          [(xmlns_ns, "xmlns"), "some_ns";
           ("", "bar"), "baz"]);
         `Start_element (("some_ns", "quux"), []);
         `End_element;
         `End_element]
        [S "<"; S "foo"; S " ";
         S "xmlns"; S "=\""; S "some_ns"; S "\""; S " ";
         S "bar"; S "=\""; S "baz"; S "\""; S ">";
         S "<"; S "quux"; S "/>";
         S "</"; S "foo"; S ">"];

      expect "prefix"
        [`Start_element (("some_ns", "foo"),
          [(xmlns_ns, "a"), "some_ns";
           ("some_ns", "bar"), "baz"]);
         `Start_element (("some_ns", "quux"), []);
         `End_element;
         `End_element]
        [S "<"; S "a:foo"; S " ";
         S "xmlns:a"; S "=\""; S "some_ns"; S "\""; S " ";
         S "a:bar"; S "=\""; S "baz"; S "\""; S ">";
         S "<"; S "a:quux"; S "/>";
         S "</"; S "a:foo"; S ">"];

      expect "shadowing"
        [`Start_element (("", "foo"),
          [(xmlns_ns, "a"), "some_ns"]);
         `Start_element (("some_ns", "bar"),
          [(xmlns_ns, "a"), "other_ns"]);
         `End_element;
         `End_element]
        [S "<"; S "foo"; S " ";
         S "xmlns:a"; S "=\""; S "some_ns"; S "\""; S ">";
         E (`Bad_namespace "some_ns");
         S "<"; S "bar"; S " ";
         S "xmlns:a"; S "=\""; S "other_ns"; S "\""; S "/>";
         S "</"; S "foo"; S ">"];

      expect "shadowing resolution"
        [`Start_element (("", "foo"),
          [(xmlns_ns, "a"), "some_ns";
           (xmlns_ns, "b"), "some_ns"]);
         `Start_element (("some_ns", "bar"),
          [(xmlns_ns, "a"), "other_ns"]);
         `End_element;
         `End_element]
        [S "<"; S "foo"; S " ";
         S "xmlns:a"; S "=\""; S "some_ns"; S "\""; S " ";
         S "xmlns:b"; S "=\""; S "some_ns"; S "\""; S ">";
         S "<"; S "b:bar"; S " ";
         S "xmlns:a"; S "=\""; S "other_ns"; S "\""; S "/>";
         S "</"; S "foo"; S ">"]);

    ("xml.writer.top-level-namespace" >:: fun _ ->
      let prefix = function
        | "some_ns" -> Some "a"
        | _ -> None
      in

      expect ~prefix "top-level namespace"
        [`Start_element (("some_ns", "foo"), []);
         `End_element]
        [S "<"; S "a:foo"; S "/>"];

      expect ~prefix "top-level namespace shadowed"
        [`Start_element (("some_ns", "foo"),
          [(xmlns_ns, "a"), "other_ns"]);
         `End_element]
        [E (`Bad_namespace "some_ns");
         S "<"; S "foo"; S " ";
         S "xmlns:a"; S "=\""; S "other_ns"; S "\""; S "/>"])
]