Skip to content

Commit

Permalink
Add assert_equal(List[StringSlice], List[StringSlice])
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <[email protected]>
  • Loading branch information
martinvuyk committed Dec 19, 2024
1 parent f3432eb commit a0ec560
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
23 changes: 23 additions & 0 deletions stdlib/src/testing/testing.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ from collections import Optional
from math import isclose

from builtin._location import __call_location, _SourceLocation
from utils import StringSlice

# ===----------------------------------------------------------------------=== #
# Assertions
Expand Down Expand Up @@ -236,6 +237,28 @@ fn assert_equal[
)


fn assert_equal[
O1: ImmutableOrigin, O2: ImmutableOrigin
](lhs: List[StringSlice[O1]], rhs: List[StringSlice[O2]]) raises:
"""Asserts that two lists are equal.
Parameters:
O1: The origin of lhs.
O2: The origin of rhs.
Args:
lhs: The left-hand side list.
rhs: The right-hand side list.
Raises:
An Error with the provided message if assert fails and `None` otherwise.
"""

assert_equal(len(lhs), len(rhs))
for i in range(len(lhs)):
assert_equal(lhs[i], rhs[i])


@always_inline
fn assert_not_equal[
T: Testable
Expand Down
35 changes: 35 additions & 0 deletions stdlib/test/testing/test_assertion.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ from testing import (
)

from utils.numerics import inf, nan
from utils import StringSlice


def test_assert_messages():
Expand Down Expand Up @@ -237,6 +238,39 @@ def test_assert_custom_location():
assert_true("always_false" in str(e))


def test_assert_equal_stringslice():
str1 = "This is Mojo"
str2 = String("This is Mojo")
str3 = "This is mojo"

fn _build(
value: StringLiteral, start: Int, end: Int
) -> StringSlice[StaticConstantOrigin]:
return StringSlice[StaticConstantOrigin](
ptr=value.unsafe_ptr() + start, length=end - start
)

fn _build(
read value: String, start: Int, end: Int
) -> StringSlice[__origin_of(value)]:
return StringSlice[__origin_of(value)](
ptr=value.unsafe_ptr() + start, length=end - start
)

l1 = List(_build(str1, 0, 4), _build(str1, 5, 7), _build(str1, 8, 12))
l2 = List(_build(str2, 0, 4), _build(str2, 5, 7), _build(str2, 8, 12))
l3 = List(_build(str3, 0, 4), _build(str3, 5, 7), _build(str3, 8, 12))
assert_equal(l1, l1)
assert_equal(l2, l2)
assert_equal(l1, l2)

with assert_raises():
assert_equal(l1, l3)

with assert_raises():
assert_equal(l2, l3)


def main():
test_assert_equal_is_generic()
test_assert_not_equal_is_generic()
Expand All @@ -248,3 +282,4 @@ def main():
test_assert_is()
test_assert_is_not()
test_assert_custom_location()
test_assert_equal_stringslice()

0 comments on commit a0ec560

Please sign in to comment.