|
- import { describe, it, expect } from 'vitest';
- import { serialize, deserialize } from '../../src/types/application/xml';
-
- describe('application/xml', () => {
- describe('serialize', () => {
- it('should serialize a string', async () => {
- const result = await serialize('Hello, World!', { type: 'application/xml' });
- expect(result).toBe('<root type="string">Hello, World!</root>');
- });
-
- it('should serialize a number', async () => {
- const result = await serialize(123, { type: 'application/xml' });
- expect(result).toBe('<root type="number">123</root>');
- });
-
- it('should serialize a boolean', async () => {
- const result = await serialize(true, { type: 'application/xml' });
- expect(result).toBe('<root type="boolean">true</root>');
- });
-
- it('should serialize an array', async () => {
- const result = await serialize([1, 2, 3], { type: 'application/xml' });
- expect(result).toBe('<root array="array"><_0 type="number">1</_0><_1 type="number">2</_1><_2 type="number">3</_2></root>');
- });
-
- it('should serialize an object', async () => {
- const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/xml' });
- expect(result).toBe('<root type="object"><a type="number">1</a><b type="number">2</b><c type="number">3</c></root>');
- });
-
- it('should serialize an object with a custom root element name', async () => {
- const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/xml', rootElementName: 'fsh', });
- expect(result).toBe('<fsh type="object"><a type="number">1</a><b type="number">2</b><c type="number">3</c></fsh>');
- });
-
- it('should serialize an object with indent', async () => {
- const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/xml', indent: 2 });
- expect(result).toBe(`<root type="object">
- <a type="number">1</a>
- <b type="number">2</b>
- <c type="number">3</c>
- </root>`);
- });
- });
-
- describe('deserialize', () => {
- it('should deserialize a string', async () => {
- const result = await deserialize<string>('<root type="string">Hello, World!</root>', { type: 'application/xml' });
- expect(result).toBe('Hello, World!');
- });
-
- it('should deserialize a number', async () => {
- const result = await deserialize<number>('<root type="number">123</root>', { type: 'application/xml' });
- expect(result).toBe(123);
- });
-
- it('should deserialize a boolean', async () => {
- const result = await deserialize<boolean>('<root type="boolean">true</root>', { type: 'application/xml' });
- expect(result).toBe(true);
- });
-
- it('should deserialize an array', async () => {
- 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' });
- expect(result).toEqual([1, 2, 3]);
- });
-
- it('should deserialize an object', async () => {
- const result = await deserialize<{ a: number, b: number, c: number }>(
- '<root type="object"><a type="number">1</a><b type="number">2</b><c type="number">3</c></root>',
- { type: 'application/xml' },
- );
- expect(result).toEqual({ a: 1, b: 2, c: 3 });
- });
- });
- });
|