import Testing
@testable import bookstax
@Suite("String – strippingHTML")
struct StringHTMLTests {
// MARK: Basic tag removal
@Test("Simple tag is removed")
func simpleTag() {
#expect("
Hello
".strippingHTML == "Hello")
}
@Test("Bold tag is removed")
func boldTag() {
#expect("Bold".strippingHTML == "Bold")
}
@Test("Nested tags are fully stripped")
func nestedTags() {
#expect("".strippingHTML == "Deep")
}
@Test("Self-closing tags are removed")
func selfClosingTag() {
let result = "Before
After".strippingHTML
// NSAttributedString adds a newline for
, so just check both words are present
#expect(result.contains("Before"))
#expect(result.contains("After"))
}
// MARK: HTML entities
@Test("& decodes to &")
func ampersandEntity() {
#expect("Cats & Dogs".strippingHTML == "Cats & Dogs")
}
@Test("< and > decode to < and >")
func angleEntities() {
#expect("<tag>".strippingHTML == "")
}
@Test(" is decoded (non-empty result)")
func nbspEntity() {
let result = "Hello World".strippingHTML
#expect(!result.isEmpty)
#expect(result.contains("Hello"))
#expect(result.contains("World"))
}
@Test("" decodes to double quote")
func quotEntity() {
#expect("Say "hi"".strippingHTML == "Say \"hi\"")
}
// MARK: Edge cases
@Test("Empty string returns empty string")
func emptyString() {
#expect("".strippingHTML == "")
}
@Test("Plain text without HTML is returned unchanged")
func plainText() {
#expect("No tags here".strippingHTML == "No tags here")
}
@Test("Leading and trailing whitespace is trimmed")
func trimmingWhitespace() {
#expect(" hello
".strippingHTML == "hello")
}
@Test("HTML with attributes strips fully")
func tagsWithAttributes() {
let html = "Click here"
#expect(html.strippingHTML == "Click here")
}
@Test("Complex real-world snippet is reduced to plain text")
func complexSnippet() {
let html = "Title
First paragraph.
"
let result = html.strippingHTML
#expect(result.contains("Title"))
#expect(result.contains("First paragraph"))
#expect(result.contains("Item 1"))
}
}