Serialize and deserialize data.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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', async () => {
  6. const result = await serialize('Hello, World!', { type: 'application/xml' });
  7. expect(result).toBe('<root type="string">Hello, World!</root>');
  8. });
  9. it('should serialize a number', async () => {
  10. const result = await serialize(123, { type: 'application/xml' });
  11. expect(result).toBe('<root type="number">123</root>');
  12. });
  13. it('should serialize a boolean', async () => {
  14. const result = await serialize(true, { type: 'application/xml' });
  15. expect(result).toBe('<root type="boolean">true</root>');
  16. });
  17. it('should serialize an array', async () => {
  18. const result = await 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', async () => {
  22. const result = await 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', async () => {
  26. const result = await 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', async () => {
  30. const result = await 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', async () => {
  40. const result = await deserialize<string>('<root type="string">Hello, World!</root>', { type: 'application/xml' });
  41. expect(result).toBe('Hello, World!');
  42. });
  43. it('should deserialize a number', async () => {
  44. const result = await deserialize<number>('<root type="number">123</root>', { type: 'application/xml' });
  45. expect(result).toBe(123);
  46. });
  47. it('should deserialize a boolean', async () => {
  48. const result = await deserialize<boolean>('<root type="boolean">true</root>', { type: 'application/xml' });
  49. expect(result).toBe(true);
  50. });
  51. it('should deserialize an array', async () => {
  52. const result = await 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', async () => {
  56. const result = await 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. });