Serialize and deserialize data.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

xml.test.ts 3.1 KiB

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', () => {
  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. });