reader.tests.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var assert = require("assert");
  2. var xmlreader = require("../../lib/xml/reader");
  3. var test = require("../test")(module);
  4. test('should read self-closing element', function() {
  5. return xmlreader.readString("<body/>").then(function(result) {
  6. assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result);
  7. });
  8. });
  9. test('should read empty element with separate closing tag', function() {
  10. return xmlreader.readString("<body></body>").then(function(result) {
  11. assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result);
  12. });
  13. });
  14. test('should read attributes of tags', function() {
  15. return xmlreader.readString('<body name="bob"/>').then(function(result) {
  16. assert.deepEqual({name: "bob"}, result.attributes);
  17. });
  18. });
  19. test('can read text element', function() {
  20. return xmlreader.readString('<body>Hello!</body>').then(function(result) {
  21. assert.deepEqual({type: "text", value: "Hello!"}, result.children[0]);
  22. });
  23. });
  24. test('should read element with children', function() {
  25. return xmlreader.readString("<body><a/><b/></body>").then(function(root) {
  26. assert.equal(2, root.children.length);
  27. assert.equal("a", root.children[0].name);
  28. assert.equal("b", root.children[1].name);
  29. });
  30. });
  31. test('unmapped namespaces URIs are included in braces as prefix', function() {
  32. return xmlreader.readString('<w:body xmlns:w="word"/>').then(function(result) {
  33. assert.deepEqual(result.name, "{word}body");
  34. });
  35. });
  36. test('mapped namespaces URIs are translated using map', function() {
  37. var namespaceMap = {
  38. "word": "x"
  39. };
  40. return xmlreader.readString('<w:body xmlns:w="word"/>', namespaceMap).then(function(result) {
  41. assert.deepEqual(result.name, "x:body");
  42. });
  43. });
  44. test('namespace of attributes is mapped to prefix', function() {
  45. var namespaceMap = {
  46. "word": "x"
  47. };
  48. var xmlString = '<w:body xmlns:w="word" w:val="Hello!"/>';
  49. return xmlreader.readString(xmlString, namespaceMap).then(function(result) {
  50. assert.deepEqual(result.attributes["x:val"], "Hello!");
  51. });
  52. });
  53. test('can find first element with name', function() {
  54. return xmlreader.readString('<body><a/><b index="1"/><b index="2"/></body>').then(function(result) {
  55. var first = result.first("b");
  56. assert.equal("1", first.attributes.index);
  57. });
  58. });
  59. test('whitespace between xml declaration and root tag is ignored', function() {
  60. return xmlreader.readString('<?xml version="1.0" ?>\n<body/>').then(function(result) {
  61. assert.deepEqual("body", result.name);
  62. });
  63. });
  64. test('error if XML is badly formed', function() {
  65. return xmlreader.readString("<bo").then(function(result) {
  66. throw new Error("Expected failure");
  67. }, function(error) {
  68. assert.ok(error);
  69. return 1;
  70. });
  71. });