@@ -13,7 +13,6 @@ class OutputFormatterTest {
1313
1414 @Test
1515 fun test (){
16- assertTrue(OutputFormatter .getFormatters()?.size == 1 )
1716 val body = """
1817 {
1918 "authorId": "VZyJz8z_Eu2",
@@ -28,7 +27,6 @@ class OutputFormatterTest {
2827
2928 @Test
3029 fun testMismatched (){
31- assertTrue(OutputFormatter .getFormatters()?.size == 1 )
3230 val body = """
3331
3432 "authorId": "VZyJz8z_Eu2",
@@ -43,7 +41,6 @@ class OutputFormatterTest {
4341 }
4442 @Test
4543 fun testEscapes (){
46- assertTrue(OutputFormatter .getFormatters()?.size == 1 )
4744 val body = """
4845 {
4946 "name":"T\""
@@ -56,15 +53,13 @@ class OutputFormatterTest {
5653
5754 @Test
5855 fun testEscapes2 (){
59- assertTrue(OutputFormatter .getFormatters()?.size == 1 )
6056
6157 val string = """ {"id":"9d8UV_=e1T0eWTlc", "value":"93${' $' } v98g"}"""
6258 OutputFormatter .JSON_FORMATTER .getFormatted(string)
6359 }
6460
6561 @Test
6662 fun testEscapes3 (){
67- assertTrue(OutputFormatter .getFormatters()?.size == 1 )
6863
6964 val string = """
7065 {"id":"19r\"l_", "value":""}
@@ -74,15 +69,13 @@ class OutputFormatterTest {
7469
7570 @Test
7671 fun testEscapes4 (){
77- assertTrue(OutputFormatter .getFormatters()?.size == 1 )
7872 val testGene = StringGene (" QuoteGene" , " Test For the quotes${' "' } escape" )
7973
8074 OutputFormatter .JSON_FORMATTER .getFormatted(testGene.getValueAsPrintableString(mode = GeneUtils .EscapeMode .JSON , targetFormat = OutputFormat .KOTLIN_JUNIT_5 ))
8175 }
8276
8377 @Test
8478 fun testEscapes6 (){
85- assertTrue(OutputFormatter .getFormatters()?.size == 1 )
8679
8780 val string = """
8881 {"id":"19r\\l_"}
@@ -92,7 +85,6 @@ class OutputFormatterTest {
9285
9386 @Test
9487 fun testEscapes7 (){
95- assertTrue(OutputFormatter .getFormatters()?.size == 1 )
9688
9789 val string = """
9890 {"id":"Ot${' $' } Ag", "value":"Q"}
@@ -102,7 +94,6 @@ class OutputFormatterTest {
10294
10395 @Test
10496 fun testEscapes8 (){
105- assertTrue(OutputFormatter .getFormatters()?.size == 1 )
10697 val testGene = StringGene (" DollarGene" , " Test For the dollar${' $' } escape" )
10798
10899 OutputFormatter .JSON_FORMATTER .getFormatted(testGene.getValueAsPrintableString(mode = GeneUtils .EscapeMode .JSON , targetFormat = OutputFormat .KOTLIN_JUNIT_5 ))
@@ -165,5 +156,246 @@ class OutputFormatterTest {
165156 assertTrue(isValid)
166157 }
167158
159+ @Test
160+ fun testXml (){
161+ assertTrue(OutputFormatter .getFormatters()?.size == 2 )
162+ val body = """
163+ <root>
164+ <authorId>VZyJz8z_Eu2</authorId>
165+ <creationTime>1921-3-13T10:18:56.000Z</creationTime>
166+ <newsId>L</newsId>
167+ </root>
168+ """ .trimIndent()
169+
170+ // should throw no exception
171+ OutputFormatter .XML_FORMATTER .getFormatted(body)
172+ }
173+
174+ @Test
175+ fun testXmlMismatched (){
176+ assertTrue(OutputFormatter .getFormatters()?.size == 2 )
177+ val body = """
178+ <root>
179+ <authorId>VZyJz8z_Eu2</authorId>
180+ <creationTime>1921-3-13T10:18:56.000Z</creationTime>
181+ <newsId>L</newsId>
182+ """ .trimIndent()
183+
184+ assertThrows<Exception > {
185+ OutputFormatter .XML_FORMATTER .getFormatted(body)
186+ }
187+ }
188+
189+ @Test
190+ fun testValidXml () {
191+ val xml = " <root><name>Hello World</name></root>"
192+ val isValid = OutputFormatter .XML_FORMATTER .isValid(xml)
193+ assertTrue(isValid)
194+ }
195+
196+ @Test
197+ fun testInvalidXml () {
198+ val xml = " <root><name>Hello World</root>"
199+ val isValid = OutputFormatter .XML_FORMATTER .isValid(xml)
200+ assertFalse(isValid)
201+ }
202+
203+ @Test
204+ fun testXmlScientificNotationLikeValues () {
205+ val body = """
206+ <root>
207+ <value>1e10</value>
208+ <small>2.5e-3</small>
209+ </root>
210+ """ .trimIndent()
211+
212+ val formatted = OutputFormatter .XML_FORMATTER .getFormatted(body)
213+ assertNotNull(formatted)
214+ }
215+
216+ @Test
217+ fun testXmlWithAttributes () {
218+ val body = """
219+ <root>
220+ <item id="123" type="example">Content</item>
221+ </root>
222+ """ .trimIndent()
223+
224+ val formatted = OutputFormatter .XML_FORMATTER .getFormatted(body)
225+ assertNotNull(formatted)
226+ }
227+
228+ @Test
229+ fun testXmlWithSpecialCharacters () {
230+ val body = """
231+ <root>
232+ <text><test> & "quote"</text>
233+ </root>
234+ """ .trimIndent()
235+
236+ val formatted = OutputFormatter .XML_FORMATTER .getFormatted(body)
237+ assertNotNull(formatted)
238+ }
239+
240+ @Test
241+ fun testXmlNestedElements () {
242+ val body = """
243+ <root>
244+ <parent>
245+ <child>
246+ <subchild>value</subchild>
247+ </child>
248+ </parent>
249+ </root>
250+ """ .trimIndent()
251+
252+ val formatted = OutputFormatter .XML_FORMATTER .getFormatted(body)
253+ assertNotNull(formatted)
254+ }
255+
256+ @Test
257+ fun testXmlSelfClosingTag () {
258+ val body = """
259+ <root>
260+ <empty />
261+ </root>
262+ """ .trimIndent()
263+
264+ val formatted = OutputFormatter .XML_FORMATTER .getFormatted(body)
265+ assertNotNull(formatted)
266+ }
168267
268+ @Test
269+ fun testXmlInvalidEscape () {
270+ val body = """
271+ <root>
272+ <text>&invalid;</text>
273+ </root>
274+ """ .trimIndent()
275+
276+ assertThrows<Exception > {
277+ OutputFormatter .XML_FORMATTER .getFormatted(body)
278+ }
279+ }
280+
281+ @Test
282+ fun testXmlUnclosedTag () {
283+ val body = """
284+ <root>
285+ <child>value
286+ </root>
287+ """ .trimIndent()
288+
289+ assertThrows<Exception > {
290+ OutputFormatter .XML_FORMATTER .getFormatted(body)
291+ }
292+ }
293+
294+ @Test
295+ fun testJsonReadFields () {
296+ val body = """
297+ {
298+ "authorId": "VZyJz8z_Eu2",
299+ "creationTime": "1921-3-13T10:18:56.000Z",
300+ "newsId": "L",
301+ "title": "Hello"
302+ }
303+ """ .trimIndent()
304+
305+ val result = OutputFormatter .JSON_FORMATTER .readFields(
306+ body,
307+ setOf (" authorId" , " newsId" )
308+ )
309+
310+ assertNotNull(result)
311+ assertEquals(" VZyJz8z_Eu2" , result?.get(" authorId" ))
312+ assertEquals(" L" , result?.get(" newsId" ))
313+ assertEquals(2 , result?.size)
314+ }
315+
316+ @Test
317+ fun testJsonReadFieldsMissingAndInvalid () {
318+ val body = """
319+ {
320+ "authorId": "VZyJz8z_Eu2",
321+ "title": "Hello"
322+ }
323+ """ .trimIndent()
324+
325+ val result = OutputFormatter .JSON_FORMATTER .readFields(
326+ body,
327+ setOf (" authorId" , " newsId" )
328+ )
329+
330+ assertNotNull(result)
331+ assertEquals(" VZyJz8z_Eu2" , result?.get(" authorId" ))
332+ assertFalse(result?.containsKey(" newsId" ) ? : true )
333+
334+ val invalidBody = """
335+ {
336+ "authorId": "VZyJz8z_Eu2",
337+ "title": "Hello"
338+ """ .trimIndent()
339+
340+ val invalidResult = OutputFormatter .JSON_FORMATTER .readFields(
341+ invalidBody,
342+ setOf (" authorId" , " title" )
343+ )
344+
345+ assertNull(invalidResult)
346+ }
347+
348+ @Test
349+ fun testXmlReadFields () {
350+ val body = """
351+ <root>
352+ <authorId>VZyJz8z_Eu2</authorId>
353+ <creationTime>1921-3-13T10:18:56.000Z</creationTime>
354+ <newsId>L</newsId>
355+ <title>Hello</title>
356+ </root>
357+ """ .trimIndent()
358+
359+ val result = OutputFormatter .XML_FORMATTER .readFields(
360+ body,
361+ setOf (" authorId" , " newsId" )
362+ )
363+
364+ assertNotNull(result)
365+ assertEquals(" VZyJz8z_Eu2" , result?.get(" authorId" ))
366+ assertEquals(" L" , result?.get(" newsId" ))
367+ assertEquals(2 , result?.size)
368+ }
369+
370+ @Test
371+ fun testXmlReadFieldsMissingAndInvalid () {
372+ val body = """
373+ <root>
374+ <authorId>VZyJz8z_Eu2</authorId>
375+ <title>Hello</title>
376+ </root>
377+ """ .trimIndent()
378+
379+ val result = OutputFormatter .XML_FORMATTER .readFields(
380+ body,
381+ setOf (" authorId" , " newsId" )
382+ )
383+
384+ assertNotNull(result)
385+ assertEquals(" VZyJz8z_Eu2" , result?.get(" authorId" ))
386+ assertFalse(result?.containsKey(" newsId" ) ? : true )
387+
388+ val invalidBody = """
389+ <root>
390+ <authorId>VZyJz8z_Eu2</authorId>
391+ <title>Hello</title>
392+ """ .trimIndent()
393+
394+ val invalidResult = OutputFormatter .XML_FORMATTER .readFields(
395+ invalidBody,
396+ setOf (" authorId" , " title" )
397+ )
398+
399+ assertNull(invalidResult)
400+ }
169401}
0 commit comments