Adapt old AttributedStringBuilder tests to the new version and update the HTML fixtures

This commit is contained in:
Stefan Ceriu
2025-08-27 17:28:55 +03:00
committed by Doug
parent 56175ac933
commit 561c98c966
11 changed files with 56 additions and 139 deletions
@@ -12,7 +12,8 @@ enum HTMLFixtures: String, CaseIterable {
case matrixIdentifiers case matrixIdentifiers
case links case links
case textFormatting case textFormatting
case blockQuotes case groupedBlockQuotes
case separatedBlockQuotes
case codeBlocks case codeBlocks
case unorderedList case unorderedList
case orderedList case orderedList
@@ -49,7 +50,7 @@ enum HTMLFixtures: String, CaseIterable {
""" """
case .links: case .links:
""" """
Links too:</br><a href=\"https://www.matrix.org/\">Matrix rules! 🤘</a>, matrix.org, www.matrix.org, http://matrix.org Links too:</br><a href=\"https://www.alpha.org/\">Matrix rules! 🤘</a>, beta.org, www.gamma.org, http://delta.org
""" """
case .textFormatting: case .textFormatting:
""" """
@@ -59,7 +60,13 @@ enum HTMLFixtures: String, CaseIterable {
<br> <br>
<sup>Thumbs</sup> if you liked it, <sub>sub</sub> if you loved it! <sup>Thumbs</sup> if you liked it, <sub>sub</sub> if you loved it!
""" """
case .blockQuotes: case .groupedBlockQuotes:
"""
<blockquote>First blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
<blockquote>Second blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
<blockquote>Third blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
"""
case .separatedBlockQuotes:
""" """
<blockquote>First blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote> <blockquote>First blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
<blockquote>Second blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote> <blockquote>Second blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
@@ -67,7 +74,8 @@ enum HTMLFixtures: String, CaseIterable {
""" """
case .codeBlocks: case .codeBlocks:
""" """
<pre>A preformatted code block<code>struct ContentView: View { <pre>A preformatted code block
<code>struct ContentView: View {
var body: some View { var body: some View {
VStack { VStack {
Text("Knock, knock!") Text("Knock, knock!")
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:4ec1eee24450c078e2d930204eb6c4bd5f5a48c1d472926ad25dd061389d7c45 oid sha256:0373e531749b8097a73c8b50b7168ddf840032517089451541f241666dd372ee
size 1144610 size 1087431
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:4d6c10079176b2c9d5d4155e0d917a25191723ce8b0c3c1c23a4b2ceed15f3fb oid sha256:1b2d5c8c8845a2afa32446d6776d6e08ab46b43f302f34cacc8c9ed052cc8d75
size 1116696 size 1060198
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:4ec1eee24450c078e2d930204eb6c4bd5f5a48c1d472926ad25dd061389d7c45 oid sha256:0373e531749b8097a73c8b50b7168ddf840032517089451541f241666dd372ee
size 1144610 size 1087431
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:4d6c10079176b2c9d5d4155e0d917a25191723ce8b0c3c1c23a4b2ceed15f3fb oid sha256:1b2d5c8c8845a2afa32446d6776d6e08ab46b43f302f34cacc8c9ed052cc8d75
size 1116696 size 1060198
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:aeee2464439a366386e0ededbd5ebde7c330a9a90c26964dc9a63e56563dc7f5 oid sha256:5f5d73086343f275db3867387967984ce2f3cb00ba5b60a94fcb3ff57a99b8ce
size 1323827 size 1241249
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:e84edbb38495d3c2d323ee5dcd37896c9b286bec6377980625d35e861e5dbd19 oid sha256:0db9ca076d445687ec5d8c222590a415747b88954bfcf46058733b7e64b868df
size 1307233 size 1224907
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:aeee2464439a366386e0ededbd5ebde7c330a9a90c26964dc9a63e56563dc7f5 oid sha256:5f5d73086343f275db3867387967984ce2f3cb00ba5b60a94fcb3ff57a99b8ce
size 1323827 size 1241249
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:e84edbb38495d3c2d323ee5dcd37896c9b286bec6377980625d35e861e5dbd19 oid sha256:0db9ca076d445687ec5d8c222590a415747b88954bfcf46058733b7e64b868df
size 1307233 size 1224907
@@ -229,8 +229,6 @@ class AttributedStringBuilderV1Tests: XCTestCase {
checkLinkIn(attributedString: attributedStringBuilder.fromPlain(string), expectedLink: expectedLink.absoluteString, expectedRuns: 3) checkLinkIn(attributedString: attributedStringBuilder.fromPlain(string), expectedLink: expectedLink.absoluteString, expectedRuns: 3)
} }
// `Plain link in codeblock: https://www.matrix.org`, Link tag in codeblock: <a href=\"https://www.matrix.org/\">link</a>, plain link: https://www.matrix.org, link tag: <a href=\"https://www.matrix.org/\">link</a>
func testDefaultFont() { func testDefaultFont() {
let htmlString = "<b>Test</b> <i>string</i> " let htmlString = "<b>Test</b> <i>string</i> "
@@ -18,44 +18,21 @@ class AttributedStringBuilderV2Tests: XCTestCase {
} }
func testRenderHTMLStringWithHeaders() { func testRenderHTMLStringWithHeaders() {
let h1HTMLString = "<h1>Large Heading</h1>" guard let attributedString = attributedStringBuilder.fromHTML(HTMLFixtures.headers.rawValue) else {
let h2HTMLString = "<h2>Smaller Heading</h2>"
let h3HTMLString = "<h3>Acceptable Heading</h3>"
guard let h1AttributedString = attributedStringBuilder.fromHTML(h1HTMLString),
let h2AttributedString = attributedStringBuilder.fromHTML(h2HTMLString),
let h3AttributedString = attributedStringBuilder.fromHTML(h3HTMLString) else {
XCTFail("Could not build the attributed string") XCTFail("Could not build the attributed string")
return return
} }
XCTAssertEqual(String(h1AttributedString.characters), "Large Heading") XCTAssertEqual(String(attributedString.characters), "H1 Header\nH2 Header\nH3 Header\nH4 Header\nH5 Header\nH6 Header")
XCTAssertEqual(String(h2AttributedString.characters), "Smaller Heading")
XCTAssertEqual(String(h3AttributedString.characters), "Acceptable Heading")
XCTAssert(h1AttributedString.runs.count == 1) XCTAssertEqual(attributedString.runs.count, 11) // newlines hold no attributes
XCTAssert(h2AttributedString.runs.count == 1)
XCTAssert(h3AttributedString.runs.count == 1)
guard let h1Font = h1AttributedString.runs.first?.uiKit.font, let pointSizes = attributedString.runs.compactMap(\.uiKit.font?.pointSize)
let h2Font = h2AttributedString.runs.first?.uiKit.font, XCTAssertEqual(pointSizes, [24, 22, 20, 18, 16, 14])
let h3Font = h3AttributedString.runs.first?.uiKit.font else {
XCTFail("Could not extract a font from the strings.")
return
}
XCTAssertEqual(h1Font, h2Font)
XCTAssertEqual(h2Font, h3Font)
XCTAssert(h1Font.pointSize > UIFont.preferredFont(forTextStyle: .body).pointSize)
XCTAssert(h1Font.pointSize <= maxHeaderPointSize)
} }
func testRenderHTMLStringWithPreCode() { func testRenderHTMLStringWithPreCode() {
let htmlString = "<pre><code>1\n2\n3\n4\n</code></pre>" guard let attributedString = attributedStringBuilder.fromHTML(HTMLFixtures.codeBlocks.rawValue) else {
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string") XCTFail("Could not build the attributed string")
return return
} }
@@ -69,24 +46,20 @@ class AttributedStringBuilderV2Tests: XCTestCase {
return return
} }
XCTAssertEqual(regex.numberOfMatches(in: string, options: [], range: .init(location: 0, length: string.count)), 3) XCTAssertEqual(regex.numberOfMatches(in: string, options: [], range: .init(location: 0, length: string.count)), 13)
} }
func testRenderHTMLStringWithLink() { func testRenderHTMLStringWithLink() {
let htmlString = "This text contains a <a href=\"https://www.matrix.org/\">link</a>." guard let attributedString = attributedStringBuilder.fromHTML(HTMLFixtures.links.rawValue) else {
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string") XCTFail("Could not build the attributed string")
return return
} }
XCTAssertEqual(String(attributedString.characters), "This text contains a link.") XCTAssertEqual(String(attributedString.characters), "Links too:\nMatrix rules! 🤘, beta.org, www.gamma.org, http://delta.org")
XCTAssertEqual(attributedString.runs.count, 3)
let link = attributedString.runs.first { $0.link != nil }?.link let link = attributedString.runs.first { $0.link != nil }?.link
XCTAssertEqual(link?.host, "www.matrix.org") XCTAssertEqual(link?.host, "www.alpha.org")
} }
func testRenderPlainStringWithLink() { func testRenderPlainStringWithLink() {
@@ -160,19 +133,6 @@ class AttributedStringBuilderV2Tests: XCTestCase {
XCTAssertEqual(h2AttributedString.runs.count, 1) XCTAssertEqual(h2AttributedString.runs.count, 1)
XCTAssertEqual(h3AttributedString.runs.count, 1) XCTAssertEqual(h3AttributedString.runs.count, 1)
guard let h1Font = h1AttributedString.runs.first?.uiKit.font,
let h2Font = h2AttributedString.runs.first?.uiKit.font,
let h3Font = h3AttributedString.runs.first?.uiKit.font else {
XCTFail("Could not extract a font from the strings.")
return
}
XCTAssertEqual(h1Font, h2Font)
XCTAssertEqual(h2Font, h3Font)
XCTAssert(h1Font.pointSize > UIFont.preferredFont(forTextStyle: .body).pointSize)
XCTAssert(h1Font.pointSize <= maxHeaderPointSize)
XCTAssertEqual(h1AttributedString.runs.first?.link?.host, "matrix.org") XCTAssertEqual(h1AttributedString.runs.first?.link?.host, "matrix.org")
XCTAssertEqual(h2AttributedString.runs.first?.link?.host, "matrix.org") XCTAssertEqual(h2AttributedString.runs.first?.link?.host, "matrix.org")
XCTAssertEqual(h3AttributedString.runs.first?.link?.host, "matrix.org") XCTAssertEqual(h3AttributedString.runs.first?.link?.host, "matrix.org")
@@ -230,23 +190,6 @@ class AttributedStringBuilderV2Tests: XCTestCase {
checkLinkIn(attributedString: attributedStringBuilder.fromPlain(string), expectedLink: expectedLink.absoluteString, expectedRuns: 3) checkLinkIn(attributedString: attributedStringBuilder.fromPlain(string), expectedLink: expectedLink.absoluteString, expectedRuns: 3)
} }
// `Plain link in codeblock: https://www.matrix.org`, Link tag in codeblock: <a href=\"https://www.matrix.org/\">link</a>, plain link: https://www.matrix.org, link tag: <a href=\"https://www.matrix.org/\">link</a>
func testDefaultFont() {
let htmlString = "<b>Test</b> <i>string</i> "
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string")
return
}
XCTAssertEqual(attributedString.runs.count, 3)
for run in attributedString.runs {
XCTAssertEqual(run.uiKit.font?.familyName, UIFont.preferredFont(forTextStyle: .body).familyName)
}
}
func testDefaultForegroundColor() { func testDefaultForegroundColor() {
let htmlString = "<b>Test</b> <i>string</i> <a href=\"https://www.matrix.org/\">link</a> <code><a href=\"https://www.matrix.org/\">link</a></code>" let htmlString = "<b>Test</b> <i>string</i> <a href=\"https://www.matrix.org/\">link</a> <code><a href=\"https://www.matrix.org/\">link</a></code>"
@@ -378,20 +321,14 @@ class AttributedStringBuilderV2Tests: XCTestCase {
} }
func testMultipleGroupedBlockquotes() { func testMultipleGroupedBlockquotes() {
let htmlString = """ guard let attributedString = attributedStringBuilder.fromHTML(HTMLFixtures.groupedBlockQuotes.rawValue) else {
<blockquote>First blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
<blockquote>Second blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
<blockquote>Third blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
"""
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string") XCTFail("Could not build the attributed string")
return return
} }
XCTAssertEqual(attributedString.runs.count, 7) XCTAssertEqual(attributedString.runs.count, 11)
XCTAssertEqual(attributedString.formattedComponents.count, 1) XCTAssertEqual(attributedString.formattedComponents.count, 5)
var numberOfBlockquotes = 0 var numberOfBlockquotes = 0
for run in attributedString.runs where run.elementX.blockquote ?? false && run.link != nil { for run in attributedString.runs where run.elementX.blockquote ?? false && run.link != nil {
@@ -402,25 +339,16 @@ class AttributedStringBuilderV2Tests: XCTestCase {
} }
func testMultipleSeparatedBlockquotes() { func testMultipleSeparatedBlockquotes() {
let htmlString = """ guard let attributedString = attributedStringBuilder.fromHTML(HTMLFixtures.separatedBlockQuotes.rawValue) else {
First
<blockquote>blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
Second
<blockquote>blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
Third
<blockquote>blockquote with a <a href=\"https://www.matrix.org/\">link</a> in it</blockquote>
"""
guard let attributedString = attributedStringBuilder.fromHTML(htmlString) else {
XCTFail("Could not build the attributed string") XCTFail("Could not build the attributed string")
return return
} }
XCTAssertEqual(attributedString.runs.count, 12) XCTAssertEqual(attributedString.runs.count, 11)
let coalescedComponents = attributedString.formattedComponents let coalescedComponents = attributedString.formattedComponents
XCTAssertEqual(coalescedComponents.count, 6) XCTAssertEqual(coalescedComponents.count, 5)
var numberOfBlockquotes = 0 var numberOfBlockquotes = 0
for run in attributedString.runs where run.elementX.blockquote ?? false && run.link != nil { for run in attributedString.runs where run.elementX.blockquote ?? false && run.link != nil {
@@ -580,43 +508,25 @@ class AttributedStringBuilderV2Tests: XCTestCase {
checkAttachment(attributedString: attributedStringFromPlain2, expectedRuns: 1) checkAttachment(attributedString: attributedStringFromPlain2, expectedRuns: 1)
} }
func testURLsAreIgnoredInCode() {
var htmlString = "<pre><code>test https://matrix.org test</code></pre>"
var attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1)
XCTAssertNil(attributedStringFromHTML?.link)
htmlString = "<pre><code>matrix.org</code></pre>"
attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1)
XCTAssertNil(attributedStringFromHTML?.link)
}
func testHyperlinksAreIgnoredInCode() {
let htmlString = "<pre><code>test <a href=\"https://matrix.org\">matrix</a> test</code></pre>"
let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
XCTAssertNil(attributedStringFromHTML?.link)
}
func testUserMentionIsIgnoredInCode() { func testUserMentionIsIgnoredInCode() {
let htmlString = "<pre><code>test https://matrix.org/#/@test:matrix.org test</code></pre>" let htmlString = "<pre><code>test https://matrix.org/#/@test:matrix.org test</code></pre>"
let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString) let attributedString = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1) XCTAssertEqual(attributedString?.runs.count, 3)
XCTAssertNil(attributedStringFromHTML?.attachment) XCTAssertNil(attributedString?.attachment)
} }
func testPlainTextUserMentionIsIgnoredInCode() { func testPlainTextUserMentionIsIgnoredInCode() {
let htmlString = "<pre><code>Hey @some.user.ceriu:matrix.org</code></pre>" let htmlString = "<pre><code>Hey @some.user.ceriu:matrix.org</code></pre>"
let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString) let attributedString = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1) XCTAssertEqual(attributedString?.runs.count, 2)
XCTAssertNil(attributedStringFromHTML?.attachment) XCTAssertNil(attributedString?.attachment)
} }
func testAllUsersIsIgnoredInCode() { func testAllUsersIsIgnoredInCode() {
let htmlString = "<pre><code>test @room test</code></pre>" let htmlString = "<pre><code>test @room test</code></pre>"
let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString) let attributedString = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1) XCTAssertEqual(attributedString?.runs.count, 3)
XCTAssertNil(attributedStringFromHTML?.attachment) XCTAssertNil(attributedString?.attachment)
} }
func testMultipleMentions() { func testMultipleMentions() {
@@ -729,6 +639,7 @@ class AttributedStringBuilderV2Tests: XCTestCase {
XCTFail("Couldn't find the link") XCTFail("Couldn't find the link")
return return
} }
XCTAssertTrue(link.requiresConfirmation) XCTAssertTrue(link.requiresConfirmation)
XCTAssertEqual(link.confirmationParameters?.internalURL.absoluteString, "https://matrix.org") XCTAssertEqual(link.confirmationParameters?.internalURL.absoluteString, "https://matrix.org")
XCTAssertEqual(link.confirmationParameters?.displayString, "https://element.io") XCTAssertEqual(link.confirmationParameters?.displayString, "https://element.io")