config.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // --------------------------------------------------------------------------------------------------------------------
  2. //
  3. // config.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. test('some simple xml with custom attributes, values and cdata', function (t) {
  16. var convert = data2xml({ attrProp : '@', valProp : '#', cdataProp : '%' });
  17. var tests = [{
  18. name : 'one element structure with an xmlns',
  19. element : 'topelement',
  20. data : { '@' : { xmlns : 'http://www.appsattic.com/xml/namespace' }, second : 'value' },
  21. exp : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second></topelement>'
  22. }, {
  23. name : 'complex 4 element array with some attributes',
  24. element : 'topelement',
  25. data : { item : [
  26. { '@' : { type : 'a' }, '#' : 'val1' },
  27. { '@' : { type : 'b' }, '#' : 'val2' },
  28. 'val3',
  29. { '#' : 'val4' },
  30. ] },
  31. exp : declaration + '<topelement><item type="a">val1</item><item type="b">val2</item><item>val3</item><item>val4</item></topelement>'
  32. }, {
  33. name : 'simple element with cdata set',
  34. element : 'topelement',
  35. data : { '%' : 'Some text with <em>unescaped</em> HTML data.' },
  36. exp : declaration + '<topelement><![CDATA[Some text with <em>unescaped</em> HTML data.]]></topelement>'
  37. }];
  38. tests.forEach(function(test) {
  39. var xml = convert(test.element, test.data);
  40. t.equal(xml, test.exp, test.name);
  41. });
  42. t.end();
  43. });
  44. test('it\'s possible to omit the XML declaration', function (t) {
  45. var convert = data2xml({xmlDecl: false});
  46. t.equal(
  47. convert('moo', {foo: 'bar', baz: 42}),
  48. '<moo><foo>bar</foo><baz>42</baz></moo>',
  49. 'must not be declared as XML'
  50. );
  51. t.end();
  52. });
  53. // --------------------------------------------------------------------------------------------------------------------