Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * Copyright 2011 Rackspace
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. var fs = require('fs');
  18. var path = require('path');
  19. var sprintf = require('./../lib/sprintf').sprintf;
  20. var et = require('elementtree');
  21. var XML = et.XML;
  22. var ElementTree = et.ElementTree;
  23. var Element = et.Element;
  24. var SubElement = et.SubElement;
  25. var SyntaxError = require('./../lib/errors').SyntaxError;
  26. function readFile(name) {
  27. return fs.readFileSync(path.join(__dirname, '/data/', name), 'utf8');
  28. }
  29. exports['test_simplest'] = function(test, assert) {
  30. /* Ported from <https://github.com/lxml/lxml/blob/master/src/lxml/tests/test_elementtree.py> */
  31. var Element = et.Element;
  32. var root = Element('root');
  33. root.append(Element('one'));
  34. root.append(Element('two'));
  35. root.append(Element('three'));
  36. assert.equal(3, root.len());
  37. assert.equal('one', root.getItem(0).tag);
  38. assert.equal('two', root.getItem(1).tag);
  39. assert.equal('three', root.getItem(2).tag);
  40. test.finish();
  41. };
  42. exports['test_attribute_values'] = function(test, assert) {
  43. var XML = et.XML;
  44. var root = XML('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>');
  45. assert.equal('Alpha', root.attrib['alpha']);
  46. assert.equal('Beta', root.attrib['beta']);
  47. assert.equal('Gamma', root.attrib['gamma']);
  48. test.finish();
  49. };
  50. exports['test_findall'] = function(test, assert) {
  51. var XML = et.XML;
  52. var root = XML('<a><b><c/></b><b/><c><b/></c></a>');
  53. assert.equal(root.findall("c").length, 1);
  54. assert.equal(root.findall(".//c").length, 2);
  55. assert.equal(root.findall(".//b").length, 3);
  56. assert.equal(root.findall(".//b")[0]._children.length, 1);
  57. assert.equal(root.findall(".//b")[1]._children.length, 0);
  58. assert.equal(root.findall(".//b")[2]._children.length, 0);
  59. assert.deepEqual(root.findall('.//b')[0], root.getchildren()[0]);
  60. test.finish();
  61. };
  62. exports['test_find'] = function(test, assert) {
  63. var a = Element('a');
  64. var b = SubElement(a, 'b');
  65. var c = SubElement(a, 'c');
  66. assert.deepEqual(a.find('./b/..'), a);
  67. test.finish();
  68. };
  69. exports['test_elementtree_find_qname'] = function(test, assert) {
  70. var tree = new et.ElementTree(XML('<a><b><c/></b><b/><c><b/></c></a>'));
  71. assert.deepEqual(tree.find(new et.QName('c')), tree.getroot()._children[2]);
  72. test.finish();
  73. };
  74. exports['test_attrib_ns_clear'] = function(test, assert) {
  75. var attribNS = '{http://foo/bar}x';
  76. var par = Element('par');
  77. par.set(attribNS, 'a');
  78. var child = SubElement(par, 'child');
  79. child.set(attribNS, 'b');
  80. assert.equal('a', par.get(attribNS));
  81. assert.equal('b', child.get(attribNS));
  82. par.clear();
  83. assert.equal(null, par.get(attribNS));
  84. assert.equal('b', child.get(attribNS));
  85. test.finish();
  86. };
  87. exports['test_create_tree_and_parse_simple'] = function(test, assert) {
  88. var i = 0;
  89. var e = new Element('bar', {});
  90. var expected = "<?xml version='1.0' encoding='utf-8'?>\n" +
  91. '<bar><blah a="11" /><blah a="12" /><gag a="13" b="abc">ponies</gag></bar>';
  92. SubElement(e, "blah", {a: 11});
  93. SubElement(e, "blah", {a: 12});
  94. var se = et.SubElement(e, "gag", {a: '13', b: 'abc'});
  95. se.text = 'ponies';
  96. se.itertext(function(text) {
  97. assert.equal(text, 'ponies');
  98. i++;
  99. });
  100. assert.equal(i, 1);
  101. var etree = new ElementTree(e);
  102. var xml = etree.write();
  103. assert.equal(xml, expected);
  104. test.finish();
  105. };
  106. exports['test_write_with_options'] = function(test, assert) {
  107. var i = 0;
  108. var e = new Element('bar', {});
  109. var expected1 = "<?xml version='1.0' encoding='utf-8'?>\n" +
  110. '<bar>\n' +
  111. ' <blah a="11">\n' +
  112. ' <baz d="11">test</baz>\n' +
  113. ' </blah>\n' +
  114. ' <blah a="12" />\n' +
  115. ' <gag a="13" b="abc">ponies</gag>\n' +
  116. '</bar>\n';
  117. var expected2 = "<?xml version='1.0' encoding='utf-8'?>\n" +
  118. '<bar>\n' +
  119. ' <blah a="11">\n' +
  120. ' <baz d="11">test</baz>\n' +
  121. ' </blah>\n' +
  122. ' <blah a="12" />\n' +
  123. ' <gag a="13" b="abc">ponies</gag>\n' +
  124. '</bar>\n';
  125. var expected3 = "<?xml version='1.0' encoding='utf-8'?>\n" +
  126. '<object>\n' +
  127. ' <title>\n' +
  128. ' Hello World\n' +
  129. ' </title>\n' +
  130. ' <children>\n' +
  131. ' <object id="obj1" />\n' +
  132. ' <object id="obj2" />\n' +
  133. ' <object id="obj3" />\n' +
  134. ' </children>\n' +
  135. ' <text>\n' +
  136. ' Test &amp; Test &amp; Test\n' +
  137. ' </text>\n' +
  138. '</object>\n';
  139. var se1 = SubElement(e, "blah", {a: 11});
  140. var se2 = SubElement(se1, "baz", {d: 11});
  141. se2.text = 'test';
  142. SubElement(e, "blah", {a: 12});
  143. var se = et.SubElement(e, "gag", {a: '13', b: 'abc'});
  144. se.text = 'ponies';
  145. se.itertext(function(text) {
  146. assert.equal(text, 'ponies');
  147. i++;
  148. });
  149. assert.equal(i, 1);
  150. var etree = new ElementTree(e);
  151. var xml1 = etree.write({'indent': 4});
  152. var xml2 = etree.write({'indent': 2});
  153. assert.equal(xml1, expected1);
  154. assert.equal(xml2, expected2);
  155. var file = readFile('xml2.xml');
  156. var etree2 = et.parse(file);
  157. var xml3 = etree2.write({'indent': 4});
  158. assert.equal(xml3, expected3);
  159. test.finish();
  160. };
  161. exports['test_parse_and_find_2'] = function(test, assert) {
  162. var data = readFile('xml1.xml');
  163. var etree = et.parse(data);
  164. assert.equal(etree.findall('./object').length, 2);
  165. assert.equal(etree.findall('[@name]').length, 1);
  166. assert.equal(etree.findall('[@name="test_container_1"]').length, 1);
  167. assert.equal(etree.findall('[@name=\'test_container_1\']').length, 1);
  168. assert.equal(etree.findall('./object')[0].findtext('name'), 'test_object_1');
  169. assert.equal(etree.findtext('./object/name'), 'test_object_1');
  170. assert.equal(etree.findall('.//bytes').length, 2);
  171. assert.equal(etree.findall('*/bytes').length, 2);
  172. assert.equal(etree.findall('*/foobar').length, 0);
  173. test.finish();
  174. };
  175. exports['test_namespaced_attribute'] = function(test, assert) {
  176. var data = readFile('xml1.xml');
  177. var etree = et.parse(data);
  178. assert.equal(etree.findall('*/bytes[@android:type="cool"]').length, 1);
  179. test.finish();
  180. }
  181. exports['test_syntax_errors'] = function(test, assert) {
  182. var expressions = [ './/@bar', '[@bar', '[@foo=bar]', '[@', '/bar' ];
  183. var errCount = 0;
  184. var data = readFile('xml1.xml');
  185. var etree = et.parse(data);
  186. expressions.forEach(function(expression) {
  187. try {
  188. etree.findall(expression);
  189. }
  190. catch (err) {
  191. errCount++;
  192. }
  193. });
  194. assert.equal(errCount, expressions.length);
  195. test.finish();
  196. };
  197. exports['test_register_namespace'] = function(test, assert){
  198. var prefix = 'TESTPREFIX';
  199. var namespace = 'http://seriously.unknown/namespace/URI';
  200. var errCount = 0;
  201. var etree = Element(sprintf('{%s}test', namespace));
  202. assert.equal(et.tostring(etree, { 'xml_declaration': false}),
  203. sprintf('<ns0:test xmlns:ns0="%s" />', namespace));
  204. et.register_namespace(prefix, namespace);
  205. var etree = Element(sprintf('{%s}test', namespace));
  206. assert.equal(et.tostring(etree, { 'xml_declaration': false}),
  207. sprintf('<%s:test xmlns:%s="%s" />', prefix, prefix, namespace));
  208. try {
  209. et.register_namespace('ns25', namespace);
  210. }
  211. catch (err) {
  212. errCount++;
  213. }
  214. assert.equal(errCount, 1, 'Reserved prefix used, but exception was not thrown');
  215. test.finish();
  216. };
  217. exports['test_tostring'] = function(test, assert) {
  218. var a = Element('a');
  219. var b = SubElement(a, 'b');
  220. var c = SubElement(a, 'c');
  221. c.text = 543;
  222. assert.equal(et.tostring(a, { 'xml_declaration': false }), '<a><b /><c>543</c></a>');
  223. assert.equal(et.tostring(c, { 'xml_declaration': false }), '<c>543</c>');
  224. test.finish();
  225. };
  226. exports['test_escape'] = function(test, assert) {
  227. var a = Element('a');
  228. var b = SubElement(a, 'b');
  229. b.text = '&&&&<>"\n\r';
  230. assert.equal(et.tostring(a, { 'xml_declaration': false }), '<a><b>&amp;&amp;&amp;&amp;&lt;&gt;\"\n\r</b></a>');
  231. test.finish();
  232. };
  233. exports['test_find_null'] = function(test, assert) {
  234. var root = Element('root');
  235. var node = SubElement(root, 'node');
  236. var leaf = SubElement(node, 'leaf');
  237. leaf.text = 'ipsum';
  238. assert.equal(root.find('node/leaf'), leaf);
  239. assert.equal(root.find('no-such-node/leaf'), null);
  240. test.finish();
  241. };
  242. exports['test_findtext_null'] = function(test, assert) {
  243. var root = Element('root');
  244. var node = SubElement(root, 'node');
  245. var leaf = SubElement(node, 'leaf');
  246. leaf.text = 'ipsum';
  247. assert.equal(root.findtext('node/leaf'), 'ipsum');
  248. assert.equal(root.findtext('no-such-node/leaf'), null);
  249. test.finish();
  250. };
  251. exports['test_remove'] = function(test, assert) {
  252. var root = Element('root');
  253. var node1 = SubElement(root, 'node1');
  254. var node2 = SubElement(root, 'node2');
  255. var node3 = SubElement(root, 'node3');
  256. assert.equal(root.len(), 3);
  257. root.remove(node2);
  258. assert.equal(root.len(), 2);
  259. assert.equal(root.getItem(0).tag, 'node1')
  260. assert.equal(root.getItem(1).tag, 'node3')
  261. test.finish();
  262. };
  263. exports['test_cdata_write'] = function(test, assert) {
  264. var root, etree, xml, values, value, i;
  265. values = [
  266. 'if(0>1) then true;',
  267. '<test1>ponies hello</test1>',
  268. ''
  269. ];
  270. for (i = 0; i < values.length; i++) {
  271. value = values[i];
  272. root = Element('root');
  273. root.append(et.CData(value));
  274. etree = new ElementTree(root);
  275. xml = etree.write({'xml_declaration': false});
  276. assert.equal(xml, sprintf('<root><![CDATA[%s]]></root>', value));
  277. }
  278. test.finish();
  279. };
  280. exports['test_read_bom'] = function(test, assert) {
  281. var file = readFile('bom-xml.xml');
  282. var etree = et.parse(file);
  283. // If parse finished, test was successful
  284. test.finish();
  285. };