Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

xml.test.ts 6.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  1. import dedent from 'dedent';
  2. import { containsXml, validateXml } from '../../src/assertions/xml';
  3. describe('validateXml', () => {
  4. it('should validate a simple valid XML string', () => {
  5. expect(validateXml('<root><child>Content</child></root>')).toEqual({
  6. isValid: true,
  7. reason: 'XML is valid and contains all required elements',
  8. });
  9. });
  10. it('should invalidate a malformed XML string', () => {
  11. expect(validateXml('<root><child>Content</child></root')).toEqual({
  12. isValid: false,
  13. reason: expect.stringContaining('XML parsing failed'),
  14. });
  15. });
  16. it('should validate XML with attributes', () => {
  17. expect(validateXml('<root><child id="1">Content</child></root>')).toEqual({
  18. isValid: true,
  19. reason: 'XML is valid and contains all required elements',
  20. });
  21. });
  22. it('should validate XML with namespaces', () => {
  23. expect(
  24. validateXml('<root xmlns:ns="http://example.com"><ns:child>Content</ns:child></root>'),
  25. ).toEqual({
  26. isValid: true,
  27. reason: 'XML is valid and contains all required elements',
  28. });
  29. });
  30. it('should validate when all required elements are present', () => {
  31. expect(
  32. validateXml(
  33. '<analysis><classification>T-shirt</classification><color>Red</color></analysis>',
  34. ['analysis.classification', 'analysis.color'],
  35. ),
  36. ).toEqual({
  37. isValid: true,
  38. reason: 'XML is valid and contains all required elements',
  39. });
  40. });
  41. it('should invalidate when a required element is missing', () => {
  42. expect(
  43. validateXml('<analysis><classification>T-shirt</classification></analysis>', [
  44. 'analysis.classification',
  45. 'analysis.color',
  46. ]),
  47. ).toEqual({
  48. isValid: false,
  49. reason: 'XML is missing required elements: analysis.color',
  50. });
  51. });
  52. it('should validate nested elements correctly', () => {
  53. expect(
  54. validateXml('<root><parent><child><grandchild>Content</grandchild></child></parent></root>', [
  55. 'root.parent.child.grandchild',
  56. ]),
  57. ).toEqual({
  58. isValid: true,
  59. reason: 'XML is valid and contains all required elements',
  60. });
  61. });
  62. it('should invalidate when a nested required element is missing', () => {
  63. expect(
  64. validateXml('<root><parent><child></child></parent></root>', [
  65. 'root.parent.child.grandchild',
  66. ]),
  67. ).toEqual({
  68. isValid: false,
  69. reason: 'XML is missing required elements: root.parent.child.grandchild',
  70. });
  71. });
  72. it('should handle empty elements correctly', () => {
  73. expect(
  74. validateXml('<root><emptyChild></emptyChild><nonEmptyChild>Content</nonEmptyChild></root>', [
  75. 'root.emptyChild',
  76. 'root.nonEmptyChild',
  77. ]),
  78. ).toEqual({
  79. isValid: true,
  80. reason: 'XML is valid and contains all required elements',
  81. });
  82. });
  83. it('should validate XML with multiple siblings', () => {
  84. expect(
  85. validateXml('<root><child>Content1</child><child>Content2</child></root>', ['root.child']),
  86. ).toEqual({
  87. isValid: true,
  88. reason: 'XML is valid and contains all required elements',
  89. });
  90. });
  91. it('should handle XML with CDATA sections', () => {
  92. expect(
  93. validateXml('<root><child><![CDATA[<p>This is CDATA content</p>]]></child></root>', [
  94. 'root.child',
  95. ]),
  96. ).toEqual({
  97. isValid: true,
  98. reason: 'XML is valid and contains all required elements',
  99. });
  100. });
  101. it('should validate XML with processing instructions', () => {
  102. const xml =
  103. '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="style.xsl"?><root><child>Content</child></root>';
  104. expect(validateXml(xml, ['root.child'])).toEqual({
  105. isValid: true,
  106. reason: 'XML is valid and contains all required elements',
  107. });
  108. });
  109. it('should handle XML with comments', () => {
  110. expect(
  111. validateXml('<root><!-- This is a comment --><child>Content</child></root>', ['root.child']),
  112. ).toEqual({
  113. isValid: true,
  114. reason: 'XML is valid and contains all required elements',
  115. });
  116. });
  117. it('should validate the example XML structure', () => {
  118. const xml = dedent`
  119. <analysis>
  120. <classification>T-shirt/top</classification>
  121. <color>White with black print</color>
  122. <features>Large circular graphic design on the front, resembling a smiley face or emoji</features>
  123. <style>Modern, casual streetwear</style>
  124. <confidence>9</confidence>
  125. <reasoning>The image clearly shows a short-sleeved garment with a round neckline, which is characteristic of a T-shirt. The large circular graphic on the front is distinctive and appears to be a stylized smiley face or emoji design, which is popular in contemporary casual fashion. The stark contrast between the white fabric and black print is very clear, leaving little room for misinterpretation. The style is unmistakably modern and aligned with current trends in graphic tees. My confidence is high (9) because all elements of the image are clear and consistent with a typical graphic T-shirt design.</reasoning>
  126. </analysis>
  127. `;
  128. expect(
  129. validateXml(xml, [
  130. 'analysis.classification',
  131. 'analysis.color',
  132. 'analysis.features',
  133. 'analysis.style',
  134. 'analysis.confidence',
  135. 'analysis.reasoning',
  136. ]),
  137. ).toEqual({
  138. isValid: true,
  139. reason: 'XML is valid and contains all required elements',
  140. });
  141. });
  142. });
  143. describe('containsXml', () => {
  144. it('should return true when valid XML is present', () => {
  145. const input = 'Some text <root><child>Content</child></root> more text';
  146. const result = containsXml(input);
  147. expect(result.isValid).toBe(true);
  148. });
  149. it('should return false when no XML is present', () => {
  150. const input = 'This is just plain text';
  151. expect(containsXml(input)).toEqual({
  152. isValid: false,
  153. reason: 'No XML content found in the output',
  154. });
  155. });
  156. it('should validate required elements', () => {
  157. const input = 'Text <root><child>Content</child></root> more';
  158. const result = containsXml(input, ['root.child']);
  159. expect(result.isValid).toBe(true);
  160. });
  161. it('should return false when required elements are missing', () => {
  162. const input = 'Text <root><child>Content</child></root> more';
  163. expect(containsXml(input, ['root.missing'])).toEqual({
  164. isValid: false,
  165. reason: 'No valid XML content found matching the requirements',
  166. });
  167. });
  168. it('should handle multiple XML fragments', () => {
  169. const input = '<root1>Content</root1> text <root2><child>More</child></root2>';
  170. const result = containsXml(input, ['root2.child']);
  171. expect(result.isValid).toBe(true);
  172. });
  173. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...