From ae7d9ccdeccd1c4208de6859d69fbba6c5a67f93 Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkk669@users.noreply.github.com> Date: Sun, 10 Apr 2022 05:39:01 +0900 Subject: [PATCH] Add testDecodeMaps --- .../HTMLEntitiesTests/HTMLEntitiesTests.swift | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/Tests/HTMLEntitiesTests/HTMLEntitiesTests.swift b/Tests/HTMLEntitiesTests/HTMLEntitiesTests.swift index c1c7b3d..e098442 100644 --- a/Tests/HTMLEntitiesTests/HTMLEntitiesTests.swift +++ b/Tests/HTMLEntitiesTests/HTMLEntitiesTests.swift @@ -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 entitiesData = entitiesData else { + XCTFail("Failed to download entities.json") + return + } + + let dict = try JSONDecoder().decode([String: CodePointsAndCharacters].self, from: entitiesData) + + 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), @@ -406,7 +452,8 @@ class HTMLEntitiesTests: XCTestCase { ("testDecode", testDecode), ("testInvertibility", testInvertibility), ("testEdgeCases", testEdgeCases), - ("testREADMEExamples", testREADMEExamples) + ("testREADMEExamples", testREADMEExamples), + ("testDecodeMaps", testDecodeMaps) ] } }