]> gitweb.factorcode.org Git - factor.git/blob - basis/xml/syntax/syntax-tests.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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 "one" :> a "two" :> c "y" :> x [XML <-x-> <foo/> XML] :> d
58         <XML
59             <x> <-a-> <b val=<-c->/> <-d-> </x>
60         XML> pprint-xml>string
61     ]
62 ] unit-test
63
64 [ """<?xml version="1.0" encoding="UTF-8"?>
65 <doc>
66   <item>
67     one
68   </item>
69   <item>
70     two
71   </item>
72   <item>
73     three
74   </item>
75 </doc>""" ] [
76     "one two three" " " split
77     [ [XML <item><-></item> XML] ] map
78     <XML <doc><-></doc> XML> pprint-xml>string
79 ] unit-test
80
81 [ """<?xml version="1.0" encoding="UTF-8"?>
82 <x number="3" url="http://factorcode.org/" string="hello" word="drop"/>""" ]
83 [ 3 f "http://factorcode.org/" "hello" \ drop
84   <XML <x number=<-> false=<-> url=<-> string=<-> word=<->/> XML>
85   pprint-xml>string  ] unit-test
86
87 [ "<x>3</x>" ] [ 3 [XML <x><-></x> XML] xml>string ] unit-test
88 [ "<x></x>" ] [ f [XML <x><-></x> XML] xml>string ] unit-test
89
90 [ [XML <-> XML] ] must-infer
91 [ [XML <foo><-></foo> <bar val=<->/> XML] ] must-infer
92
93 [ xml-chunk ] [ [ [XML <foo/> XML] ] first class ] unit-test
94 [ xml ] [ [ <XML <foo/> XML> ] first class ] unit-test
95 [ xml-chunk ] [ [ [XML <foo val=<->/> XML] ] third class ] unit-test
96 [ xml ] [ [ <XML <foo val=<->/> XML> ] third class ] unit-test
97 [ 1 ] [ [ [XML <foo/> XML] ] length ] unit-test
98 [ 1 ] [ [ <XML <foo/> XML> ] length ] unit-test
99
100 [ "" ] [ [XML XML] concat ] unit-test
101
102 [ "foo" ] [ [XML <a>foo</a> XML] [ [XML <a><-></a> XML] ] undo ] unit-test
103 [ "foo" ] [ [XML <a bar='foo'/> XML] [ [XML <a bar=<-> /> XML] ] undo ] unit-test
104 [ "foo" "baz" ] [ [XML <a bar='foo'>baz</a> XML] [ [XML <a bar=<->><-></a> XML] ] undo ] unit-test
105
106 : dispatch ( xml -- string )
107     {
108         { [ [XML <a><-></a> XML] ] [ "a" prepend ] }
109         { [ [XML <b><-></b> XML] ] [ "b" prepend ] }
110         { [ [XML <b val='yes'/> XML] ] [ "byes" ] }
111         { [ [XML <b val=<->/> XML] ] [ "bno" prepend ] }
112     } switch ;
113
114 [ "apple" ] [ [XML <a>pple</a> XML] dispatch ] unit-test
115 [ "banana" ] [ [XML <b>anana</b> XML] dispatch ] unit-test
116 [ "byes" ] [ [XML <b val="yes"/> XML] dispatch ] unit-test
117 [ "bnowhere" ] [ [XML <b val="where"/> XML] dispatch ] unit-test
118 [ "baboon" ] [ [XML <b val="something">aboon</b> XML] dispatch ] unit-test
119 [ "apple" ] [ <XML <a>pple</a> XML> dispatch ] unit-test
120 [ "apple" ] [ <XML <a>pple</a> XML> body>> dispatch ] unit-test
121
122 : dispatch-doc ( xml -- string )
123     {
124         { [ <XML <a><-></a> XML> ] [ "a" prepend ] }
125         { [ <XML <b><-></b> XML> ] [ "b" prepend ] }
126         { [ <XML <b val='yes'/> XML> ] [ "byes" ] }
127         { [ <XML <b val=<->/> XML> ] [ "bno" prepend ] }
128     } switch ;
129
130 [ "apple" ] [ <XML <a>pple</a> XML> dispatch-doc ] unit-test
131 [ "apple" ] [ [XML <a>pple</a> XML] dispatch-doc ] unit-test
132 [ "apple" ] [ <XML <a>pple</a> XML> body>> dispatch-doc ] unit-test