]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/writer/writer-tests.factor
e27c500f3366eeb7a7268279ead7c2a5b6692332
[factor.git] / basis / xml / writer / writer-tests.factor
1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: xml.data xml.writer tools.test fry xml xml.syntax kernel multiline
4 xml.writer.private io.streams.string xml.traversal sequences
5 io.encodings.utf8 io.files io.files.temp accessors io.directories math
6 math.parser ;
7 IN: xml.writer.tests
8
9 ! Add a test for pprint-xml with sensitive-tags
10
11 [ "foo" ] [ T{ name { main "foo" } } name>string ] unit-test
12 [ "foo" ] [ T{ name { space "" } { main "foo" } } name>string ] unit-test
13 [ "ns:foo" ] [ T{ name { space "ns" } { main "foo" } } name>string ] unit-test
14
15 : reprints-as ( to from -- )
16      [ ] [ string>xml xml>string ] bi-curry* unit-test ;
17
18 : pprint-reprints-as ( to from -- )
19      [ ] [ string>xml pprint-xml>string ] bi-curry* unit-test ;
20
21 : reprints-same ( string -- ) dup reprints-as ;
22
23 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><x/>" reprints-same
24
25 """<?xml version="1.0" encoding="UTF-8"?>
26 <!DOCTYPE foo [<!ENTITY foo "bar">]>
27 <x>bar</x>"""
28 """<?xml version="1.0" encoding="UTF-8"?>
29 <!DOCTYPE foo [<!ENTITY foo 'bar'>]>
30 <x>&foo;</x>""" reprints-as
31
32 """<?xml version="1.0" encoding="UTF-8"?>
33 <!DOCTYPE foo [
34   <!ENTITY foo "bar">
35   <!ELEMENT br EMPTY>
36   <!ATTLIST list type    (bullets|ordered|glossary)  "ordered">
37   <!NOTATION foo bar>
38   <?baz bing bang bong?>
39   <!--wtf-->
40 ]>
41 <x>
42   bar
43 </x>"""
44 """<?xml version="1.0" encoding="UTF-8"?>
45 <!DOCTYPE foo [ <!ENTITY foo 'bar'> <!ELEMENT br EMPTY>
46 <!ATTLIST list
47           type    (bullets|ordered|glossary)  "ordered">
48 <!NOTATION      foo bar> <?baz bing bang bong?>
49                 <!--wtf-->
50 ]>
51 <x>&foo;</x>""" pprint-reprints-as
52
53 [ t ] [ "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\" >" dup string>xml-chunk xml>string = ] unit-test
54 [ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a b=\"c\"/>" ]
55     [ "<a b='c'/>" string>xml xml>string ] unit-test
56 [ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar baz</foo>" ]
57 [ "<foo>bar</foo>" string>xml [ " baz" append ] map xml>string ] unit-test
58 [ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<foo>\n  bar\n</foo>" ]
59 [ "<foo>         bar            </foo>" string>xml pprint-xml>string ] unit-test
60 [ "<foo'>" ] [ "<foo'>" <unescaped> xml>string ] unit-test
61
62 : test-file ( -- path )
63     "test.xml" temp-file ;
64
65 [ ] [
66     "<?xml version='1.0' encoding='UTF-16BE'?><x/>" string>xml test-file utf8 [ write-xml ] with-file-writer
67 ] unit-test
68 [ "x" ] [ test-file file>xml body>> name>> main>> ] unit-test
69 [ ] [ test-file delete-file ] unit-test
70
71 [ ] [
72     { 1 2 3 4 } [
73         [ number>string ] [ sq number>string ] bi
74         [XML <tr><td><-></td><td><-></td></tr> XML]
75     ] map [XML <h2>Timings</h2> <table><-></table> XML]
76     pprint-xml
77 ] unit-test
78
79 { "<test name=\"bob\"/>" } [
80     "test" { { "name" "bob" } } { } <tag> xml>string
81 ] unit-test