scxml 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. data/README +0 -0
  2. data/lib/scxml.rb +86 -0
  3. data/lib/scxml/document.rb +165 -0
  4. data/lib/scxml/element.rb +219 -0
  5. data/lib/scxml/xpath.rb +142 -0
  6. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate01.out +2 -0
  7. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate02.out +2 -0
  8. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate03.out +2 -0
  9. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate04.out +2 -0
  10. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate12.out +4 -0
  11. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate13.out +2 -0
  12. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate41.out +2 -0
  13. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate42.out +2 -0
  14. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate45.out +2 -0
  15. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate51.out +2 -0
  16. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate52.out +2 -0
  17. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate56.out +2 -0
  18. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate57.out +5 -0
  19. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/predicate/predicate58.out +11 -0
  20. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select01.out +2 -0
  21. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select02.out +4 -0
  22. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select03.out +2 -0
  23. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select04.out +14 -0
  24. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select12.out +2 -0
  25. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select13.out +13 -0
  26. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select41.out +5 -0
  27. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select42.out +5 -0
  28. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select45.out +5 -0
  29. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select51.out +5 -0
  30. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select52.out +3 -0
  31. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select56.out +7 -0
  32. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select57.out +7 -0
  33. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select58.out +5 -0
  34. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select60.out +5 -0
  35. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select62.out +2 -0
  36. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select63.out +2 -0
  37. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select76.out +2 -0
  38. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select81.out +10 -0
  39. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select82.out +2 -0
  40. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select83.out +7 -0
  41. data/test/scxml/Xalan_Conformance_Tests/REF_OUT/select/select84.out +8 -0
  42. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate01.txt +8 -0
  43. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate01.xml +7 -0
  44. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate01.xsl +18 -0
  45. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate02.txt +8 -0
  46. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate02.xml +7 -0
  47. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate02.xsl +18 -0
  48. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate04.txt +8 -0
  49. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate04.xml +7 -0
  50. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate04.xsl +18 -0
  51. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate07.txt +9 -0
  52. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate07.xml +7 -0
  53. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate07.xsl +20 -0
  54. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate08.txt +9 -0
  55. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate08.xml +7 -0
  56. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate08.xsl +20 -0
  57. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate10.txt +8 -0
  58. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate10.xml +7 -0
  59. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate10.xsl +20 -0
  60. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate14.txt +9 -0
  61. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate14.xml +9 -0
  62. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate14.xsl +20 -0
  63. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate15.txt +9 -0
  64. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate15.xml +9 -0
  65. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate15.xsl +20 -0
  66. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate16.txt +9 -0
  67. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate16.xml +9 -0
  68. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate16.xsl +20 -0
  69. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate17.txt +9 -0
  70. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate17.xml +11 -0
  71. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate17.xsl +20 -0
  72. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate36.txt +8 -0
  73. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate36.xml +7 -0
  74. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate36.xsl +20 -0
  75. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate48.txt +8 -0
  76. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate48.xml +21 -0
  77. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate48.xsl +22 -0
  78. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate49.txt +8 -0
  79. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate49.xml +21 -0
  80. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate49.xsl +22 -0
  81. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate50.txt +8 -0
  82. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate50.xml +21 -0
  83. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate50.xsl +22 -0
  84. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate51.txt +8 -0
  85. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate51.xml +21 -0
  86. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate51.xsl +22 -0
  87. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate52.txt +8 -0
  88. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate52.xml +21 -0
  89. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate52.xsl +22 -0
  90. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate53.txt +8 -0
  91. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate53.xml +21 -0
  92. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate53.xsl +22 -0
  93. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate54.txt +7 -0
  94. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate54.xml +21 -0
  95. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate54.xsl +21 -0
  96. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate55.txt +8 -0
  97. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate55.xml +21 -0
  98. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate55.xsl +22 -0
  99. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate56.txt +8 -0
  100. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate56.xml +21 -0
  101. data/test/scxml/Xalan_Conformance_Tests/predicate/predicate56.xsl +22 -0
  102. data/test/scxml/Xalan_Conformance_Tests/predicate/prepare.rb +34 -0
  103. data/test/scxml/Xalan_Conformance_Tests/select/prepare.rb +34 -0
  104. data/test/scxml/Xalan_Conformance_Tests/select/select01.txt +7 -0
  105. data/test/scxml/Xalan_Conformance_Tests/select/select01.xml +9 -0
  106. data/test/scxml/Xalan_Conformance_Tests/select/select01.xsl +19 -0
  107. data/test/scxml/Xalan_Conformance_Tests/select/select02.txt +7 -0
  108. data/test/scxml/Xalan_Conformance_Tests/select/select02.xml +4 -0
  109. data/test/scxml/Xalan_Conformance_Tests/select/select02.xsl +17 -0
  110. data/test/scxml/Xalan_Conformance_Tests/select/select03.txt +8 -0
  111. data/test/scxml/Xalan_Conformance_Tests/select/select03.xml +13 -0
  112. data/test/scxml/Xalan_Conformance_Tests/select/select03.xsl +19 -0
  113. data/test/scxml/Xalan_Conformance_Tests/select/select04.txt +12 -0
  114. data/test/scxml/Xalan_Conformance_Tests/select/select04.xml +12 -0
  115. data/test/scxml/Xalan_Conformance_Tests/select/select04.xsl +35 -0
  116. data/test/scxml/Xalan_Conformance_Tests/select/select12.txt +7 -0
  117. data/test/scxml/Xalan_Conformance_Tests/select/select12.xml +9 -0
  118. data/test/scxml/Xalan_Conformance_Tests/select/select12.xsl +19 -0
  119. data/test/scxml/Xalan_Conformance_Tests/select/select13.txt +13 -0
  120. data/test/scxml/Xalan_Conformance_Tests/select/select13.xml +18 -0
  121. data/test/scxml/Xalan_Conformance_Tests/select/select13.xsl +33 -0
  122. data/test/scxml/Xalan_Conformance_Tests/select/select41.txt +7 -0
  123. data/test/scxml/Xalan_Conformance_Tests/select/select41.xml +9 -0
  124. data/test/scxml/Xalan_Conformance_Tests/select/select41.xsl +23 -0
  125. data/test/scxml/Xalan_Conformance_Tests/select/select42.txt +8 -0
  126. data/test/scxml/Xalan_Conformance_Tests/select/select42.xml +9 -0
  127. data/test/scxml/Xalan_Conformance_Tests/select/select42.xsl +26 -0
  128. data/test/scxml/Xalan_Conformance_Tests/select/select45.txt +7 -0
  129. data/test/scxml/Xalan_Conformance_Tests/select/select45.xml +24 -0
  130. data/test/scxml/Xalan_Conformance_Tests/select/select45.xsl +25 -0
  131. data/test/scxml/Xalan_Conformance_Tests/select/select51.txt +7 -0
  132. data/test/scxml/Xalan_Conformance_Tests/select/select51.xml +24 -0
  133. data/test/scxml/Xalan_Conformance_Tests/select/select51.xsl +25 -0
  134. data/test/scxml/Xalan_Conformance_Tests/select/select52.txt +6 -0
  135. data/test/scxml/Xalan_Conformance_Tests/select/select52.xml +6 -0
  136. data/test/scxml/Xalan_Conformance_Tests/select/select52.xsl +23 -0
  137. data/test/scxml/Xalan_Conformance_Tests/select/select56.txt +7 -0
  138. data/test/scxml/Xalan_Conformance_Tests/select/select56.xml +29 -0
  139. data/test/scxml/Xalan_Conformance_Tests/select/select56.xsl +25 -0
  140. data/test/scxml/Xalan_Conformance_Tests/select/select57.txt +7 -0
  141. data/test/scxml/Xalan_Conformance_Tests/select/select57.xml +29 -0
  142. data/test/scxml/Xalan_Conformance_Tests/select/select57.xsl +25 -0
  143. data/test/scxml/Xalan_Conformance_Tests/select/select58.txt +7 -0
  144. data/test/scxml/Xalan_Conformance_Tests/select/select58.xml +17 -0
  145. data/test/scxml/Xalan_Conformance_Tests/select/select58.xsl +25 -0
  146. data/test/scxml/Xalan_Conformance_Tests/select/select60.txt +6 -0
  147. data/test/scxml/Xalan_Conformance_Tests/select/select60.xml +9 -0
  148. data/test/scxml/Xalan_Conformance_Tests/select/select60.xsl +25 -0
  149. data/test/scxml/Xalan_Conformance_Tests/select/select62.txt +6 -0
  150. data/test/scxml/Xalan_Conformance_Tests/select/select62.xml +5 -0
  151. data/test/scxml/Xalan_Conformance_Tests/select/select62.xsl +22 -0
  152. data/test/scxml/Xalan_Conformance_Tests/select/select63.txt +7 -0
  153. data/test/scxml/Xalan_Conformance_Tests/select/select63.xml +10 -0
  154. data/test/scxml/Xalan_Conformance_Tests/select/select63.xsl +19 -0
  155. data/test/scxml/Xalan_Conformance_Tests/select/select76.txt +7 -0
  156. data/test/scxml/Xalan_Conformance_Tests/select/select76.xml +6 -0
  157. data/test/scxml/Xalan_Conformance_Tests/select/select76.xsl +17 -0
  158. data/test/scxml/Xalan_Conformance_Tests/select/select81.txt +9 -0
  159. data/test/scxml/Xalan_Conformance_Tests/select/select81.xml +6 -0
  160. data/test/scxml/Xalan_Conformance_Tests/select/select81.xsl +24 -0
  161. data/test/scxml/Xalan_Conformance_Tests/select/select82.txt +9 -0
  162. data/test/scxml/Xalan_Conformance_Tests/select/select82.xml +5 -0
  163. data/test/scxml/Xalan_Conformance_Tests/select/select82.xsl +28 -0
  164. data/test/scxml/Xalan_Conformance_Tests/select/select83.txt +10 -0
  165. data/test/scxml/Xalan_Conformance_Tests/select/select83.xml +10 -0
  166. data/test/scxml/Xalan_Conformance_Tests/select/select83.xsl +32 -0
  167. data/test/scxml/Xalan_Conformance_Tests/select/select84.txt +11 -0
  168. data/test/scxml/Xalan_Conformance_Tests/select/select84.xml +10 -0
  169. data/test/scxml/Xalan_Conformance_Tests/select/select84.xsl +33 -0
  170. data/test/scxml/document_test.rb +231 -0
  171. data/test/scxml/element_test.rb +552 -0
  172. data/test/scxml_test_helper.rb +73 -0
  173. data/test/scxml_test_suite.rb +2 -0
  174. metadata +241 -0
