]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/syntax/syntax-tests.factor
Merge branch 'master' into directory-searching
[factor.git] / basis / xml / syntax / syntax-tests.factor
1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: xml io kernel math sequences strings xml.traversal
4 tools.test math.parser xml.syntax xml.data xml.syntax.private
5 accessors multiline locals inverse xml.writer splitting classes ;
6 IN: xml.syntax.tests
7
8 ! TAGS test
9
10 TAGS: calculate ( tag -- n )
11
12 : calc-2children ( tag -- n n )
13     children-tags first2 [ calculate ] dip calculate ;
14
15 TAG: number calculate
16     children>string string>number ;
17 TAG: add calculate
18     calc-2children + ;
19 TAG: minus calculate
20     calc-2children - ;
21 TAG: times calculate
22     calc-2children * ;
23 TAG: divide calculate
24     calc-2children / ;
25 TAG: neg calculate
26     children-tags first calculate neg ;
27
28 : calc-arith ( string -- n )
29     string>xml first-child-tag calculate ;
30
31 [ 32 ] [
32     "<math><times><add><number>1</number><number>3</number></add><neg><number>-8</number></neg></times></math>"
33     calc-arith
34 ] unit-test
35
36 XML-NS: foo http://blah.com
37
38 [ T{ name { main "bling" } { url "http://blah.com" } } ] [ "bling" foo ] unit-test
39
40 ! XML literals
41
42 [ "a" "c" { "a" "c" f } ] [
43     "<?xml version='1.0'?><x><-a-><b val=<-c->/><-></x>"
44     string>doc
45     [ second var>> ]
46     [ fourth "val" attr var>> ]
47     [ extract-variables ] tri
48 ] unit-test
49
50 [ {" <?xml version="1.0" encoding="UTF-8"?>
51 <x>
52   one
53   <b val="two"/>
54   y
55   <foo/>
56 </x>"} ] [
57     [let* | a [ "one" ] c [ "two" ] x [ "y" ]
58            d [ [XML <-x-> <foo/> XML] ] |
59         <XML
60             <x> <-a-> <b val=<-c->/> <-d-> </x>
61         XML> pprint-xml>string
62     ]
63 ] unit-test
64
65 [ {" <?xml version="1.0" encoding="UTF-8"?>
66 <doc>
67   <item>
68     one
69   </item>
70   <item>
71     two
72   </item>
73   <item>
74     three
75   </item>
76 </doc>"} ] [
77     "one two three" " " split
78     [ [XML <item><-></item> XML] ] map
79     <XML <doc><-></doc> XML> pprint-xml>string
80 ] unit-test
81
82 [ {" <?xml version="1.0" encoding="UTF-8"?>
83 <x number="3" url="http://factorcode.org/" string="hello" word="drop"/>"} ]
84 [ 3 f "http://factorcode.org/" "hello" \ drop
85   <XML <x number=<-> false=<-> url=<-> string=<-> word=<->/> XML>
86   pprint-xml>string  ] unit-test
87
88 [ "<x>3</x>" ] [ 3 [XML <x><-></x> XML] xml>string ] unit-test
89 [ "<x></x>" ] [ f [XML <x><-></x> XML] xml>string ] unit-test
90
91 [ [XML <-> XML] ] must-infer
92 [ [XML <foo><-></foo> <bar val=<->/> XML] ] must-infer
93
94 [ xml-chunk ] [ [ [XML <foo/> XML] ] first class ] unit-test
95 [ xml ] [ [ <XML <foo/> XML> ] first class ] unit-test
96 [ xml-chunk ] [ [ [XML <foo val=<->/> XML] ] third class ] unit-test
97 [ xml ] [ [ <XML <foo val=<->/> XML> ] third class ] unit-test
98 [ 1 ] [ [ [XML <foo/> XML] ] length ] unit-test
99 [ 1 ] [ [ <XML <foo/> XML> ] length ] unit-test
100
101 [ "" ] [ [XML XML] concat ] unit-test
102
103 USE: inverse
104
105 [ "foo" ] [ [XML <a>foo</a> XML] [ [XML <a><-></a> XML] ] undo ] unit-test
106 [ "foo" ] [ [XML <a bar='foo'/> XML] [ [XML <a bar=<-> /> XML] ] undo ] unit-test
107 [ "foo" "baz" ] [ [XML <a bar='foo'>baz</a> XML] [ [XML <a bar=<->><-></a> XML] ] undo ] unit-test
108
109 : dispatch ( xml -- string )
110     {
111         { [ [XML <a><-></a> XML] ] [ "a" prepend ] }
112         { [ [XML <b><-></b> XML] ] [ "b" prepend ] }
113         { [ [XML <b val='yes'/> XML] ] [ "byes" ] }
114         { [ [XML <b val=<->/> XML] ] [ "bno" prepend ] }
115     } switch ;
116
117 [ "apple" ] [ [XML <a>pple</a> XML] dispatch ] unit-test
118 [ "banana" ] [ [XML <b>anana</b> XML] dispatch ] unit-test
119 [ "byes" ] [ [XML <b val="yes"/> XML] dispatch ] unit-test
120 [ "bnowhere" ] [ [XML <b val="where"/> XML] dispatch ] unit-test
121 [ "baboon" ] [ [XML <b val="something">aboon</b> XML] dispatch ] unit-test
122 [ "apple" ] [ <XML <a>pple</a> XML> dispatch ] unit-test
123 [ "apple" ] [ <XML <a>pple</a> XML> body>> dispatch ] unit-test
124
125 : dispatch-doc ( xml -- string )
126     {
127         { [ <XML <a><-></a> XML> ] [ "a" prepend ] }
128         { [ <XML <b><-></b> XML> ] [ "b" prepend ] }
129         { [ <XML <b val='yes'/> XML> ] [ "byes" ] }
130         { [ <XML <b val=<->/> XML> ] [ "bno" prepend ] }
131     } switch ;
132
133 [ "apple" ] [ <XML <a>pple</a> XML> dispatch-doc ] unit-test
134 [ "apple" ] [ [XML <a>pple</a> XML] dispatch-doc ] unit-test
135 [ "apple" ] [ <XML <a>pple</a> XML> body>> dispatch-doc ] unit-test