NetNewsWire/Modules/Parser/Tests/ParserTests/EntityDecodingTests.swift
2024-09-26 19:52:51 -07:00

96 lines
2.2 KiB
Swift

//
// EntityDecodingTests.swift
// RSParserTests
//
// Created by Brent Simmons on 12/30/17.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import XCTest
import Parser
final class EntityDecodingTests: XCTestCase {
func test39Decoding() {
// Bug found by Manton Reece the ' entity was not getting decoded by NetNewsWire in JSON Feeds from micro.blog.
let s = "These are the times that try men's souls."
let decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "These are the times that try men's souls.")
}
func testEntityAtBeginning() {
let s = "'leading single quote"
let decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "'leading single quote")
}
func testEntityAtEnd() {
let s = "trailing single quote'"
let decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "trailing single quote'")
}
func testEntityInMiddle() {
let s = "entity ç in middle"
let decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "entity ç in middle")
}
func testMultipleEntitiesInARow() {
let s = "çèmult……iple 'æ"entities÷♥"
let decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "çèmult……iple 'æ\"entities÷♥")
}
func testOnlyEntity() {
var s = "…"
var decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "")
s = "…"
decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "")
s = "'"
decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "'")
s = "§"
decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "§")
s = "£"
decoded = HTMLEntityDecoder.decodedString(s)
XCTAssertEqual(decoded, "£")
}
func testPerformance() {
// 0.009 sec on my 2012 iMac.
let s = stringForResource("DaringFireball", "html")
self.measure {
_ = HTMLEntityDecoder.decodedString(s)
}
}
}
func stringForResource(_ filename: String, _ fileExtension: String) -> String {
let filename = "Resources/\(filename)"
let path = Bundle.module.path(forResource: filename, ofType: fileExtension)!
return try! String(contentsOfFile: path)
}