2009-02-05 22:17:03 -05:00
|
|
|
! Copyright (C) 2005, 2009 Daniel Ehrenberg
|
2009-01-19 23:25:15 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-02-05 22:17:03 -05:00
|
|
|
USING: xml io kernel math sequences strings xml.traversal
|
|
|
|
tools.test math.parser xml.syntax xml.data xml.syntax.private
|
2012-10-24 13:48:22 -04:00
|
|
|
accessors multiline locals inverse xml.writer splitting classes
|
|
|
|
xml.private ;
|
2009-02-05 22:17:03 -05:00
|
|
|
IN: xml.syntax.tests
|
|
|
|
|
|
|
|
! TAGS test
|
|
|
|
|
|
|
|
TAGS: calculate ( tag -- n )
|
|
|
|
|
|
|
|
: calc-2children ( tag -- n n )
|
|
|
|
children-tags first2 [ calculate ] dip calculate ;
|
|
|
|
|
|
|
|
TAG: number calculate
|
|
|
|
children>string string>number ;
|
|
|
|
TAG: add calculate
|
|
|
|
calc-2children + ;
|
|
|
|
TAG: minus calculate
|
|
|
|
calc-2children - ;
|
|
|
|
TAG: times calculate
|
|
|
|
calc-2children * ;
|
|
|
|
TAG: divide calculate
|
|
|
|
calc-2children / ;
|
|
|
|
TAG: neg calculate
|
|
|
|
children-tags first calculate neg ;
|
|
|
|
|
|
|
|
: calc-arith ( string -- n )
|
|
|
|
string>xml first-child-tag calculate ;
|
|
|
|
|
2015-07-03 12:39:59 -04:00
|
|
|
{ 32 } [
|
2009-02-05 22:17:03 -05:00
|
|
|
"<math><times><add><number>1</number><number>3</number></add><neg><number>-8</number></neg></times></math>"
|
|
|
|
calc-arith
|
|
|
|
] unit-test
|
|
|
|
|
|
|
|
XML-NS: foo http://blah.com
|
|
|
|
|
2015-07-03 12:39:59 -04:00
|
|
|
{ T{ name { main "bling" } { url "http://blah.com" } } } [ "bling" foo ] unit-test
|
2009-02-05 22:17:03 -05:00
|
|
|
|
|
|
|
! XML literals
|
2009-01-25 22:06:45 -05:00
|
|
|
|
2015-07-03 12:39:59 -04:00
|
|
|
{ "a" "c" { "a" "c" f } } [
|
2009-01-26 00:52:25 -05:00
|
|
|
"<?xml version='1.0'?><x><-a-><b val=<-c->/><-></x>"
|
2009-01-26 17:11:30 -05:00
|
|
|
string>doc
|
2009-01-25 22:06:45 -05:00
|
|
|
[ second var>> ]
|
2009-01-28 15:33:33 -05:00
|
|
|
[ fourth "val" attr var>> ]
|
2009-01-25 22:06:45 -05:00
|
|
|
[ extract-variables ] tri
|
|
|
|
] unit-test
|
|
|
|
|
2015-07-26 01:59:56 -04:00
|
|
|
{ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
2009-01-25 22:06:45 -05:00
|
|
|
<x>
|
|
|
|
one
|
2015-07-26 01:59:56 -04:00
|
|
|
<b val=\"two\"/>
|
2009-01-25 22:06:45 -05:00
|
|
|
y
|
|
|
|
<foo/>
|
2015-07-26 01:59:56 -04:00
|
|
|
</x>" } [
|
2017-08-26 18:17:24 -04:00
|
|
|
let[ "one" :> a "two" :> c "y" :> x XML-CHUNK[[ <-x-> <foo/> ]] :> d
|
|
|
|
XML-DOC[[
|
2009-01-25 22:06:45 -05:00
|
|
|
<x> <-a-> <b val=<-c->/> <-d-> </x>
|
2017-08-26 18:17:24 -04:00
|
|
|
]] pprint-xml>string
|
2015-07-02 16:37:42 -04:00
|
|
|
]
|
2009-01-25 22:06:45 -05:00
|
|
|
] unit-test
|
2009-01-26 00:52:25 -05:00
|
|
|
|
2015-07-26 01:59:56 -04:00
|
|
|
{ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
2009-01-26 00:52:25 -05:00
|
|
|
<doc>
|
|
|
|
<item>
|
|
|
|
one
|
|
|
|
</item>
|
|
|
|
<item>
|
|
|
|
two
|
|
|
|
</item>
|
|
|
|
<item>
|
|
|
|
three
|
|
|
|
</item>
|
2015-07-26 01:59:56 -04:00
|
|
|
</doc>" } [
|
2009-01-26 00:52:25 -05:00
|
|
|
"one two three" " " split
|
2017-08-26 18:17:24 -04:00
|
|
|
[ XML-CHUNK[[ <item><-></item> ]] ] map
|
|
|
|
XML-DOC[[ <doc><-></doc> ]] pprint-xml>string
|
2009-01-26 00:52:25 -05:00
|
|
|
] unit-test
|
2009-01-26 17:11:30 -05:00
|
|
|
|
2015-07-26 01:59:56 -04:00
|
|
|
{ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
|
|
|
<x number=\"3\" url=\"http://factorcode.org/\" string=\"hello\" word=\"drop\"/>" }
|
2009-02-05 22:17:03 -05:00
|
|
|
[ 3 f "http://factorcode.org/" "hello" \ drop
|
2017-08-26 18:17:24 -04:00
|
|
|
XML-DOC[[ <x number=<-> false=<-> url=<-> string=<-> word=<->/> ]]
|
2009-01-26 17:11:30 -05:00
|
|
|
pprint-xml>string ] unit-test
|
2009-01-27 14:34:14 -05:00
|
|
|
|
2017-08-26 18:17:24 -04:00
|
|
|
{ "<x>3</x>" } [ 3 XML-CHUNK[[ <x><-></x> ]] xml>string ] unit-test
|
|
|
|
{ "<x></x>" } [ f XML-CHUNK[[ <x><-></x> ]] xml>string ] unit-test
|
2009-01-27 15:33:43 -05:00
|
|
|
|
2017-08-26 18:17:24 -04:00
|
|
|
[ XML-CHUNK[[ <-> ]] ] must-infer
|
|
|
|
[ XML-CHUNK[[ <foo><-></foo> <bar val=<->/> ]] ] must-infer
|
2009-01-28 17:17:20 -05:00
|
|
|
|
2017-08-26 18:17:24 -04:00
|
|
|
{ xml-chunk } [ [ XML-CHUNK[[ <foo/> ]] ] first class-of ] unit-test
|
|
|
|
{ xml } [ [ XML-DOC[[ <foo/> ]] ] first class-of ] unit-test
|
|
|
|
{ xml-chunk } [ [ XML-CHUNK[[ <foo val=<->/> ]] ] third class-of ] unit-test
|
|
|
|
{ xml } [ [ XML-DOC[[ <foo val=<->/> ]] ] third class-of ] unit-test
|
|
|
|
{ 1 } [ [ XML-CHUNK[[ <foo/> ]] ] length ] unit-test
|
|
|
|
{ 1 } [ [ XML-DOC[[ <foo/> ]] ] length ] unit-test
|
2009-01-28 17:17:20 -05:00
|
|
|
|
2017-08-26 18:17:24 -04:00
|
|
|
{ "" } [ XML-CHUNK[[ ]] concat ] unit-test
|
2009-02-05 15:21:36 -05:00
|
|
|
|
2017-08-26 18:17:24 -04:00
|
|
|
{ "foo" } [ XML-CHUNK[[ <a>foo</a> ]] [ XML-CHUNK[[ <a><-></a> ]] ] undo ] unit-test
|
|
|
|
{ "foo" } [ XML-CHUNK[[ <a bar='foo'/> ]] [ XML-CHUNK[[ <a bar=<-> /> ]] ] undo ] unit-test
|
|
|
|
{ "foo" "baz" } [ XML-CHUNK[[ <a bar='foo'>baz</a> ]] [ XML-CHUNK[[ <a bar=<->><-></a> ]] ] undo ] unit-test
|
2009-02-05 15:21:36 -05:00
|
|
|
|
|
|
|
: dispatch ( xml -- string )
|
|
|
|
{
|
2017-08-26 18:17:24 -04:00
|
|
|
{ [ XML-CHUNK[[ <a><-></a> ]] ] [ "a" prepend ] }
|
|
|
|
{ [ XML-CHUNK[[ <b><-></b> ]] ] [ "b" prepend ] }
|
|
|
|
{ [ XML-CHUNK[[ <b val='yes'/> ]] ] [ "byes" ] }
|
|
|
|
{ [ XML-CHUNK[[ <b val=<->/> ]] ] [ "bno" prepend ] }
|
2009-02-05 15:21:36 -05:00
|
|
|
} switch ;
|
|
|
|
|
2017-08-26 18:17:24 -04:00
|
|
|
{ "apple" } [ XML-CHUNK[[ <a>pple</a> ]] dispatch ] unit-test
|
|
|
|
{ "banana" } [ XML-CHUNK[[ <b>anana</b> ]] dispatch ] unit-test
|
|
|
|
{ "byes" } [ XML-CHUNK[[ <b val="yes"/> ]] dispatch ] unit-test
|
|
|
|
{ "bnowhere" } [ XML-CHUNK[[ <b val="where"/> ]] dispatch ] unit-test
|
|
|
|
{ "baboon" } [ XML-CHUNK[[ <b val="something">aboon</b> ]] dispatch ] unit-test
|
|
|
|
{ "apple" } [ XML-DOC[[ <a>pple</a> ]] dispatch ] unit-test
|
|
|
|
{ "apple" } [ XML-DOC[[ <a>pple</a> ]] body>> dispatch ] unit-test
|
2009-02-05 15:21:36 -05:00
|
|
|
|
|
|
|
: dispatch-doc ( xml -- string )
|
|
|
|
{
|
2017-08-26 18:17:24 -04:00
|
|
|
{ [ XML-DOC[[ <a><-></a> ]] ] [ "a" prepend ] }
|
|
|
|
{ [ XML-DOC[[ <b><-></b> ]] ] [ "b" prepend ] }
|
|
|
|
{ [ XML-DOC[[ <b val='yes'/> ]] ] [ "byes" ] }
|
|
|
|
{ [ XML-DOC[[ <b val=<->/> ]] ] [ "bno" prepend ] }
|
2009-02-05 15:21:36 -05:00
|
|
|
} switch ;
|
|
|
|
|
2017-08-26 18:17:24 -04:00
|
|
|
{ "apple" } [ XML-DOC[[ <a>pple</a> ]] dispatch-doc ] unit-test
|
|
|
|
{ "apple" } [ XML-CHUNK[[ <a>pple</a> ]] dispatch-doc ] unit-test
|
|
|
|
{ "apple" } [ XML-DOC[[ <a>pple</a> ]] body>> dispatch-doc ] unit-test
|
2012-10-24 13:48:22 -04:00
|
|
|
|
|
|
|
! Make sure nested XML documents interpolate correctly
|
|
|
|
{
|
2015-07-26 01:59:56 -04:00
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><color><blue>it's blue!</blue></color>"
|
2012-10-24 13:48:22 -04:00
|
|
|
} [
|
2017-08-26 18:17:24 -04:00
|
|
|
"it's blue!" XML-DOC[[ <blue><-></blue> ]]
|
|
|
|
XML-DOC[[ <color><-></color> ]] xml>string
|
2012-10-24 13:48:22 -04:00
|
|
|
] unit-test
|
|
|
|
|
|
|
|
{
|
2015-07-26 11:32:40 -04:00
|
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><a>asdf<asdf/>asdf2</a>"
|
2012-10-24 13:48:22 -04:00
|
|
|
} [
|
|
|
|
default-prolog
|
|
|
|
"asdf"
|
|
|
|
"asdf" f f <tag>
|
|
|
|
"asdf2" <xml>
|
2017-08-26 18:17:24 -04:00
|
|
|
XML-DOC[[ <a><-></a> ]] xml>string
|
2012-10-24 13:48:22 -04:00
|
|
|
] unit-test
|