Serialize and deserialize data.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

76 рядки
3.1 KiB

  1. import { describe, it, expect } from 'vitest';
  2. import { serialize, deserialize } from '../../src/types/application/xml';
  3. describe('application/xml', () => {
  4. describe('serialize', () => {
  5. it('should serialize a string', () => {
  6. const result = serialize('Hello, World!', { type: 'application/xml' });
  7. expect(result).toBe('<root type="string">Hello, World!</root>');
  8. });
  9. it('should serialize a number', () => {
  10. const result = serialize(123, { type: 'application/xml' });
  11. expect(result).toBe('<root type="number">123</root>');
  12. });
  13. it('should serialize a boolean', () => {
  14. const result = serialize(true, { type: 'application/xml' });
  15. expect(result).toBe('<root type="boolean">true</root>');
  16. });
  17. it('should serialize an array', () => {
  18. const result = serialize([1, 2, 3], { type: 'application/xml' });
  19. expect(result).toBe('<root array="array"><_0 type="number">1</_0><_1 type="number">2</_1><_2 type="number">3</_2></root>');
  20. });
  21. it('should serialize an object', () => {
  22. const result = serialize({ a: 1, b: 2, c: 3 }, { type: 'application/xml' });
  23. expect(result).toBe('<root type="object"><a type="number">1</a><b type="number">2</b><c type="number">3</c></root>');
  24. });
  25. it('should serialize an object with a custom root element name', () => {
  26. const result = serialize({ a: 1, b: 2, c: 3 }, { type: 'application/xml', rootElementName: 'fsh', });
  27. expect(result).toBe('<fsh type="object"><a type="number">1</a><b type="number">2</b><c type="number">3</c></fsh>');
  28. });
  29. it('should serialize an object with indent', () => {
  30. const result = serialize({ a: 1, b: 2, c: 3 }, { type: 'application/xml', indent: 2 });
  31. expect(result).toBe(`<root type="object">
  32. <a type="number">1</a>
  33. <b type="number">2</b>
  34. <c type="number">3</c>
  35. </root>`);
  36. });
  37. });
  38. describe('deserialize', () => {
  39. it('should deserialize a string', () => {
  40. const result = deserialize<string>('<root type="string">Hello, World!</root>', { type: 'application/xml' });
  41. expect(result).toBe('Hello, World!');
  42. });
  43. it('should deserialize a number', () => {
  44. const result = deserialize<number>('<root type="number">123</root>', { type: 'application/xml' });
  45. expect(result).toBe(123);
  46. });
  47. it('should deserialize a boolean', () => {
  48. const result = deserialize<boolean>('<root type="boolean">true</root>', { type: 'application/xml' });
  49. expect(result).toBe(true);
  50. });
  51. it('should deserialize an array', () => {
  52. const result = deserialize<number[]>('<root array="array"><_0 type="number">1</_0><_1 type="number">2</_1><_2 type="number">3</_2></root>', { type: 'application/xml' });
  53. expect(result).toEqual([1, 2, 3]);
  54. });
  55. it('should deserialize an object', () => {
  56. const result = deserialize<{ a: number, b: number, c: number }>(
  57. '<root type="object"><a type="number">1</a><b type="number">2</b><c type="number">3</c></root>',
  58. { type: 'application/xml' },
  59. );
  60. expect(result).toEqual({ a: 1, b: 2, c: 3 });
  61. });
  62. });
  63. });