// // 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(_ html: String, configuration: TextConverterConfiguration = .init(), callbacks _: Callbacks.Type = Callbacks.self) -> String { var converter = TextConverter(configuration: configuration) return converter.convert(html: html) } func testConvertBR() { XCTAssertEqual(convert("a
b"), "a\nb") XCTAssertEqual(convert("a
b"), "a\nb") } func testConvertA() { XCTAssertEqual(convert("link"), "link") } func testIncorrectNesting() { XCTAssertEqual(convert("bold both italic"), "bold both italic") } func testTextAfterBlockElement() { XCTAssertEqual(convert("
wee
after"), "wee\n\nafter") XCTAssertEqual(convert("
wee
after", configuration: .init(insertNewlines: false)), "wee after") } func testMultipleBlockElements() { XCTAssertEqual(convert("
a
b
"), "a\n\nb") XCTAssertEqual(convert("
a
b
", configuration: .init(insertNewlines: false)), "a b") } func testElementActionCallback() { struct Callbacks: HTMLConversionCallbacks { static func elementAction(name: String, attributes: [Attribute]) -> ElementAction { let clazz = attributes.attributeValue(for: "class") if clazz == "skip" { return .skip } else if clazz == "replace" { return .replace("…") } else if clazz == "append" { return .append("…") } else { return .default } } } let skipped = convert("test", callbacks: Callbacks.self) XCTAssertEqual(skipped, "") let skipNested = convert("test", callbacks: Callbacks.self) XCTAssertEqual(skipNested, "") let replaced = convert("test", callbacks: Callbacks.self) XCTAssertEqual(replaced, "…") let appended = convert("test", callbacks: Callbacks.self) XCTAssertEqual(appended, "test…") let appended2 = convert("test blah", callbacks: Callbacks.self) XCTAssertEqual(appended2, "test blah…") } func testEmptyBlockElements() { XCTAssertEqual(convert("

inside
quote
after

"), "inside\nquote\n\nafter") } }