@@ -0,0 +1,552 @@
1
+ require File.join(File.dirname(__FILE__), '../scxml_test_helper')
2
+ require 'stringio'
3
+
4
+ class ElementTest < Test::Unit::TestCase
5
+ include SCXML
6
+ include Benchmark
7
+
8
+ def doc_str
9
+ %Q{
10
+ <root>
11
+ <tags>
12
+ <tag>a</tag>
13
+ <tag >b</tag>
14
+ </tags>
15
+ <tags key='value'>
16
+ <tag id='0' another='value'>c</tag>
17
+ </tags>
18
+ <another>
19
+ <tag id='1'>d</tag>
20
+ </another>
21
+ <tag id='2'>e</tag>
22
+ </root>}
23
+ end
24
+
25
+ def doc
26
+ Document.new(doc_str, :remove_whitespace => true)
27
+ end
28
+
29
+ #
30
+ # initialization tests
31
+ #
32
+
33
+ def test_initialize_without_range_returns_doc_root
34
+ s = Element.new("")
35
+ doc = s.doc
36
+ assert_equal doc.root, s
37
+
38
+ s1 = Element.new(s.doc)
39
+ assert_equal doc.root, s1
40
+
41
+ s2 = Element.new(s)
42
+ assert_equal doc.root, s2
43
+ end
44
+
45
+ def test_initialize_sets_root_attributes_and_content
46
+ cases = {
47
+ # basic
48
+ "<tag>a</tag>" => ['tag', {}, 'a'],
49
+ "<tag >a</tag>" => ['tag', {}, 'a'],
50
+ # with attributes
51
+ "<tag id='0'>a</tag>" => ['tag', {'id' => '0'}, 'a'],
52
+ "<tag id='0' key='value'>a</tag>" => ['tag', {'id' => '0', 'key' => 'value'}, 'a'],
53
+ # with variable content
54
+ "<tag>some longer content</tag>" => ['tag', {}, 'some longer content'],
55
+ "<tag><inner>a</inner></tag>" => ['tag', {}, '<inner>a</inner>'],
56
+ # with newline whitespace
57
+ "\n<tag>\n <inner>\n a\n </inner>\n</tag>\n" => ['tag', {}, "\n <inner>\n a\n </inner>\n"]}
58
+
59
+ cases.each_pair do |str, expected|
60
+ s = Element.new(str)
61
+ assert_equal expected, [s.name, s.attributes, s.content]
62
+ assert_equal str, s.doc.string
63
+ end
64
+ end
65
+
66
+ #
67
+ # content tests
68
+ #
69
+
70
+ def test_content
71
+ cases = {
72
+ %Q{<?xml version="1.0"?>\n<doc>\n</doc>\n} => "\n",
73
+ %Q{<?xml version="1.0"?>\n<doc/>\n} => "",
74
+ %Q{<doc><a></a></doc>} => "<a></a>",
75
+ %Q{<doc><a/></doc>} => "<a/>",
76
+ %Q{} => ""}
77
+
78
+ cases.each_pair do |string, expected|
79
+ e = Element.new(string)
80
+ assert_equal expected, e.content, string
81
+ end
82
+ end
83
+
84
+ def test_content_with_objectify_true
85
+ cases = {
86
+ # no content
87
+ "<tag></tag>" => "",
88
+ "<tag/>" => "",
89
+ # int
90
+ "<tag>1</tag>" => 1,
91
+ "<tag> 10 </tag>" => 10,
92
+ # float
93
+ "<tag>1.0</tag>" => 1.0,
94
+ "<tag>0.1</tag>" => 0.1,
95
+ "<tag> .1 </tag>" => 0.1,
96
+ # bool
97
+ "<tag>true</tag>" => true,
98
+ "<tag>false</tag>" => false,
99
+ "<tag> TrUe </tag>" => true,
100
+ # string
101
+ "<tag> some string </tag>" => ' some string ',
102
+ "<tag><inner>a</inner></tag>" => '<inner>a</inner>'}
103
+
104
+ cases.each_pair do |str, expected|
105
+ s = Element.new(str)
106
+ assert_equal expected, s.content(true)
107
+ end
108
+ end
109
+
110
+ #
111
+ # children tests
112
+ #
113
+
114
+ def test_children
115
+ cases = {
116
+ %Q{<?xml version="1.0"?>\n<doc><a>\n</a></doc>\n} => ['a'],
117
+ %Q{<doc><a></a></doc>} => ['a'],
118
+ %Q{<doc><a/></doc>} => ['a'],
119
+ %Q{} => [],
120
+ # test results unique when in sequence
121
+ %Q{<doc><a></a><a></a></doc>} => ['a', 'a'],
122
+ %Q{<doc><a/><a/></doc>} => ['a', 'a'],
123
+ # test results unique when nested
124
+ %Q{<doc><a><a></a></a></doc>} => ['a'],
125
+ %Q{<doc><a><a/></a></doc>} => ['a']}
126
+
127
+ cases.each_pair do |string, expected|
128
+ e = Element.new(string)
129
+ children_names = e.children.collect { |c| c.name}
130
+ assert_equal expected, children_names, string
131
+ end
132
+ end
133
+
134
+ #
135
+ # select tests
136
+ #
137
+
138
+ def assert_selected(element, xpath, expected)
139
+ assert_equal expected, element.select(xpath), "selecting on => '#{xpath}'"
140
+ end
141
+
142
+ def test_select_with_self
143
+ e = doc.root
144
+ assert_selected(e, '.', [e])
145
+ end
146
+
147
+ def test_select_document
148
+ e = doc.root
149
+ assert_selected(e, '/', [e.doc])
150
+ end
151
+
152
+ def test_select_root_element
153
+ e = doc.root
154
+ assert_selected(e, '/root', [e])
155
+ end
156
+
157
+ def test_select_parent
158
+ e = doc.root
159
+ assert_selected(e, '..', [])
160
+ end
161
+
162
+ def assert_selected_content(element, xpath, expected)
163
+ result = element.select(xpath).collect {|selected| selected.string}
164
+ assert_equal expected, result, "selecting on => '#{xpath}'"
165
+ end
166
+
167
+ def test_select_with_astrix_picks_all_children_nodes
168
+ s = Element.new(doc)
169
+
170
+ expected = [
171
+ "<tags><tag>a</tag><tag >b</tag></tags>",
172
+ "<tags key='value'><tag id='0' another='value'>c</tag></tags>",
173
+ "<another><tag id='1'>d</tag></another>",
174
+ "<tag id='2'>e</tag>"]
175
+
176
+ assert_selected_content(s, '*', expected)
177
+ end
178
+
179
+ def btest_selected_elements_have_parent_set
180
+ s = Element.new(doc)
181
+
182
+ s.select("//tags").each do |e|
183
+ assert_equal s, e.parent
184
+ end
185
+ end
186
+
187
+ def test_select_picks_all_tags_matching_path
188
+ s = Element.new(doc)
189
+
190
+ tags = [
191
+ "<tag>a</tag>",
192
+ "<tag >b</tag>",
193
+ "<tag id='0' another='value'>c</tag>",
194
+ "<tag id='1'>d</tag>",
195
+ "<tag id='2'>e</tag>"]
196
+
197
+ case_test(
198
+ "//tag" => tags,
199
+ "root//tag" => [],
200
+ "/root//tag" => tags,
201
+
202
+ "root/tags/tag" => [],
203
+ "/root/tags/tag" => tags[0..2],
204
+ "/root/tags/*" => tags[0..2],
205
+ "tags/tag" => tags[0..2],
206
+ "another/tag" => [tags[3]],
207
+ "/root/another/tag" => [tags[3]],
208
+
209
+ "//tag[@id]" => tags[2..4],
210
+ "//tag[@id='0']" => [tags[2]]
211
+ #"//@id" => tags[2..4]
212
+ ) do |path, expected|
213
+ assert_selected_content(s, path, expected)
214
+ end
215
+ end
216
+
217
+ def test_select_speed
218
+ benchmark_test do
219
+ bm(25) do |x|
220
+ doc = nil
221
+ xml_string = ""
222
+ n_rows = 1000
223
+ n_cols = 10
224
+
225
+ x.report('Builder make') do
226
+ builder = Builder::XmlMarkup.new :target => xml_string
227
+ builder.root {
228
+ builder.rows {
229
+ 1.upto(n_rows) do |i|
230
+ builder.row('id' => i.to_s) {
231
+ 1.upto(n_cols) do |j|
232
+ builder.col("[#{i}, #{j}]", 'id' => j.to_s)
233
+ end
234
+ }
235
+ end
236
+ }
237
+ }
238
+ end
239
+
240
+ puts " XML length: #{xml_string.length}"
241
+ puts " n rows: #{n_rows}"
242
+ puts " n cols: #{n_rows*n_cols}"
243
+
244
+ x.report('SCXML parse') { doc = Document.new(xml_string) }
245
+ x.report('SCXML nodes') { assert_equal(['root', 'rows', 'row', 'col'], doc.node_names) }
246
+ x.report('SCXML select //row') { assert_equal n_rows, doc.select('//row').length }
247
+ x.report('SCXML select rows/row') { assert_equal n_rows, doc.root.select('rows/row').length }
248
+ x.report("SCXML select rows/row[@id='10']") {assert_equal 1, doc.root.select("rows/row[@id='10']").length }
249
+ x.report('SCXML select //col') {assert_equal n_rows*n_cols, doc.select('//col').length }
250
+ x.report("SCXML select //col[@id='10']") {assert_equal n_rows, doc.select("//col[@id='10']").length }
251
+ end
252
+ end
253
+ end
254
+
255
+ #
256
+ # rewrite tests
257
+ #
258
+
259
+ def test_rewrite_indentation
260
+ case_test(
261
+ # basic cases
262
+ "<a></a>" => " <a></a>",
263
+ "<a>content</a>" => " <a>content</a>",
264
+ "<a><b></b></a>" => " <a>\n <b></b>\n </a>",
265
+ "<a><b><c></c></b></a>" => " <a>\n <b>\n <c></c>\n </b>\n </a>",
266
+ "<a>\n <b></b>\n</a>" => " <a>\n <b></b>\n </a>",
267
+ "<a><b>content</b></a>" => " <a>\n <b>content</b>\n </a>",
268
+ "<a>\n <b>\n <c></c>\n </b>\n</a>" => " <a>\n <b>\n <c></c>\n </b>\n </a>",
269
+
270
+ # cases with closed tags
271
+ "<a/>" => " <a/>",
272
+ "<a key='value'/>" => " <a key='value'/>",
273
+ "<a><b/></a>" => " <a>\n <b/>\n </a>",
274
+
275
+ # funky whitespace, note whitespace in content is preserved
276
+ " <a> </a> \n\t " => " <a> </a>",
277
+ " <a>\n <b> content </b>\n\n \t</a> " => " <a>\n <b> content </b>\n </a>"
278
+
279
+ # not supported
280
+ #"<a>content<b></b></a>" => " <a>content\n <b></b>\n </a>"
281
+ ) do |doc, expected|
282
+ s = Element.new(doc)
283
+ assert_equal expected, s.rewrite(:indent => 2), doc
284
+ end
285
+ end
286
+
287
+ def test_rewrite_with_new_attributes
288
+ s = Element.new("<a key='value'></a>")
289
+ assert_equal({'key' => 'value'}, s.attributes)
290
+
291
+ case_test(
292
+ # basic cases
293
+ {:one => '1'} => "<a one='1'></a>"
294
+
295
+ # can't test this due to hashing... workaround?
296
+ #{:one => '1', :two => '2'} => "<a two='2' one='1'></a>"
297
+ ) do |attr, expected|
298
+ assert_equal expected, s.rewrite(:attributes => attr), attr
299
+ end
300
+ end
301
+
302
+ def test_merge_attributes_merges_input_with_attributes
303
+ s = Element.new("<a key='value'></a>")
304
+ assert_equal({'key' => 'value'}, s.attributes)
305
+
306
+ case_test(
307
+ {'one' => '1'} => {'one' => '1', 'key' => 'value'},
308
+ {'key' => 'another'} => {'key' => 'another'}
309
+ ) do |attr, expected|
310
+ e = Element.new(s.rewrite(:merge_attributes => attr))
311
+ assert_equal expected, e.attributes, attr
312
+ end
313
+ end
314
+
315
+ def test_rewrite_with_new_content
316
+ s = Element.new("<a></a>")
317
+ assert_equal "<a>content</a>", s.rewrite(:add_content => "content")
318
+
319
+ s = Element.new("<a>content</a>")
320
+ assert_equal "<a>content added</a>", s.rewrite(:add_content => " added")
321
+
322
+ s = Element.new("<a><b/></a>")
323
+ assert_equal "<a>\n <b/>\n <c/>\n</a>", s.rewrite(:add_content => "<c/>")
324
+
325
+ s = Element.new("<a><b/></a>")
326
+ assert_equal "<a>\n <b/>\n <c>\n <d></d>\n </c>\n</a>", s.rewrite(:add_content => "<c><d></d></c>")
327
+ end
328
+
329
+ #
330
+ # test hashify
331
+ #
332
+
333
+ def test_hashify_returns_hash_with_name_content_pairs
334
+ input = ""
335
+ x = Builder::XmlMarkup.new :target => input
336
+ x.root {
337
+ x.key('value')
338
+ x.another('another value')
339
+ }
340
+
341
+ e = Element.new(input)
342
+ assert_equal({'key' => 'value', 'another' => 'another value'}, e.hashify)
343
+ end
344
+
345
+ def test_hashify_xpath_selects_elements_to_hashify
346
+ input = ""
347
+ x = Builder::XmlMarkup.new :target => input
348
+ x.root {
349
+ x.one('1', 'id' => 'a')
350
+ x.two('2', 'id' => 'b')
351
+ x.three('3', 'id' => 'a')
352
+ }
353
+
354
+ e = Element.new(input)
355
+ assert_equal({'one' => '1', 'three' => '3'}, e.hashify("/root/*[@id='a']"))
356
+ end
357
+
358
+ def test_hashify_block_determines_key
359
+ input = ""
360
+ x = Builder::XmlMarkup.new :target => input
361
+ x.root {
362
+ x.element('1', 'id' => 'a')
363
+ x.element('2', 'id' => 'b')
364
+ x.element('3', 'id' => 'c')
365
+ }
366
+
367
+ e = Element.new(input)
368
+ hash = e.hashify do |element|
369
+ element.attributes['id']
370
+ end
371
+
372
+ assert_equal({'a' => '1', 'b' => '2', 'c' => '3'}, hash)
373
+ end
374
+
375
+ def test_attribute_returns_attribute_if_present
376
+ s = Element.new("<a key='value'></a>")
377
+ assert_equal('value', s.attribute('key'))
378
+ end
379
+
380
+ def test_attribute_returns_nil_if_absent
381
+ s = Element.new("<a></a>")
382
+ assert_equal(nil, s.attribute('key'))
383
+
384
+ s = Element.new("<a another='value'></a>")
385
+ assert_equal(nil, s.attribute('key'))
386
+ end
387
+ end
388
+
389
+ class Hold
390
+ #
391
+ # insert tests
392
+ #
393
+
394
+ def assert_element(element, string, string_range, content, content_range)
395
+ assert_equal string, element.string, "#{element.name} => string"
396
+ assert_equal string_range, element.string_range, "#{element.name} => string_range"
397
+ assert_equal content, element.content, "#{element.name} => content"
398
+ assert_equal content_range, element.content_range, "#{element.name} => content_range"
399
+ end
400
+
401
+ def test_insert
402
+ [6, -7].each do |index|
403
+ s = Element.new("<root></root>")
404
+ assert_element(s, "<root></root>", 0...s.doc.string.length, "", 6...-7)
405
+
406
+ s.insert(index, "text")
407
+ assert_element(s, "<root>text</root>", 0...s.doc.string.length, "text", 6...-7)
408
+ end
409
+ end
410
+
411
+ def nested_insert_setup
412
+ a = Element.new("<a><b><c></c></b></a>")
413
+ b = a.select('b').first
414
+ c = a.select('b/c').first
415
+ [a,b,c]
416
+ end
417
+
418
+ def test_nested_insert_setup
419
+ a,b,c = nested_insert_setup
420
+
421
+ assert_equal 21, a.doc.string.length
422
+
423
+ # string | string_range | content | content_range
424
+ {a => ["<a><b><c></c></b></a>", 0...21, "<b><c></c></b>", 3...-4],
425
+ b => ["<b><c></c></b>", 3...-4, "<c></c>", 3...-4],
426
+ c => ["<c></c>", 6...-8, "", 3...-4]}.each_pair do |e, expected|
427
+ assert_element(e, *expected)
428
+ end
429
+ end
430
+
431
+ def test_insert_into_nested
432
+ [3, -4].each do |index|
433
+ a,b,c = nested_insert_setup
434
+
435
+ c.insert(index, "text")
436
+
437
+ # string | string_range | content | content_range
438
+ {a => ["<a><b><c>text</c></b></a>", 0...25, "<b><c>text</c></b>", 3...-4],
439
+ b => ["<b><c>text</c></b>", 3...-4, "<c>text</c>", 3...-4],
440
+ c => ["<c>text</c>", 6...-8, "text", 3...-4]}.each_pair do |e, expected|
441
+ assert_element(e, *expected)
442
+ end
443
+ end
444
+ end
445
+
446
+ def test_insert_before_nested
447
+ # -11 = -4 - content_length
448
+ [3,-11].each do |index|
449
+ a,b,c = nested_insert_setup
450
+
451
+ b.insert(index, "text")
452
+
453
+ # string | string_range | content | content_range
454
+ {a => ["<a><b>text<c></c></b></a>", 0...25, "<b>text<c></c></b>", 3...-4],
455
+ b => ["<b>text<c></c></b>", 3...-4, "text<c></c>", 3...-4],
456
+ c => ["<c></c>", 10...-8, "", 3...-4]}.each_pair do |e, expected|
457
+ assert_element(e, *expected)
458
+ end
459
+ end
460
+ end
461
+
462
+ def test_insert_after_nested
463
+ # 10 = 3 + content_length
464
+ [10, -4].each do |index|
465
+ a,b,c = nested_insert_setup
466
+
467
+ b.insert(index, "text")
468
+
469
+ # string | string_range | content | content_range
470
+ {a => ["<a><b><c></c>text</b></a>", 0...25, "<b><c></c>text</b>", 3...-4],
471
+ b => ["<b><c></c>text</b>", 3...-4, "<c></c>text", 3...-4],
472
+ c => ["<c></c>", 6...-12, "", 3...-4]}.each_pair do |e, expected|
473
+ assert_element(e, *expected)
474
+ end
475
+ end
476
+ end
477
+
478
+ #
479
+ # remove tests
480
+ #
481
+
482
+ def test_remove
483
+ [6, -11].each do |index|
484
+ s = Element.new("<root>text</root>")
485
+ assert_element(s, "<root>text</root>", 0...s.doc.string.length, "text", 6...-7)
486
+
487
+ s.remove(index, 4)
488
+ assert_element(s, "<root></root>", 0...s.doc.string.length, "", 6...-7)
489
+ end
490
+ end
491
+
492
+ def nested_remove_setup
493
+ a = Element.new("<a><b>text<c>text</c>text</b></a>")
494
+ b = a.select('b').first
495
+ c = a.select('b/c').first
496
+ [a,b,c]
497
+ end
498
+
499
+ def test_nested_remove_setup
500
+ a,b,c = nested_remove_setup
501
+
502
+ assert_equal 33, a.doc.string.length
503
+
504
+ {a => ["<a><b>text<c>text</c>text</b></a>", 0...33, "<b>text<c>text</c>text</b>", 3...-4],
505
+ b => ["<b>text<c>text</c>text</b>", 3...-4, "text<c>text</c>text", 3...-4],
506
+ c => ["<c>text</c>", 10...-12, "text", 3...-4]}.each_pair do |e, expected|
507
+ assert_element(e, *expected)
508
+ end
509
+ end
510
+
511
+ def test_remove_from_nested
512
+ [3, -8].each do |index|
513
+
514
+ a,b,c = nested_remove_setup
515
+
516
+ c.remove(index, 4)
517
+ {a => ["<a><b>text<c></c>text</b></a>", 0...29, "<b>text<c></c>text</b>", 3...-4],
518
+ b => ["<b>text<c></c>text</b>", 3...-4, "text<c></c>text", 3...-4],
519
+ c => ["<c></c>", 10...-12, "", 3...-4]}.each_pair do |e, expected|
520
+ assert_element(e, *expected)
521
+ end
522
+ end
523
+ end
524
+
525
+ def test_remove_before_nested
526
+ [3, -23].each do |index|
527
+ a,b,c = nested_remove_setup
528
+
529
+ b.remove(index, 4)
530
+
531
+ {a => ["<a><b><c>text</c>text</b></a>", 0...29, "<b><c>text</c>text</b>", 3...-4],
532
+ b => ["<b><c>text</c>text</b>", 3...-4, "<c>text</c>text", 3...-4],
533
+ c => ["<c>text</c>", 6...-12, "text", 3...-4]}.each_pair do |e, expected|
534
+ assert_element(e, *expected)
535
+ end
536
+ end
537
+ end
538
+
539
+ def test_remove_after_nested
540
+ [18, -8].each do |index|
541
+ a,b,c = nested_remove_setup
542
+
543
+ b.remove(index, 4)
544
+
545
+ {a => ["<a><b>text<c>text</c></b></a>", 0...29, "<b>text<c>text</c></b>", 3...-4],
546
+ b => ["<b>text<c>text</c></b>", 3...-4, "text<c>text</c>", 3...-4],
547
+ c => ["<c>text</c>", 10...-8, "text", 3...-4]}.each_pair do |e, expected|
548
+ assert_element(e, *expected)
549
+ end
550
+ end
551
+ end
552
+ end