@@ -18,13 +18,14 @@ import func Commands.getOutputDir
18
18
import enum Commands. CoverageFormat
19
19
import struct Commands. CoverageFormatOutput
20
20
import typealias Basics. StringError
21
+ import struct Commands. PlainTextEncoder
21
22
22
23
@Suite (
23
24
. tags(
24
25
. TestSize. small,
25
26
)
26
27
)
27
- struct TestCommmandHelperTests {
28
+ struct TestCommmandHelpersTests {
28
29
29
30
@Suite
30
31
struct getOutputDirTests {
@@ -404,8 +405,10 @@ struct TestCommmandHelperTests {
404
405
var output = CoverageFormatOutput ( )
405
406
try output. addFormat ( . json, path: path)
406
407
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)
409
412
let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
410
413
411
414
#expect( decoded [ " json " ] == " /path/to/coverage.json " )
@@ -430,8 +433,11 @@ struct TestCommmandHelperTests {
430
433
try output. addFormat ( . json, path: jsonPath)
431
434
try output. addFormat ( . html, path: htmlPath)
432
435
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)
435
441
let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
436
442
437
443
#expect( decoded [ " json " ] == " /path/to/coverage.json " )
@@ -447,8 +453,11 @@ struct TestCommmandHelperTests {
447
453
func encodeAsJSONEmpty( ) throws {
448
454
let output = CoverageFormatOutput ( )
449
455
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)
452
461
let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
453
462
454
463
#expect( decoded. isEmpty)
@@ -469,9 +478,14 @@ struct TestCommmandHelperTests {
469
478
var output = CoverageFormatOutput ( )
470
479
try output. addFormat ( format, path: path)
471
480
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)
473
485
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 " )
475
489
}
476
490
477
491
@Test ( " Encode as text with multiple formats " )
@@ -483,17 +497,24 @@ struct TestCommmandHelperTests {
483
497
try output. addFormat ( . json, path: jsonPath)
484
498
try output. addFormat ( . html, path: htmlPath)
485
499
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)
487
504
488
505
// 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 " )
490
508
}
491
509
492
510
@Test ( " Encode as text with empty data " )
493
511
func encodeAsTextEmpty( ) throws {
494
512
let output = CoverageFormatOutput ( )
495
513
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)
497
518
498
519
#expect( textString. isEmpty)
499
520
}
@@ -510,13 +531,17 @@ struct TestCommmandHelperTests {
510
531
try output. addFormat ( . html, path: htmlPath) // Add html second
511
532
512
533
// 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 " ) )
516
540
517
541
// 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)
520
545
let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
521
546
522
547
#expect( decoded [ " html " ] == " /html/path " )
@@ -529,9 +554,12 @@ struct TestCommmandHelperTests {
529
554
var output = CoverageFormatOutput ( )
530
555
try output. addFormat ( . json, path: specialPath)
531
556
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)
533
561
534
- #expect( textString == " /path with/spaces & symbols/coverage.json " )
562
+ #expect( textString == " Json: /path with/spaces & symbols/coverage.json" )
535
563
}
536
564
537
565
@Test ( " JSON encoding handles special characters in paths " )
@@ -540,8 +568,9 @@ struct TestCommmandHelperTests {
540
568
var output = CoverageFormatOutput ( )
541
569
try output. addFormat ( . json, path: specialPath)
542
570
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)
545
574
let decoded = try JSONSerialization . jsonObject ( with: jsonData) as! [ String : String ]
546
575
547
576
#expect( decoded [ " json " ] == " /path with/spaces & symbols/coverage.json " )
0 commit comments