Skip to content

Commit eaf3862

Browse files
committed
DXML-27 test serialization of built-in data types
as per https://www.w3.org/TR/xmlschema-2/#built-in-datatypes
1 parent 8dab1ab commit eaf3862

1 file changed

Lines changed: 50 additions & 14 deletions

File tree

src/test/clojure/clojure/data/xml/test_emit.clj

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
(:require
1313
[clojure.test :refer :all]
1414
[clojure.data.xml :refer :all]
15-
[clojure.data.xml.test-utils :refer [test-stream lazy-parse*]]))
15+
[clojure.data.xml.test-utils :refer [test-stream lazy-parse*]])
16+
(:import (javax.xml.namespace QName)))
1617

1718
(def deep-tree
1819
(lazy-parse* (str "<a h=\"1\" i='2' j=\"3\">"
@@ -153,19 +154,54 @@
153154
result (indent-str nested-xml :doctype doctype)]
154155
(is (= expect (subs result (.indexOf result doctype))))))
155156

156-
(deftest test-boolean
157-
(is (= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>true</foo>"
158-
(emit-str (element :foo {} true)))))
159-
160-
(deftest test-number
161-
(is (= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>1</foo>"
162-
(emit-str (element :foo {} 1))))
163-
(is (= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>1.2</foo>"
164-
(emit-str (element :foo {} 1.2))))
165-
(is (= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>0</foo>"
166-
(emit-str (element :foo {} (int 0)))))
167-
(is (= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>1.2</foo>"
168-
(emit-str (element :foo {} (float 1.2))))))
157+
(defmacro are-serializable [group-description extra-attrs & {:as data-strings}]
158+
`(testing ~group-description
159+
(testing "in content"
160+
~@(for [[data string] data-strings]
161+
`(is (= (parse-str (emit-str (element :e ~extra-attrs ~string)))
162+
(parse-str (emit-str (element :e ~extra-attrs ~data)))))))
163+
(testing "in attrs"
164+
~@(for [[data string] data-strings]
165+
`(is (= (emit-str (element :e ~(assoc extra-attrs :a string)))
166+
(emit-str (element :e ~(assoc extra-attrs :a data)))))))))
167+
168+
(deftest test-datatypes
169+
;; https://www.w3.org/TR/xmlschema-2/#built-in-datatypes
170+
(testing "serializing"
171+
(are-serializable
172+
"booleans" {}
173+
true "true"
174+
false "false")
175+
(are-serializable
176+
"numbers" {}
177+
1 "1"
178+
1.2 "1.2"
179+
3/4 "0.75"
180+
(int 0) "0"
181+
(float 1.4) "1.4"
182+
1.25M "1.25"
183+
(BigInteger. "42424242424242424242424242424242") "42424242424242424242424242424242"
184+
42424242424242424242424242424242 "42424242424242424242424242424242")
185+
(are-serializable
186+
"byte-arrays" {}
187+
(byte-array [0 1 2 3 4]) "AAECAwQ=")
188+
(are-serializable
189+
"uris" {}
190+
(java.net.URI. "S:l") "S:l"
191+
(java.net.URL. "http://foo") "http://foo")
192+
(are-serializable
193+
"dates" {}
194+
(java.util.Date. 0) "1970-01-01T00:00:00.000-00:00"
195+
(java.time.Instant/ofEpochMilli 0) "1970-01-01T00:00:00.000-00:00")
196+
(are-serializable
197+
"qnames" {:xmlns/p "U:"}
198+
:xmlns.U%3A/qn "p:qn"
199+
(QName. "U:" "qn") "p:qn")
200+
(testing "qnames generated"
201+
(is (thrown? Exception (emit-str (element :e {} :xmlns.U%3A/qn))))
202+
(is (thrown? Exception (emit-str (element :e {:a :xmlns.U%3A/qn}))))
203+
(is (thrown? Exception (emit-str (element :e {} (QName. "U:" "qn")))))
204+
(is (thrown? Exception (emit-str (element :e {:a (QName. "U:" "qn")})))))))
169205

170206
(deftest test-event-seq-emit
171207
(is (= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a>123</a>"

0 commit comments

Comments
 (0)