Files
element-x-ios/UnitTests/Sources/StringTests.swift
Stefan Ceriu 925e61e40c Preserve the blank lines typed after a list
The newline to break replacement only matched paragraph to paragraph, so a blank
line after a list was dropped and the text following it glued to the last item.
Only a following paragraph is matched, because without a blank line its text
would have continued the last item instead of starting a paragraph of its own.
2026-08-24 15:45:19 +03:00

99 lines
3.3 KiB
Swift

//
// Copyright 2025 Element Creations Ltd.
// Copyright 2022-2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
@testable import ElementX
import Testing
struct StringTests {
@Test
func emptyIsAscii() {
#expect("".isASCII)
}
@Test
func spaceIsAscii() {
#expect("".isASCII)
}
@Test
func johnnyIsAscii() {
#expect("johnny".isASCII)
}
@Test
func jöhnnyIsNotAscii() {
#expect(!"jöhnny".isASCII)
}
@Test
func jEmojiHnnyIsNotAscii() {
#expect(!"j🅾️hnny".isASCII)
}
@Test
func asciifiedMethod() {
// ASCII strings return themselves unchanged
#expect("johnny".asciified() == "johnny")
#expect("hello".asciified() == "hello")
#expect("abc123".asciified() == "abc123")
#expect("".asciified() == "")
#expect(" ".asciified() == " ")
// Non-ASCII strings get converted or stripped
#expect("jöhnny".asciified() == "johnny", "ö should become o")
#expect("jåhnny".asciified() == "jahnny", "å should become a")
#expect("café".asciified() == "cafe")
#expect("naïve".asciified() == "naive")
#expect("résumé".asciified() == "resume")
#expect("🚀".asciified() == "")
#expect("Heartbreak Hotel 🏩".asciified() == "Heartbreak Hotel", "The emoji should be stripped.")
#expect("1️⃣2️⃣3️⃣".asciified() == "123", "The emoji should be converted to ASCII.")
}
@Test
func ellipsizeWorks() {
#expect("ellipsize".ellipsize(length: 5) == "ellip…")
}
@Test
func ellipsizeNotNeeded() {
#expect("ellipsize".ellipsize(length: 15) == "ellipsize")
}
@Test
func replaceBreakOccurrences() {
let input0 = "</p><p>"
let input1 = "</p>\n<p>"
let input2 = "</p>\n\n<p>"
let input3 = "</p>\n\n\n\n<p>"
let input4 = "<p>a</p>\n<p>b</p>"
let input5 = "empty"
let input6 = "<ul><li>a</li></ul>\n<p>b</p>"
let input7 = "</p>\n<ul>"
let expectedOutput0 = input0
let expectedOutput1 = "</p><br><p>"
let expectedOutput2 = "</p><br><br><p>"
let expectedOutput3 = "</p><br><br><br><br><p>"
let expectedOutput4 = "<p>a</p><br><p>b</p>"
let expectedOutput5 = input5
let expectedOutput6 = "<ul><li>a</li></ul><br><p>b</p>"
// A list can interrupt a paragraph, so this newline isn't a blank line the user typed.
let expectedOutput7 = input7
#expect(input0.replacingHtmlBreaksOccurrences() == expectedOutput0)
#expect(input1.replacingHtmlBreaksOccurrences() == expectedOutput1)
#expect(input2.replacingHtmlBreaksOccurrences() == expectedOutput2)
#expect(input3.replacingHtmlBreaksOccurrences() == expectedOutput3)
#expect(input4.replacingHtmlBreaksOccurrences() == expectedOutput4)
#expect(input5.replacingHtmlBreaksOccurrences() == expectedOutput5)
#expect(input6.replacingHtmlBreaksOccurrences() == expectedOutput6)
#expect(input7.replacingHtmlBreaksOccurrences() == expectedOutput7)
}
}