@@ -18,13 +18,14 @@ import func Commands.getOutputDir
1818import enum Commands. CoverageFormat
1919import struct Commands. CoverageFormatOutput
2020import typealias Basics. StringError
21+ import struct Commands. PlainTextEncoder
2122
2223@Suite (
2324 . tags(
2425 . TestSize. small,
2526 )
2627)
27- struct TestCommmandHelperTests {
28+ struct TestCommmandHelpersTests {
2829
2930 @Suite
3031 struct getOutputDirTests {
@@ -404,8 +405,10 @@ struct TestCommmandHelperTests {
404405 var output = CoverageFormatOutput ( )
405406 try output. addFormat ( . json, path: path)
406407
407- let jsonString = try output. encodeAsJSON ( )
408- let jsonData = jsonString. data ( using: . utf8) !
408+ let encoder = JSONEncoder ( )
409+ encoder. keyEncodingStrategy = . convertToSnakeCase
410+ let jsonData = try encoder. encode ( output)
411+ let jsonString = String ( decoding: jsonData, as: UTF8 . self)
409412 let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
410413
411414 #expect( decoded [ " json " ] == " /path/to/coverage.json " )
@@ -430,8 +433,11 @@ struct TestCommmandHelperTests {
430433 try output. addFormat ( . json, path: jsonPath)
431434 try output. addFormat ( . html, path: htmlPath)
432435
433- let jsonString = try output. encodeAsJSON ( )
434- let jsonData = jsonString. data ( using: . utf8) !
436+ let encoder = JSONEncoder ( )
437+ encoder. keyEncodingStrategy = . convertToSnakeCase
438+ encoder. outputFormatting = [ . prettyPrinted]
439+ let jsonData = try encoder. encode ( output)
440+ let jsonString = String ( decoding: jsonData, as: UTF8 . self)
435441 let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
436442
437443 #expect( decoded [ " json " ] == " /path/to/coverage.json " )
@@ -447,8 +453,11 @@ struct TestCommmandHelperTests {
447453 func encodeAsJSONEmpty( ) throws {
448454 let output = CoverageFormatOutput ( )
449455
450- let jsonString = try output. encodeAsJSON ( )
451- let jsonData = jsonString. data ( using: . utf8) !
456+ let encoder = JSONEncoder ( )
457+ encoder. keyEncodingStrategy = . convertToSnakeCase
458+ encoder. outputFormatting = [ . prettyPrinted]
459+ let jsonData = try encoder. encode ( output)
460+ let jsonString = String ( decoding: jsonData, as: UTF8 . self)
452461 let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
453462
454463 #expect( decoded. isEmpty)
@@ -469,9 +478,14 @@ struct TestCommmandHelperTests {
469478 var output = CoverageFormatOutput ( )
470479 try output. addFormat ( format, path: path)
471480
472- let textString = output. encodeAsText ( )
481+ var encoder = PlainTextEncoder ( )
482+ encoder. formattingOptions = [ . prettyPrinted]
483+ let textData = try encoder. encode ( output)
484+ let textString = String ( decoding: textData, as: UTF8 . self) . trimmingCharacters ( in: . whitespacesAndNewlines)
473485
474- #expect( textString == " /path/to/coverage.json " )
486+ // PlainTextEncoder capitalizes first letter of keys
487+ let expectedFormat = format. rawValue. prefix ( 1 ) . uppercased ( ) + format. rawValue. dropFirst ( )
488+ #expect( textString == " \( expectedFormat) : /path/to/coverage.json " )
475489 }
476490
477491 @Test ( " Encode as text with multiple formats " )
@@ -483,17 +497,24 @@ struct TestCommmandHelperTests {
483497 try output. addFormat ( . json, path: jsonPath)
484498 try output. addFormat ( . html, path: htmlPath)
485499
486- let textString = output. encodeAsText ( )
500+ var encoder = PlainTextEncoder ( )
501+ encoder. formattingOptions = [ . prettyPrinted]
502+ let textData = try encoder. encode ( output)
503+ let textString = String ( decoding: textData, as: UTF8 . self) . trimmingCharacters ( in: . whitespacesAndNewlines)
487504
488505 // Should be sorted by format name (html comes before json alphabetically)
489- #expect( textString == " HTML: /path/to/coverage-html \n JSON: /path/to/coverage.json " )
506+ // PlainTextEncoder capitalizes first letter of keys
507+ #expect( textString == " Html: /path/to/coverage-html \n Json: /path/to/coverage.json " )
490508 }
491509
492510 @Test ( " Encode as text with empty data " )
493511 func encodeAsTextEmpty( ) throws {
494512 let output = CoverageFormatOutput ( )
495513
496- let textString = output. encodeAsText ( )
514+ var encoder = PlainTextEncoder ( )
515+ encoder. formattingOptions = [ . prettyPrinted]
516+ let textData = try encoder. encode ( output)
517+ let textString = String ( decoding: textData, as: UTF8 . self) . trimmingCharacters ( in: . whitespacesAndNewlines)
497518
498519 #expect( textString. isEmpty)
499520 }
@@ -510,13 +531,17 @@ struct TestCommmandHelperTests {
510531 try output. addFormat ( . html, path: htmlPath) // Add html second
511532
512533 // Text encoding should show html first (alphabetically)
513- let textString = output. encodeAsText ( )
514- #expect( textString. hasPrefix ( " HTML: " ) )
515- #expect( textString. hasSuffix ( " JSON: /json/path " ) )
534+ var textEncoder = PlainTextEncoder ( )
535+ textEncoder. formattingOptions = [ . prettyPrinted]
536+ let textData = try textEncoder. encode ( output)
537+ let textString = String ( decoding: textData, as: UTF8 . self) . trimmingCharacters ( in: . whitespacesAndNewlines)
538+ #expect( textString. hasPrefix ( " Html: " ) )
539+ #expect( textString. hasSuffix ( " Json: /json/path " ) )
516540
517541 // JSON encoding should also maintain consistent ordering
518- let jsonString = try output. encodeAsJSON ( )
519- let jsonData = jsonString. data ( using: . utf8) !
542+ let jsonEncoder = JSONEncoder ( )
543+ jsonEncoder. keyEncodingStrategy = . convertToSnakeCase
544+ let jsonData = try jsonEncoder. encode ( output)
520545 let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
521546
522547 #expect( decoded [ " html " ] == " /html/path " )
@@ -529,9 +554,12 @@ struct TestCommmandHelperTests {
529554 var output = CoverageFormatOutput ( )
530555 try output. addFormat ( . json, path: specialPath)
531556
532- let textString = output. encodeAsText ( )
557+ var encoder = PlainTextEncoder ( )
558+ encoder. formattingOptions = [ . prettyPrinted]
559+ let textData = try encoder. encode ( output)
560+ let textString = String ( decoding: textData, as: UTF8 . self) . trimmingCharacters ( in: . whitespacesAndNewlines)
533561
534- #expect( textString == " /path with/spaces & symbols/coverage.json " )
562+ #expect( textString == " Json: /path with/spaces & symbols/coverage.json" )
535563 }
536564
537565 @Test ( " JSON encoding handles special characters in paths " )
@@ -540,8 +568,9 @@ struct TestCommmandHelperTests {
540568 var output = CoverageFormatOutput ( )
541569 try output. addFormat ( . json, path: specialPath)
542570
543- let jsonString = try output. encodeAsJSON ( )
544- let jsonData = jsonString. data ( using: . utf8) !
571+ let encoder = JSONEncoder ( )
572+ encoder. keyEncodingStrategy = . convertToSnakeCase
573+ let jsonData = try encoder. encode ( output)
545574 let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
546575
547576 #expect( decoded [ " json " ] == " /path with/spaces & symbols/coverage.json " )
0 commit comments