config-2.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // --------------------------------------------------------------------------------------------------------------------
  2. //
  3. // config-2.js - tests for node-data2xml
  4. //
  5. // Copyright (c) 2011 Andrew Chilton - http://chilts.org/
  6. // Written by Andrew Chilton <andychilton@gmail.com>
  7. //
  8. // License: http://opensource.org/licenses/MIT
  9. //
  10. // --------------------------------------------------------------------------------------------------------------------
  11. var test = require('tape');
  12. var data2xml = require('../data2xml');
  13. var declaration = '<?xml version="1.0" encoding="utf-8"?>\n';
  14. // --------------------------------------------------------------------------------------------------------------------
  15. var tests = [
  16. {
  17. name : 'one element structure with an xmlns',
  18. element : 'topelement',
  19. data : {
  20. '_attr' : { xmlns : 'http://www.appsattic.com/xml/namespace' },
  21. second : 'value',
  22. 'undefined' : undefined,
  23. 'null' : null,
  24. },
  25. exp1 : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second><undefined></undefined><null/></topelement>',
  26. exp2 : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second><undefined/><null></null></topelement>'
  27. },
  28. {
  29. name : 'element with attributes and value undefined',
  30. element : 'topelement',
  31. data : {
  32. '_attr' : { xmlns : 'http://www.appsattic.com/xml/namespace' },
  33. second : 'value',
  34. 'undefined' : {"_attr": {"key1": "something", "key2": "else"}, "_value": undefined},
  35. 'null' : null,
  36. },
  37. exp1 : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second><undefined key1="something" key2="else"></undefined><null/></topelement>',
  38. exp2 : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second><undefined key1="something" key2="else"/><null></null></topelement>'
  39. },
  40. {
  41. name : 'element with attributes and value null',
  42. element : 'topelement',
  43. data : {
  44. '_attr' : { xmlns : 'http://www.appsattic.com/xml/namespace' },
  45. second : 'value',
  46. 'undefined' : undefined,
  47. 'null' : {"_attr":{"key1":"value", "key2": "value2"}, "_value": null},
  48. },
  49. exp1 : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second><undefined></undefined><null key1="value" key2="value2"/></topelement>',
  50. exp2 : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second><undefined/><null key1="value" key2="value2"></null></topelement>'
  51. },
  52. {
  53. name : 'complex 4 element array with some attributes',
  54. element : 'topelement',
  55. data : { item : [
  56. { '_attr' : { type : 'a' }, '_value' : 'val1' },
  57. { '_attr' : { type : 'b' }, '_value' : 'val2' },
  58. 'val3',
  59. { '_value' : 'val4' },
  60. ] },
  61. exp1 : declaration + '<topelement><item type="a">val1</item><item type="b">val2</item><item>val3</item><item>val4</item></topelement>',
  62. exp2 : declaration + '<topelement><item type="a">val1</item><item type="b">val2</item><item>val3</item><item>val4</item></topelement>'
  63. },
  64. ];
  65. var convert1 = data2xml({ 'undefined' : 'empty', 'null' : 'closed' });
  66. test('1) some simple xml with undefined or null values', function (t) {
  67. tests.forEach(function(test) {
  68. var xml = convert1(test.element, test.data, { attrProp : '@', valProp : '#' });
  69. t.equal(xml, test.exp1, test.name);
  70. });
  71. t.end();
  72. });
  73. var convert2 = data2xml({ 'undefined' : 'closed', 'null' : 'empty' });
  74. test('2) some simple xml with undefined or null values', function (t) {
  75. tests.forEach(function(test) {
  76. var xml = convert2(test.element, test.data, { attrProp : '@', valProp : '#' });
  77. t.equal(xml, test.exp2, test.name);
  78. });
  79. t.end();
  80. });
  81. // --------------------------------------------------------------------------------------------------------------------