Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Make String and StringLiteral .splitlines() return List[StringSlice] #3894

Open
wants to merge 11 commits into
base: nightly
Choose a base branch
from
Prev Previous commit
Next Next commit
bring over the tests form string slice
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
  • Loading branch information
martinvuyk committed Dec 17, 2024
commit 8cae7a9543c9c6a7789ece2ceda7a933ebfcd2d7
65 changes: 41 additions & 24 deletions stdlib/test/collections/test_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -849,58 +849,75 @@ def test_split():


def test_splitlines():
alias L = List[String]
alias S = StringSlice[StaticConstantOrigin]
alias L = List[StringSlice[StaticConstantOrigin]]

# FIXME: remove once StringSlice conforms to TestableCollectionElement
fn _assert_equal[
O1: ImmutableOrigin, O2: ImmutableOrigin
](l1: List[StringSlice[O1]], l2: List[StringSlice[O2]]) raises:
assert_equal(len(l1), len(l2))
for i in range(len(l1)):
assert_equal(str(l1[i]), str(l2[i]))

# FIXME: remove once StringSlice conforms to TestableCollectionElement
fn _assert_equal[
O1: ImmutableOrigin
](l1: List[StringSlice[O1]], l2: List[String]) raises:
assert_equal(len(l1), len(l2))
for i in range(len(l1)):
assert_equal(str(l1[i]), l2[i])

# Test with no line breaks
assert_equal(String("hello world").splitlines(), L("hello world"))
_assert_equal(S("hello world").splitlines(), L("hello world"))

# Test with line breaks
assert_equal(String("hello\nworld").splitlines(), L("hello", "world"))
assert_equal(String("hello\rworld").splitlines(), L("hello", "world"))
assert_equal(String("hello\r\nworld").splitlines(), L("hello", "world"))
_assert_equal(S("hello\nworld").splitlines(), L("hello", "world"))
_assert_equal(S("hello\rworld").splitlines(), L("hello", "world"))
_assert_equal(S("hello\r\nworld").splitlines(), L("hello", "world"))

# Test with multiple different line breaks
s1 = String("hello\nworld\r\nmojo\rlanguage\r\n")
s1 = S("hello\nworld\r\nmojo\rlanguage\r\n")
hello_mojo = L("hello", "world", "mojo", "language")
assert_equal(s1.splitlines(), hello_mojo)
assert_equal(
_assert_equal(s1.splitlines(), hello_mojo)
_assert_equal(
s1.splitlines(keepends=True),
L("hello\n", "world\r\n", "mojo\r", "language\r\n"),
)

# Test with an empty string
assert_equal(String("").splitlines(), L())
_assert_equal(S("").splitlines(), L())
# test \v \f \x1c \x1d
s2 = String("hello\vworld\fmojo\x1clanguage\x1d")
assert_equal(s2.splitlines(), hello_mojo)
assert_equal(
s2 = S("hello\vworld\fmojo\x1clanguage\x1d")
_assert_equal(s2.splitlines(), hello_mojo)
_assert_equal(
s2.splitlines(keepends=True),
L("hello\v", "world\f", "mojo\x1c", "language\x1d"),
)

# test \x1c \x1d \x1e
s3 = String("hello\x1cworld\x1dmojo\x1elanguage\x1e")
assert_equal(s3.splitlines(), hello_mojo)
assert_equal(
s3 = S("hello\x1cworld\x1dmojo\x1elanguage\x1e")
_assert_equal(s3.splitlines(), hello_mojo)
_assert_equal(
s3.splitlines(keepends=True),
L("hello\x1c", "world\x1d", "mojo\x1e", "language\x1e"),
)

# test \x85 \u2028 \u2029
var next_line = List[UInt8](0xC2, 0x85, 0)
var next_line = String(List[UInt8](0xC2, 0x85, 0))
"""TODO: \\x85"""
var unicode_line_sep = List[UInt8](0xE2, 0x80, 0xA8, 0)
var unicode_line_sep = String(List[UInt8](0xE2, 0x80, 0xA8, 0))
"""TODO: \\u2028"""
var unicode_paragraph_sep = List[UInt8](0xE2, 0x80, 0xA9, 0)
var unicode_paragraph_sep = String(List[UInt8](0xE2, 0x80, 0xA9, 0))
"""TODO: \\u2029"""

for i in List(next_line, unicode_line_sep, unicode_paragraph_sep):
u = String(i[])
u = i[]
item = String("").join("hello", u, "world", u, "mojo", u, "language", u)
assert_equal(item.splitlines(), hello_mojo)
assert_equal(
item.splitlines(keepends=True),
L("hello" + u, "world" + u, "mojo" + u, "language" + u),
)
s = StringSlice(item)
_assert_equal(s.splitlines(), hello_mojo)
items = List("hello" + u, "world" + u, "mojo" + u, "language" + u)
_assert_equal(s.splitlines(keepends=True), items)


def test_isupper():
Expand Down
Loading