Skip to content

Commit ae7d9cc

Browse files
committed
Add testDecodeMaps
1 parent d8ca731 commit ae7d9cc

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

Tests/HTMLEntitiesTests/HTMLEntitiesTests.swift

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,52 @@ class HTMLEntitiesTests: XCTestCase {
398398
XCTAssertEqual(try text.htmlUnescape(strict: false), "한")
399399
}
400400

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+
401447
static var allTests : [(String, (HTMLEntitiesTests) -> () throws -> Void)] {
402448
return [
403449
("testNamedCharacterReferences", testNamedCharacterReferences),
@@ -406,7 +452,8 @@ class HTMLEntitiesTests: XCTestCase {
406452
("testDecode", testDecode),
407453
("testInvertibility", testInvertibility),
408454
("testEdgeCases", testEdgeCases),
409-
("testREADMEExamples", testREADMEExamples)
455+
("testREADMEExamples", testREADMEExamples),
456+
("testDecodeMaps", testDecodeMaps)
410457
]
411458
}
412459
}

0 commit comments

Comments
 (0)