Files
bookstax/bookstaxTests/StringHTMLTests.swift
2026-04-20 09:41:18 +02:00

89 lines
2.5 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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("&amp; decodes to &")
func ampersandEntity() {
#expect("Cats &amp; Dogs".strippingHTML == "Cats & Dogs")
}
@Test("&lt; and &gt; decode to < and >")
func angleEntities() {
#expect("&lt;tag&gt;".strippingHTML == "<tag>")
}
@Test("&nbsp; is decoded (non-empty result)")
func nbspEntity() {
let result = "Hello&nbsp;World".strippingHTML
#expect(!result.isEmpty)
#expect(result.contains("Hello"))
#expect(result.contains("World"))
}
@Test("&quot; decodes to double quote")
func quotEntity() {
#expect("Say &quot;hi&quot;".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"))
}
}