Update string_iterator.c3 to include extra convenience methods (#1327)

Update string_iterator.c3 to include extra convenience methods

Added peek: returns the next character without incrementing current
Added has_next: checks if the iterator has another element
Added get: gets the current element (the same one that was returned with the previous call to next).
This commit is contained in:
Samuel Goad
2024-08-09 15:10:46 -06:00
committed by GitHub
parent 696d39b922
commit f85c4cd79f
2 changed files with 68 additions and 7 deletions

View File

@@ -0,0 +1,35 @@
module std::core::test::string_iterator::tests @test;
fn void test_at_start()
{
String test = "abcd";
StringIterator iterator = test.iterator();
assert(iterator.get()! == 'a');
assert(iterator.peek()! == 'a');
iterator.next()!;
assert(iterator.next()! == 'b');
assert(iterator.has_next());
}
fn void test_general()
{
String test = "åƦs1";
StringIterator iterator = test.iterator();
assert(iterator.get()! == 'å');
iterator.next()!;
assert(iterator.peek()! == 'Ʀ');
assert(iterator.next()! == 'Ʀ');
iterator.reset();
assert(iterator.current == 0);
}
fn void test_end()
{
String test = "åƦ";
StringIterator iterator = test.iterator();
assert(@ok(iterator.next()));
assert(iterator.peek()! == 'Ʀ');
assert(@ok(iterator.next()));
assert(@catch(iterator.next()));
}