电速宝智配引擎
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import test from 'ava';
  2. import xml from '../lib/xml';
  3. test('no elements', t => {
  4. t.is(xml(), '');
  5. t.is(xml([]), '');
  6. t.is(xml('test'), 'test');
  7. t.is(xml('scotch & whisky'), 'scotch & whisky');
  8. t.is(xml('bob\'s escape character'), 'bob's escape character');
  9. });
  10. test('simple options', t => {
  11. t.is(xml([{a: {}}]), '<a/>');
  12. t.is(xml([{a: null}]), '<a/>');
  13. t.is(xml([{a: []}]), '<a></a>');
  14. t.is(xml([{a: -1}]), '<a>-1</a>');
  15. t.is(xml([{a: false}]), '<a>false</a>');
  16. t.is(xml([{a: 'test'}]), '<a>test</a>');
  17. t.is(xml({a: {}}), '<a/>');
  18. t.is(xml({a: null}), '<a/>');
  19. t.is(xml({a: []}), '<a></a>');
  20. t.is(xml({a: -1}), '<a>-1</a>');
  21. t.is(xml({a: false}), '<a>false</a>');
  22. t.is(xml({a: 'test'}), '<a>test</a>');
  23. t.is(xml([{a: 'test'}, {b: 123}, {c: -0.5}]), '<a>test</a><b>123</b><c>-0.5</c>');
  24. });
  25. test('deeply nested objects', t => {
  26. t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}]), '<a><b><c>1</c><c>2</c><c>3</c></b></a>');
  27. });
  28. test('indents property', t => {
  29. t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], true), '<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
  30. t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], ' '), '<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
  31. t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], '\t'), '<a>\n\t<b>\n\t\t<c>1</c>\n\t\t<c>2</c>\n\t\t<c>3</c>\n\t</b>\n</a>');
  32. t.is(xml({guid: [{_attr: {premalink: true}}, 'content']}, true), '<guid premalink="true">content</guid>');
  33. });
  34. test('supports xml attributes', t => {
  35. t.is(xml([{b: {_attr: {}}}]), '<b/>');
  36. t.is(xml([{
  37. a: {
  38. _attr: {
  39. attribute1: 'some value',
  40. attribute2: 12345
  41. }
  42. }
  43. }]), '<a attribute1="some value" attribute2="12345"/>');
  44. t.is(xml([{
  45. a: [{
  46. _attr: {
  47. attribute1: 'some value',
  48. attribute2: 12345
  49. }
  50. }]
  51. }]), '<a attribute1="some value" attribute2="12345"></a>');
  52. t.is(xml([{
  53. a: [{
  54. _attr: {
  55. attribute1: 'some value',
  56. attribute2: 12345
  57. }
  58. }, 'content']
  59. }]), '<a attribute1="some value" attribute2="12345">content</a>');
  60. });
  61. test('supports cdata', t => {
  62. t.is(xml([{a: {_cdata: 'This is some <strong>CDATA</strong>'}}]), '<a><![CDATA[This is some <strong>CDATA</strong>]]></a>');
  63. t.is(xml([{
  64. a: {
  65. _attr: {attribute1: 'some value', attribute2: 12345},
  66. _cdata: 'This is some <strong>CDATA</strong>'
  67. }
  68. }]), '<a attribute1="some value" attribute2="12345"><![CDATA[This is some <strong>CDATA</strong>]]></a>');
  69. t.is(xml([{a: {_cdata: 'This is some <strong>CDATA</strong> with ]]> and then again ]]>'}}]), '<a><![CDATA[This is some <strong>CDATA</strong> with ]]]]><![CDATA[> and then again ]]]]><![CDATA[>]]></a>');
  70. });
  71. test('supports encoding', t => {
  72. t.is(xml([{
  73. a: [{
  74. _attr: {
  75. anglebrackets: 'this is <strong>strong</strong>',
  76. url: 'http://google.com?s=opower&y=fun'
  77. }
  78. }, 'text']
  79. }]), '<a anglebrackets="this is &lt;strong&gt;strong&lt;/strong&gt;" url="http://google.com?s=opower&amp;y=fun">text</a>');
  80. });
  81. test('supports stream interface', t => {
  82. const elem = xml.element({_attr: {decade: '80s', locale: 'US'}});
  83. const xmlStream = xml({toys: elem}, {stream: true});
  84. const results = ['<toys decade="80s" locale="US">', '<toy>Transformers</toy>', '<toy><name>He-man</name></toy>', '<toy>GI Joe</toy>', '</toys>'];
  85. elem.push({toy: 'Transformers'});
  86. elem.push({toy: [{name: 'He-man'}]});
  87. elem.push({toy: 'GI Joe'});
  88. elem.close();
  89. xmlStream.on('data', stanza => {
  90. t.is(stanza, results.shift());
  91. });
  92. return new Promise( (resolve, reject) => {
  93. xmlStream.on('close', () => {
  94. t.same(results, []);
  95. resolve();
  96. });
  97. xmlStream.on('error', reject);
  98. });
  99. });
  100. test('streams end properly', t => {
  101. const elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
  102. const xmlStream = xml({ toys: elem }, { stream: true });
  103. let gotData;
  104. t.plan(7);
  105. elem.push({ toy: 'Transformers' });
  106. elem.push({ toy: 'GI Joe' });
  107. elem.push({ toy: [{name:'He-man'}] });
  108. elem.close();
  109. xmlStream.on('data', data => {
  110. t.ok(data);
  111. gotData = true;
  112. });
  113. xmlStream.on('end', () => {
  114. t.ok(gotData);
  115. });
  116. return new Promise( (resolve, reject) => {
  117. xmlStream.on('close', () => {
  118. t.ok(gotData);
  119. resolve();
  120. });
  121. xmlStream.on('error', reject);
  122. });
  123. });
  124. test('xml declaration options', t => {
  125. t.is(xml([{a: 'test'}], {declaration: true}), '<?xml version="1.0" encoding="UTF-8"?><a>test</a>');
  126. t.is(xml([{a: 'test'}], {declaration: {encoding: 'foo'}}), '<?xml version="1.0" encoding="foo"?><a>test</a>');
  127. t.is(xml([{a: 'test'}], {declaration: {standalone: 'yes'}}), '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a>test</a>');
  128. t.is(xml([{a: 'test'}], {declaration: false}), '<a>test</a>');
  129. t.is(xml([{a: 'test'}], {declaration: true, indent: '\n'}), '<?xml version="1.0" encoding="UTF-8"?>\n<a>test</a>');
  130. t.is(xml([{a: 'test'}], {}), '<a>test</a>');
  131. });