HTMLStreamer/Tests/HTMLStreamerTests/TextConverterTests.swift

78 lines
3.1 KiB
Swift
Raw Normal View History

2023-12-23 01:30:29 +00:00
//
// TextConverterTests.swift
//
//
// Created by Shadowfacts on 12/22/23.
//
import XCTest
@testable import HTMLStreamer
final class TextConverterTests: XCTestCase {
private func convert(_ html: String, configuration: TextConverterConfiguration = .init()) -> String {
convert(html, configuration: configuration, callbacks: DefaultCallbacks.self)
}
private func convert<Callbacks: HTMLConversionCallbacks>(_ html: String, configuration: TextConverterConfiguration = .init(), callbacks _: Callbacks.Type = Callbacks.self) -> String {
var converter = TextConverter<Callbacks>(configuration: configuration)
return converter.convert(html: html)
}
func testConvertBR() {
XCTAssertEqual(convert("a<br>b"), "a\nb")
XCTAssertEqual(convert("a<br />b"), "a\nb")
}
func testConvertA() {
XCTAssertEqual(convert("<a href='https://example.com'>link</a>"), "link")
}
func testIncorrectNesting() {
XCTAssertEqual(convert("<strong>bold <em>both</strong> italic</em>"), "bold both italic")
}
func testTextAfterBlockElement() {
XCTAssertEqual(convert("<blockquote>wee</blockquote>after"), "wee\n\nafter")
XCTAssertEqual(convert("<blockquote>wee</blockquote>after", configuration: .init(insertNewlines: false)), "wee after")
2023-12-23 01:30:29 +00:00
}
func testMultipleBlockElements() {
XCTAssertEqual(convert("<blockquote>a</blockquote><blockquote>b</blockquote>"), "a\n\nb")
XCTAssertEqual(convert("<blockquote>a</blockquote><blockquote>b</blockquote>", configuration: .init(insertNewlines: false)), "a b")
2023-12-23 01:30:29 +00:00
}
func testElementActionCallback() {
struct Callbacks: HTMLConversionCallbacks {
static func elementAction(name: String, attributes: [Attribute]) -> ElementAction {
let clazz = attributes.attributeValue(for: "class")
2024-01-17 00:15:56 +00:00
if clazz == "skip" {
2023-12-23 01:30:29 +00:00
return .skip
2024-01-17 00:15:56 +00:00
} else if clazz == "replace" {
2023-12-23 01:30:29 +00:00
return .replace("")
2024-01-17 00:15:56 +00:00
} else if clazz == "append" {
return .append("")
2023-12-23 01:30:29 +00:00
} else {
return .default
}
}
}
2024-01-17 00:15:56 +00:00
let skipped = convert("<span class='skip'>test</span>", callbacks: Callbacks.self)
2023-12-23 01:30:29 +00:00
XCTAssertEqual(skipped, "")
2024-01-17 00:15:56 +00:00
let skipNested = convert("<span class='skip'><b>test</b></span>", callbacks: Callbacks.self)
2023-12-23 01:30:29 +00:00
XCTAssertEqual(skipNested, "")
2024-01-17 00:15:56 +00:00
let replaced = convert("<span class='replace'>test</span>", callbacks: Callbacks.self)
2023-12-23 01:30:29 +00:00
XCTAssertEqual(replaced, "")
2024-01-17 00:15:56 +00:00
let appended = convert("<span class='append'>test</span>", callbacks: Callbacks.self)
XCTAssertEqual(appended, "test…")
let appended2 = convert("<span class='append'>test <span>blah</span></span>", callbacks: Callbacks.self)
XCTAssertEqual(appended2, "test blah…")
2023-12-23 01:30:29 +00:00
}
func testEmptyBlockElements() {
XCTAssertEqual(convert("<p></p><blockquote><span>inside<br>quote</span></blockquote><span>after</span><p></p>"), "inside\nquote\n\nafter")
}
2023-12-23 01:30:29 +00:00
}