@@ -398,6 +398,52 @@ class HTMLEntitiesTests: XCTestCase {
398
398
XCTAssertEqual ( try text. htmlUnescape ( strict: false ) , " 한 " )
399
399
}
400
400
401
+ func testDecodeMaps( ) throws {
402
+ struct CodePointsAndCharacters : Codable {
403
+ var codepoints : [ UInt32 ]
404
+ var characters : String
405
+ }
406
+
407
+ var entitiesData : Data ?
408
+ var dataTaskError : Error ?
409
+ let expectation = self . expectation ( description: " Downloading entities.json " )
410
+
411
+ let url = URL ( string: " https://html.spec.whatwg.org/entities.json " ) !
412
+ URLSession . shared. dataTask ( with: url) { data, _, error in
413
+ if let error = error {
414
+ dataTaskError = error
415
+ } else if let data = data {
416
+ entitiesData = data
417
+ }
418
+ expectation. fulfill ( )
419
+ } . resume ( )
420
+
421
+ self . wait ( for: [ expectation] , timeout: 60 )
422
+
423
+ if let dataTaskError = dataTaskError {
424
+ throw dataTaskError
425
+ }
426
+
427
+ guard let entitiesData = entitiesData else {
428
+ XCTFail ( " Failed to download entities.json " )
429
+ return
430
+ }
431
+
432
+ let dict = try JSONDecoder ( ) . decode ( [ String : CodePointsAndCharacters ] . self, from: entitiesData)
433
+
434
+ for (k, v) in specialNamedCharactersDecodeMap {
435
+ XCTAssertEqual ( dict [ " & \( k) " ] !. codepoints, v. unicodeScalars. map ( \. value) , k)
436
+ }
437
+
438
+ for (k, v) in legacyNamedCharactersDecodeMap {
439
+ XCTAssertEqual ( dict [ " & \( k) " ] !. codepoints, v. unicodeScalars. map ( \. value) , k)
440
+ }
441
+
442
+ for (k, v) in namedCharactersDecodeMap {
443
+ XCTAssertEqual ( dict [ " & \( k) " ] !. codepoints, v. unicodeScalars. map ( \. value) , k)
444
+ }
445
+ }
446
+
401
447
static var allTests : [ ( String , ( HTMLEntitiesTests ) -> ( ) throws -> Void ) ] {
402
448
return [
403
449
( " testNamedCharacterReferences " , testNamedCharacterReferences) ,
@@ -406,7 +452,8 @@ class HTMLEntitiesTests: XCTestCase {
406
452
( " testDecode " , testDecode) ,
407
453
( " testInvertibility " , testInvertibility) ,
408
454
( " testEdgeCases " , testEdgeCases) ,
409
- ( " testREADMEExamples " , testREADMEExamples)
455
+ ( " testREADMEExamples " , testREADMEExamples) ,
456
+ ( " testDecodeMaps " , testDecodeMaps)
410
457
]
411
458
}
412
459
}
0 commit comments