Skip to content

Commit

Permalink
Add testDecodeMaps
Browse files Browse the repository at this point in the history
  • Loading branch information
kkebo committed Apr 9, 2022
1 parent d8ca731 commit f8a9658
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion Tests/HTMLEntitiesTests/HTMLEntitiesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,52 @@ class HTMLEntitiesTests: XCTestCase {
XCTAssertEqual(try text.htmlUnescape(strict: false), "한")
}

func testDecodeMaps() throws {
struct CodePointsAndCharacters: Codable {
var codepoints: [UInt32]
var characters: String
}

var entitiesData: Data?
var dataTaskError: Error?
let expectation = self.expectation(description: "Downloading entities.json")

let url = URL(string: "https://html.spec.whatwg.org/entities.json")!
URLSession.shared.dataTask(with: url) { data, _, error in
if let error = error {
dataTaskError = error
} else if let data = data {
entitiesData = data
}
expectation.fulfill()
}.resume()

self.wait(for: [expectation], timeout: 60)

if let dataTaskError = dataTaskError {
throw dataTaskError
}

guard let data = entitiesData else {
XCTFail("Failed to download entities.json")
return
}

let dict = try JSONDecoder().decode([String: CodePointsAndCharacters].self, from: data)

for (k, v) in specialNamedCharactersDecodeMap {
XCTAssertEqual(dict["&\(k)"]!.codepoints, v.unicodeScalars.map(\.value), k)
}

for (k, v) in legacyNamedCharactersDecodeMap {
XCTAssertEqual(dict["&\(k)"]!.codepoints, v.unicodeScalars.map(\.value), k)
}

for (k, v) in namedCharactersDecodeMap {
XCTAssertEqual(dict["&\(k)"]!.codepoints, v.unicodeScalars.map(\.value), k)
}
}

static var allTests : [(String, (HTMLEntitiesTests) -> () throws -> Void)] {
return [
("testNamedCharacterReferences", testNamedCharacterReferences),
Expand All @@ -406,7 +452,8 @@ class HTMLEntitiesTests: XCTestCase {
("testDecode", testDecode),
("testInvertibility", testInvertibility),
("testEdgeCases", testEdgeCases),
("testREADMEExamples", testREADMEExamples)
("testREADMEExamples", testREADMEExamples),
("testDecodeMaps", testDecodeMaps)
]
}
}

0 comments on commit f8a9658

Please sign in to comment.