89 lines
2.5 KiB
Swift
89 lines
2.5 KiB
Swift
import Testing
|
||
@testable import bookstax
|
||
|
||
@Suite("String – strippingHTML")
|
||
struct StringHTMLTests {
|
||
|
||
// MARK: Basic tag removal
|
||
|
||
@Test("Simple tag is removed")
|
||
func simpleTag() {
|
||
#expect("<p>Hello</p>".strippingHTML == "Hello")
|
||
}
|
||
|
||
@Test("Bold tag is removed")
|
||
func boldTag() {
|
||
#expect("<b>Bold</b>".strippingHTML == "Bold")
|
||
}
|
||
|
||
@Test("Nested tags are fully stripped")
|
||
func nestedTags() {
|
||
#expect("<div><p><span>Deep</span></p></div>".strippingHTML == "Deep")
|
||
}
|
||
|
||
@Test("Self-closing tags are removed")
|
||
func selfClosingTag() {
|
||
let result = "Before<br/>After".strippingHTML
|
||
// NSAttributedString adds a newline for <br>, 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 == "<tag>")
|
||
}
|
||
|
||
@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("<p> hello </p>".strippingHTML == "hello")
|
||
}
|
||
|
||
@Test("HTML with attributes strips fully")
|
||
func tagsWithAttributes() {
|
||
let html = "<a href=\"https://example.com\" class=\"link\">Click here</a>"
|
||
#expect(html.strippingHTML == "Click here")
|
||
}
|
||
|
||
@Test("Complex real-world snippet is reduced to plain text")
|
||
func complexSnippet() {
|
||
let html = "<h1>Title</h1><p>First paragraph.</p><ul><li>Item 1</li></ul>"
|
||
let result = html.strippingHTML
|
||
#expect(result.contains("Title"))
|
||
#expect(result.contains("First paragraph"))
|
||
#expect(result.contains("Item 1"))
|
||
}
|
||
}
|