1
- import { toYaml } from '../src/converters/toYaml' ;
1
+ import toYaml from '../src/converters/toYaml' ;
2
2
3
3
describe ( 'toYaml' , ( ) => {
4
4
it ( 'should convert a JSON object to YAML format' , ( ) => {
@@ -8,27 +8,26 @@ describe('toYaml', () => {
8
8
features : [ 'JSON to YAML Conversion' ] ,
9
9
} ;
10
10
11
- const expectedYaml = `name: JsonWeaver\nversion: 1.0.3\nfeatures:\n - JSON to YAML Conversion\n` ;
11
+ const expectedYaml = `name: JsonWeaver
12
+ version: 1.0.3
13
+ features:
14
+ - JSON to YAML Conversion
15
+ ` ;
12
16
13
17
const yamlOutput = toYaml ( jsonInput ) ;
18
+
14
19
expect ( yamlOutput ) . toBe ( expectedYaml ) ;
15
20
} ) ;
16
21
17
22
it ( 'should handle empty JSON objects' , ( ) => {
18
- const jsonInput = { } ;
19
- const expectedYaml = '{}\n' ;
23
+ const yamlOutput = toYaml ( { } ) ;
20
24
21
- const yamlOutput = toYaml ( jsonInput ) ;
22
- expect ( yamlOutput ) . toBe ( expectedYaml ) ;
25
+ expect ( yamlOutput ) . toBe ( '{}\n' ) ;
23
26
} ) ;
24
27
25
28
it ( 'should throw an error for invalid inputs' , ( ) => {
26
- expect ( ( ) => toYaml ( null as unknown as object ) ) . toThrow (
27
- 'Conversion error: Input must be a valid JSON string or object.'
28
- ) ;
29
- expect ( ( ) => toYaml ( undefined as unknown as object ) ) . toThrow (
30
- 'Conversion error: Input must be a valid JSON string or object.'
31
- ) ;
29
+ expect ( ( ) => toYaml ( null as unknown as object ) ) . toThrow ( 'Conversion error: Input must be a valid JSON string or object.' ) ;
30
+ expect ( ( ) => toYaml ( undefined as unknown as object ) ) . toThrow ( 'Conversion error: Input must be a valid JSON string or object.' ) ;
32
31
} ) ;
33
32
34
33
it ( 'should correctly handle nested JSON objects' , ( ) => {
@@ -42,9 +41,15 @@ describe('toYaml', () => {
42
41
} ,
43
42
} ;
44
43
45
- const expectedYaml = `person:\n name: John\n address:\n city: New York\n zip: '10001'\n` ;
44
+ const expectedYaml = `person:
45
+ name: John
46
+ address:
47
+ city: New York
48
+ zip: '10001'
49
+ ` ;
46
50
47
51
const yamlOutput = toYaml ( jsonInput ) ;
52
+
48
53
expect ( yamlOutput ) . toBe ( expectedYaml ) ;
49
54
} ) ;
50
55
@@ -53,22 +58,31 @@ describe('toYaml', () => {
53
58
items : [ 'item1' , 'item2' , 'item3' ] ,
54
59
} ;
55
60
56
- const expectedYaml = `items:\n - item1\n - item2\n - item3\n` ;
61
+ const expectedYaml = `items:
62
+ - item1
63
+ - item2
64
+ - item3
65
+ ` ;
57
66
58
67
const yamlOutput = toYaml ( jsonInput ) ;
68
+
59
69
expect ( yamlOutput ) . toBe ( expectedYaml ) ;
60
70
} ) ;
61
71
62
- it ( 'should handle numbers, booleans, and null' , ( ) => {
72
+ it ( 'should handle numbers, booleans, and null values ' , ( ) => {
63
73
const jsonInput = {
64
74
number : 42 ,
65
75
boolean : true ,
66
76
nullValue : null ,
67
77
} ;
68
78
69
- const expectedYaml = `number: 42\nboolean: true\nnullValue: null\n` ;
79
+ const expectedYaml = `number: 42
80
+ boolean: true
81
+ nullValue: null
82
+ ` ;
70
83
71
84
const yamlOutput = toYaml ( jsonInput ) ;
85
+
72
86
expect ( yamlOutput ) . toBe ( expectedYaml ) ;
73
87
} ) ;
74
88
@@ -78,38 +92,46 @@ describe('toYaml', () => {
78
92
emoji : '😊' ,
79
93
} ;
80
94
81
- const expectedYaml = `text: |-\n Line1\n Line2\nemoji: 😊\n` ;
95
+ const expectedYaml = `text: |-
96
+ Line1
97
+ Line2
98
+ emoji: 😊
99
+ ` ;
82
100
83
101
const yamlOutput = toYaml ( jsonInput ) ;
102
+
84
103
expect ( yamlOutput ) . toBe ( expectedYaml ) ;
85
104
} ) ;
86
105
87
-
88
106
it ( 'should handle large JSON objects' , ( ) => {
89
107
const jsonInput = Array . from ( { length : 1000 } , ( _ , i ) => ( { index : i } ) ) ;
90
108
91
109
expect ( ( ) => toYaml ( jsonInput ) ) . not . toThrow ( ) ;
92
110
} ) ;
93
111
94
- it ( 'should produce consistent YAML output for YAML-like input ' , ( ) => {
112
+ it ( 'should produce consistent YAML output for YAML-like strings ' , ( ) => {
95
113
const jsonInput = {
96
114
yamlLike : `key: value` ,
97
115
} ;
98
116
99
- const expectedYaml = `yamlLike: 'key: value'\n` ;
117
+ const expectedYaml = `yamlLike: 'key: value'
118
+ ` ;
100
119
101
120
const yamlOutput = toYaml ( jsonInput ) ;
121
+
102
122
expect ( yamlOutput ) . toBe ( expectedYaml ) ;
103
123
} ) ;
104
124
105
- it ( 'should handle numeric keys in objects' , ( ) => {
125
+ it ( 'should handle numeric keys in JSON objects' , ( ) => {
106
126
const jsonInput = {
107
127
123 : 'value' ,
108
128
} ;
109
129
110
- const expectedYaml = `'123': value\n` ;
130
+ const expectedYaml = `'123': value
131
+ ` ;
111
132
112
133
const yamlOutput = toYaml ( jsonInput ) ;
134
+
113
135
expect ( yamlOutput ) . toBe ( expectedYaml ) ;
114
136
} ) ;
115
137
} ) ;
0 commit comments