|
1 #!/usr/local/bin/lua |
|
2 |
|
3 local totable = require "lxp.totable" |
|
4 |
|
5 local tests = { |
|
6 { |
|
7 [[<abc a1="A1" a2="A2">inside tag `abc'</abc>]], |
|
8 { |
|
9 [0] = "abc", |
|
10 a1 = "A1", |
|
11 a2 = "A2", |
|
12 "inside tag `abc'", |
|
13 }, |
|
14 }, |
|
15 { |
|
16 [[<qwerty q1="q1" q2="q2"> |
|
17 <asdf>some text</asdf> |
|
18 </qwerty>]], |
|
19 { |
|
20 [0] = "qwerty", |
|
21 q1 = "q1", |
|
22 q2 = "q2", |
|
23 "\n\t", |
|
24 { |
|
25 [0] = "asdf", |
|
26 "some text", |
|
27 }, |
|
28 "\n", |
|
29 }, |
|
30 }, |
|
31 { |
|
32 [[ |
|
33 <!-- http://www.w3schools.com/xml/simple.xml --> |
|
34 <breakfast_menu> |
|
35 <food> |
|
36 <name>Belgian Waffles</name> |
|
37 <price>$5.95</price> |
|
38 <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description> |
|
39 <calories>650</calories> |
|
40 </food> |
|
41 <food> |
|
42 <name>Strawberry Belgian Waffles</name> |
|
43 <price>$7.95</price> |
|
44 <description>Light Belgian waffles covered with strawberries and whipped cream</description> |
|
45 <calories>900</calories> |
|
46 </food> |
|
47 <food> |
|
48 <name>Berry-Berry Belgian Waffles</name> |
|
49 <price>$8.95</price> |
|
50 <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> |
|
51 <calories>900</calories> |
|
52 </food> |
|
53 <food> |
|
54 <name>French Toast</name> |
|
55 <price>$4.50</price> |
|
56 <description>Thick slices made from our homemade sourdough bread</description> |
|
57 <calories>600</calories> |
|
58 </food> |
|
59 <food> |
|
60 <name>Homestyle Breakfast</name> |
|
61 <price>$6.95</price> |
|
62 <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> |
|
63 <calories>950</calories> |
|
64 </food> |
|
65 </breakfast_menu>]], |
|
66 { |
|
67 [0] = "breakfast_menu", |
|
68 [1] = "\n\t", |
|
69 [2] = { |
|
70 [0] = "food", |
|
71 [1] = "\n\t\t", |
|
72 [2] = { [0] = "name", [1] = "Belgian Waffles", }, |
|
73 [3] = "\n\t\t", |
|
74 [4] = { [0] = "price", [1] = "$5.95", }, |
|
75 [5] = "\n\t\t", |
|
76 [6] = { |
|
77 [0] = "description", |
|
78 [1] = "Two of our famous Belgian Waffles with plenty of real maple syrup", |
|
79 }, |
|
80 [7] = "\n\t\t", |
|
81 [8] = { [0] = "calories", [1] = "650", }, |
|
82 [9] = "\n\t", |
|
83 }, |
|
84 [3] = "\n\t", |
|
85 [4] = { |
|
86 [0] = "food", |
|
87 [1] = "\n\t\t", |
|
88 [2] = { [0] = "name", [1] = "Strawberry Belgian Waffles", }, |
|
89 [3] = "\n\t\t", |
|
90 [4] = { [0] = "price", [1] = "$7.95", }, |
|
91 [5] = "\n\t\t", |
|
92 [6] = { |
|
93 [0] = "description", |
|
94 [1] = "Light Belgian waffles covered with strawberries and whipped cream", |
|
95 }, |
|
96 [7] = "\n\t\t", |
|
97 [8] = { [0] = "calories", [1] = "900", }, |
|
98 [9] = "\n\t", |
|
99 }, |
|
100 [5] = "\n\t", |
|
101 [6] = { |
|
102 [0] = "food", |
|
103 [1] = "\n\t\t", |
|
104 [2] = { [0] = "name", [1] = "Berry-Berry Belgian Waffles", }, |
|
105 [3] = "\n\t\t", |
|
106 [4] = { [0] = "price", [1] = "$8.95", }, |
|
107 [5] = "\n\t\t", |
|
108 [6] = { |
|
109 [0] = "description", |
|
110 [1] = "Light Belgian waffles covered with an assortment of fresh berries and whipped cream", |
|
111 }, |
|
112 [7] = "\n\t\t", |
|
113 [8] = { [0] = "calories", [1] = "900", }, |
|
114 [9] = "\n\t", |
|
115 }, |
|
116 [7] = "\n\t", |
|
117 [8] = { |
|
118 [0] = "food", |
|
119 [1] = "\n\t\t", |
|
120 [2] = { [0] = "name", [1] = "French Toast", }, |
|
121 [3] = "\n\t\t", |
|
122 [4] = { [0] = "price", [1] = "$4.50", }, |
|
123 [5] = "\n\t\t", |
|
124 [6] = { |
|
125 [0] = "description", |
|
126 [1] = "Thick slices made from our homemade sourdough bread", |
|
127 }, |
|
128 [7] = "\n\t\t", |
|
129 [8] = { [0] = "calories", [1] = "600", }, |
|
130 [9] = "\n\t", |
|
131 }, |
|
132 [9] = "\n\t", |
|
133 [10] = { |
|
134 [0] = "food", |
|
135 [1] = "\n\t\t", |
|
136 [2] = { [0] = "name", [1] = "Homestyle Breakfast", }, |
|
137 [3] = "\n\t\t", |
|
138 [4] = { [0] = "price", [1] = "$6.95", }, |
|
139 [5] = "\n\t\t", |
|
140 [6] = { |
|
141 [0] = "description", |
|
142 [1] = "Two eggs, bacon or sausage, toast, and our ever-popular hash browns", |
|
143 }, |
|
144 [7] = "\n\t\t", |
|
145 [8] = { [0] = "calories", [1] = "950", }, |
|
146 [9] = "\n\t", |
|
147 }, |
|
148 [11] = "\n", |
|
149 }, |
|
150 clean = { |
|
151 [0] = "breakfast_menu", |
|
152 [1] = { |
|
153 [0] = "food", |
|
154 [1] = { [0] = "name", [1] = "Belgian Waffles", }, |
|
155 [2] = { [0] = "price", [1] = "$5.95", }, |
|
156 [3] = { |
|
157 [0] = "description", |
|
158 [1] = "Two of our famous Belgian Waffles with plenty of real maple syrup", |
|
159 }, |
|
160 [4] = { [0] = "calories", [1] = "650", }, |
|
161 }, |
|
162 [2] = { |
|
163 [0] = "food", |
|
164 [1] = { [0] = "name", [1] = "Strawberry Belgian Waffles", }, |
|
165 [2] = { [0] = "price", [1] = "$7.95", }, |
|
166 [3] = { |
|
167 [0] = "description", |
|
168 [1] = "Light Belgian waffles covered with strawberries and whipped cream", |
|
169 }, |
|
170 [4] = { [0] = "calories", [1] = "900", }, |
|
171 }, |
|
172 [3] = { |
|
173 [0] = "food", |
|
174 [1] = { [0] = "name", [1] = "Berry-Berry Belgian Waffles", }, |
|
175 [2] = { [0] = "price", [1] = "$8.95", }, |
|
176 [3] = { |
|
177 [0] = "description", |
|
178 [1] = "Light Belgian waffles covered with an assortment of fresh berries and whipped cream", |
|
179 }, |
|
180 [4] = { [0] = "calories", [1] = "900", }, |
|
181 }, |
|
182 [4] = { |
|
183 [0] = "food", |
|
184 [1] = { [0] = "name", [1] = "French Toast", }, |
|
185 [2] = { [0] = "price", [1] = "$4.50", }, |
|
186 [3] = { |
|
187 [0] = "description", |
|
188 [1] = "Thick slices made from our homemade sourdough bread", |
|
189 }, |
|
190 [4] = { [0] = "calories", [1] = "600", }, |
|
191 }, |
|
192 [5] = { |
|
193 [0] = "food", |
|
194 [1] = { [0] = "name", [1] = "Homestyle Breakfast", }, |
|
195 [2] = { [0] = "price", [1] = "$6.95", }, |
|
196 [3] = { |
|
197 [0] = "description", |
|
198 [1] = "Two eggs, bacon or sausage, toast, and our ever-popular hash browns", |
|
199 }, |
|
200 [4] = { [0] = "calories", [1] = "950", }, |
|
201 }, |
|
202 }, |
|
203 torecord = { |
|
204 [0] = "breakfast_menu", |
|
205 [1] = { |
|
206 [0] = "food", |
|
207 name = "Belgian Waffles", |
|
208 price = "$5.95", |
|
209 description = "Two of our famous Belgian Waffles with plenty of real maple syrup", |
|
210 calories = "650", |
|
211 }, |
|
212 [2] = { |
|
213 [0] = "food", |
|
214 name = "Strawberry Belgian Waffles", |
|
215 price = "$7.95", |
|
216 description = "Light Belgian waffles covered with strawberries and whipped cream", |
|
217 calories = "900", |
|
218 }, |
|
219 [3] = { |
|
220 [0] = "food", |
|
221 name = "Berry-Berry Belgian Waffles", |
|
222 price = "$8.95", |
|
223 description = "Light Belgian waffles covered with an assortment of fresh berries and whipped cream", |
|
224 calories = "900", |
|
225 }, |
|
226 [4] = { |
|
227 [0] = "food", |
|
228 name = "French Toast", |
|
229 price = "$4.50", |
|
230 description = "Thick slices made from our homemade sourdough bread", |
|
231 calories = "600", |
|
232 }, |
|
233 [5] = { |
|
234 [0] = "food", |
|
235 name = "Homestyle Breakfast", |
|
236 price = "$6.95", |
|
237 description = "Two eggs, bacon or sausage, toast, and our ever-popular hash browns", |
|
238 calories = "950", |
|
239 }, |
|
240 }, |
|
241 }, |
|
242 } |
|
243 |
|
244 |
|
245 function table.equal (t1, t2) |
|
246 for nome, val in pairs (t1) do |
|
247 local tv = type(val) |
|
248 if tv == "table" then |
|
249 if type(t2[nome]) ~= "table" then |
|
250 return false, "Different types at entry `"..nome.."': t1."..nome.." is "..tv.." while t2."..nome.." is "..type(t2[nome]).." ["..tostring(t2[nome]).."]" |
|
251 else |
|
252 local ok, msg = table.equal (val, t2[nome]) |
|
253 if not ok then |
|
254 return false, "["..nome.."]\t"..tostring(val).." ~= "..tostring(t2[nome]).."; "..msg |
|
255 end |
|
256 end |
|
257 else |
|
258 if val ~= t2[nome] then |
|
259 return false, "["..nome.."]\t"..tostring(val).." ~= "..tostring(t2[nome]) |
|
260 end |
|
261 end |
|
262 end |
|
263 return true |
|
264 end |
|
265 |
|
266 |
|
267 for i, s in ipairs(tests) do |
|
268 local ds = assert (totable.parse ([[<?xml version="1.0" encoding="ISO-8859-1"?>]]..s[1])) |
|
269 assert(table.equal (ds, s[2])) |
|
270 end |
|
271 |
|
272 local t = totable.parse ([[<?xml version="1.0" encoding="ISO-8859-1"?>]]..tests[3][1]) |
|
273 totable.clean (t) |
|
274 assert (table.equal (t, tests[3].clean)) |
|
275 totable.torecord (t) |
|
276 assert (table.equal (t, tests[3].torecord)) |
|
277 |
|
278 print"OK" |