xml.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Utilities: A classic collection of JavaScript utilities
  3. * Copyright 2112 Matthew Eernisse ([email protected])
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. var XML = require('../lib/xml').XML
  19. , assert = require('assert')
  20. , obj
  21. , xml
  22. , res
  23. , serialize
  24. , tests;
  25. serialize = function (o) {
  26. return XML.stringify(o, {whitespace: false});
  27. };
  28. tests = {
  29. 'test serialized object': function () {
  30. obj = {foo: 'bar'};
  31. xml = serialize(obj);
  32. res = '<?xml version="1.0" encoding="UTF-8"?><object><foo>bar</foo></object>';
  33. assert.equal(res, xml);
  34. }
  35. , 'test array of numbers': function () {
  36. obj = [1, 2, 3];
  37. xml = serialize(obj);
  38. res = '<?xml version="1.0" encoding="UTF-8"?><numbers type="array"><number>1</number><number>2</number><number>3</number></numbers>';
  39. assert.equal(res, xml);
  40. }
  41. , 'test array of strings': function () {
  42. obj = ['foo', 'bar'];
  43. xml = serialize(obj);
  44. res = '<?xml version="1.0" encoding="UTF-8"?><strings type="array"><string>foo</string><string>bar</string></strings>';
  45. assert.equal(res, xml);
  46. }
  47. , 'test array of mixed datatypes': function () {
  48. obj = ['foo', 1];
  49. xml = serialize(obj);
  50. res = '<?xml version="1.0" encoding="UTF-8"?><records type="array"><record>foo</record><record>1</record></records>';
  51. assert.equal(res, xml);
  52. }
  53. , 'test array property of an object': function () {
  54. obj = {foo: ['bar', 'baz']};
  55. xml = serialize(obj);
  56. res = '<?xml version="1.0" encoding="UTF-8"?><object><foo type="array"><foo>bar</foo><foo>baz</foo></foo></object>';
  57. assert.equal(res, xml);
  58. }
  59. , 'test setIndentLevel for xml': function () {
  60. var data = XML.setIndentLevel(5)
  61. , actual = 5;
  62. assert.equal(actual, data)
  63. }
  64. , 'test stringify with object for xml': function () {
  65. var data = XML.stringify({user: 'name'})
  66. , actual = '<?xml version="1.0" encoding="UTF-8"?>\n<object>\n <user>name</user>\n</object>\n';
  67. assert.equal(actual, data)
  68. }
  69. , 'test stringify with array for xml': function () {
  70. var data = XML.stringify(['user'])
  71. , actual = '<?xml version="1.0" encoding="UTF-8"?>\n<strings type="array">\n\
  72. <string>user</string>\n</strings>';
  73. assert.equal(actual, data)
  74. }
  75. , 'test stringify with object and no whitespace for xml': function () {
  76. var data = XML.stringify({user: 'name'}, {whitespace: false})
  77. , actual = '<?xml version="1.0" encoding="UTF-8"?><object><user>name</user></object>';
  78. assert.equal(actual, data)
  79. }
  80. , 'test stringify with object and name for xml': function () {
  81. var data = XML.stringify({user: 'name'}, {name: 'omg'})
  82. , actual = '<?xml version="1.0" encoding="UTF-8"?>\n<omg>\n<user>name</user>\n</omg>\n';
  83. assert.equal(actual, data)
  84. }
  85. , 'test stringify with object and fragment for xml': function () {
  86. var data = XML.stringify({user: 'name'}, {fragment: true})
  87. , actual = '<object>\n<user>name</user>\n</object>\n';
  88. assert.equal(actual, data)
  89. }
  90. , 'test stringify with object for xml': function () {
  91. var data = XML.stringify({user: 'name'}, {level: 1})
  92. , actual = '<?xml version="1.0" encoding="UTF-8"?>\n <user>name</user>\n';
  93. assert.equal(actual, data)
  94. }
  95. , 'test stringify with array and no arrayRoot for xml': function () {
  96. var data = XML.stringify(['user'], {arrayRoot: false})
  97. , actual = '<?xml version="1.0" encoding="UTF-8"?>\n<strings type="array">\n\
  98. <string>user</string>\n</strings>';
  99. assert.equal(actual, data)
  100. }
  101. };
  102. module.exports = tests;