| 
									
										
										
										
											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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ 32 ] [ | 
					
						
							|  |  |  |     "<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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ T{ name { main "bling" } { url "http://blah.com" } } ] [ "bling" foo ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! XML literals | 
					
						
							| 
									
										
										
										
											2009-01-25 22:06:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-26 00:52:25 -05:00
										 |  |  | [ "a" "c" { "a" "c" f } ] [ | 
					
						
							|  |  |  |     "<?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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-20 23:42:40 -04:00
										 |  |  | [ """<?xml version="1.0" encoding="UTF-8"?> | 
					
						
							| 
									
										
										
										
											2009-01-25 22:06:45 -05:00
										 |  |  | <x> | 
					
						
							|  |  |  |   one | 
					
						
							|  |  |  |   <b val="two"/> | 
					
						
							|  |  |  |   y | 
					
						
							|  |  |  |   <foo/> | 
					
						
							| 
									
										
										
										
											2009-09-20 23:42:40 -04:00
										 |  |  | </x>""" ] [ | 
					
						
							| 
									
										
										
										
											2009-10-27 22:50:31 -04:00
										 |  |  |     [let "one" :> a "two" :> c "y" :> x [XML <-x-> <foo/> XML] :> d | 
					
						
							| 
									
										
										
										
											2009-01-25 22:06:45 -05:00
										 |  |  |         <XML | 
					
						
							|  |  |  |             <x> <-a-> <b val=<-c->/> <-d-> </x> | 
					
						
							|  |  |  |         XML> pprint-xml>string | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | ] unit-test | 
					
						
							| 
									
										
										
										
											2009-01-26 00:52:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-20 23:42:40 -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> | 
					
						
							| 
									
										
										
										
											2009-09-20 23:42:40 -04:00
										 |  |  | </doc>""" ] [ | 
					
						
							| 
									
										
										
										
											2009-01-26 00:52:25 -05:00
										 |  |  |     "one two three" " " split | 
					
						
							|  |  |  |     [ [XML <item><-></item> XML] ] map
 | 
					
						
							|  |  |  |     <XML <doc><-></doc> XML> pprint-xml>string | 
					
						
							|  |  |  | ] unit-test | 
					
						
							| 
									
										
										
										
											2009-01-26 17:11:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-20 23:42:40 -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 | 
					
						
							| 
									
										
										
										
											2009-01-26 17:11:30 -05:00
										 |  |  |   <XML <x number=<-> false=<-> url=<-> string=<-> word=<->/> XML> | 
					
						
							|  |  |  |   pprint-xml>string  ] unit-test | 
					
						
							| 
									
										
										
										
											2009-01-27 14:34:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-29 14:33:04 -05:00
										 |  |  | [ "<x>3</x>" ] [ 3 [XML <x><-></x> XML] xml>string ] unit-test | 
					
						
							|  |  |  | [ "<x></x>" ] [ f [XML <x><-></x> XML] xml>string ] unit-test | 
					
						
							| 
									
										
										
										
											2009-01-27 15:33:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-04 18:29:35 -05:00
										 |  |  | [ [XML <-> XML] ] must-infer | 
					
						
							| 
									
										
										
										
											2009-01-27 16:10:56 -05:00
										 |  |  | [ [XML <foo><-></foo> <bar val=<->/> XML] ] must-infer | 
					
						
							| 
									
										
										
										
											2009-01-28 17:17:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-24 22:33:09 -04:00
										 |  |  | [ xml-chunk ] [ [ [XML <foo/> XML] ] first class-of ] unit-test | 
					
						
							|  |  |  | [ xml ] [ [ <XML <foo/> XML> ] first class-of ] unit-test | 
					
						
							|  |  |  | [ xml-chunk ] [ [ [XML <foo val=<->/> XML] ] third class-of ] unit-test | 
					
						
							|  |  |  | [ xml ] [ [ <XML <foo val=<->/> XML> ] third class-of ] unit-test | 
					
						
							| 
									
										
										
										
											2009-01-28 17:17:20 -05:00
										 |  |  | [ 1 ] [ [ [XML <foo/> XML] ] length ] unit-test | 
					
						
							|  |  |  | [ 1 ] [ [ <XML <foo/> XML> ] length ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ "" ] [ [XML XML] concat ] unit-test | 
					
						
							| 
									
										
										
										
											2009-02-05 15:21:36 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | [ "foo" ] [ [XML <a>foo</a> XML] [ [XML <a><-></a> XML] ] undo ] unit-test | 
					
						
							|  |  |  | [ "foo" ] [ [XML <a bar='foo'/> XML] [ [XML <a bar=<-> /> XML] ] undo ] unit-test | 
					
						
							|  |  |  | [ "foo" "baz" ] [ [XML <a bar='foo'>baz</a> XML] [ [XML <a bar=<->><-></a> XML] ] undo ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : dispatch ( xml -- string )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { [ [XML <a><-></a> XML] ] [ "a" prepend ] } | 
					
						
							|  |  |  |         { [ [XML <b><-></b> XML] ] [ "b" prepend ] } | 
					
						
							|  |  |  |         { [ [XML <b val='yes'/> XML] ] [ "byes" ] } | 
					
						
							|  |  |  |         { [ [XML <b val=<->/> XML] ] [ "bno" prepend ] } | 
					
						
							|  |  |  |     } switch ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ "apple" ] [ [XML <a>pple</a> XML] dispatch ] unit-test | 
					
						
							|  |  |  | [ "banana" ] [ [XML <b>anana</b> XML] dispatch ] unit-test | 
					
						
							|  |  |  | [ "byes" ] [ [XML <b val="yes"/> XML] dispatch ] unit-test | 
					
						
							|  |  |  | [ "bnowhere" ] [ [XML <b val="where"/> XML] dispatch ] unit-test | 
					
						
							|  |  |  | [ "baboon" ] [ [XML <b val="something">aboon</b> XML] dispatch ] unit-test | 
					
						
							|  |  |  | [ "apple" ] [ <XML <a>pple</a> XML> dispatch ] unit-test | 
					
						
							|  |  |  | [ "apple" ] [ <XML <a>pple</a> XML> body>> dispatch ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : dispatch-doc ( xml -- string )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { [ <XML <a><-></a> XML> ] [ "a" prepend ] } | 
					
						
							|  |  |  |         { [ <XML <b><-></b> XML> ] [ "b" prepend ] } | 
					
						
							|  |  |  |         { [ <XML <b val='yes'/> XML> ] [ "byes" ] } | 
					
						
							|  |  |  |         { [ <XML <b val=<->/> XML> ] [ "bno" prepend ] } | 
					
						
							|  |  |  |     } switch ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ "apple" ] [ <XML <a>pple</a> XML> dispatch-doc ] unit-test | 
					
						
							|  |  |  | [ "apple" ] [ [XML <a>pple</a> XML] dispatch-doc ] unit-test | 
					
						
							|  |  |  | [ "apple" ] [ <XML <a>pple</a> XML> body>> dispatch-doc ] unit-test | 
					
						
							| 
									
										
										
										
											2012-10-24 13:48:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Make sure nested XML documents interpolate correctly | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     """<?xml version="1.0" encoding="UTF-8"?><color><blue>it's blue!</blue></color>""" | 
					
						
							|  |  |  | } [ | 
					
						
							|  |  |  |     "it's blue!" <XML <blue><-></blue> XML> | 
					
						
							|  |  |  |     <XML <color><-></color> XML> xml>string | 
					
						
							|  |  |  | ] unit-test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     """<?xml version="1.0" encoding="UTF-8"?><a>asdf<asdf/>asdf2</a>""" | 
					
						
							|  |  |  | } [ | 
					
						
							|  |  |  |     default-prolog | 
					
						
							|  |  |  |     "asdf" | 
					
						
							|  |  |  |     "asdf" f f <tag> | 
					
						
							|  |  |  |     "asdf2" <xml> | 
					
						
							|  |  |  |     <XML <a><-></a> XML> xml>string | 
					
						
							|  |  |  | ] unit-test |