From bf319e504e5476d9b0d2dec3e2f8d81ef6439ce4 Mon Sep 17 00:00:00 2001 From: Sam Atman Date: Sat, 2 Nov 2024 10:31:28 -0400 Subject: Add peek() to Grapheme.Iterator This does the expected thing: returns the next ?Grapheme without mutation of the iteration state. --- src/unicode_tests.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/unicode_tests.zig') diff --git a/src/unicode_tests.zig b/src/unicode_tests.zig index 691ccfb..245c03f 100644 --- a/src/unicode_tests.zig +++ b/src/unicode_tests.zig @@ -7,11 +7,37 @@ const mem = std.mem; const testing = std.testing; const unicode = std.unicode; +const grapheme = @import("grapheme"); const Grapheme = @import("grapheme").Grapheme; const GraphemeData = @import("grapheme").GraphemeData; const GraphemeIterator = @import("grapheme").Iterator; const Normalize = @import("Normalize"); +comptime { + testing.refAllDecls(grapheme); +} +test "Iterator.peek" { + const peek_seq = "aΔ👨🏻‍🌾→"; + const data = try GraphemeData.init(std.testing.allocator); + defer data.deinit(); + + var iter = grapheme.Iterator.init(peek_seq, &data); + const peek_a = iter.peek().?; + const next_a = iter.next().?; + try std.testing.expectEqual(peek_a, next_a); + try std.testing.expectEqualStrings("a", peek_a.bytes(peek_seq)); + const peek_d1 = iter.peek().?; + const peek_d2 = iter.peek().?; + try std.testing.expectEqual(peek_d1, peek_d2); + const next_d = iter.next().?; + try std.testing.expectEqual(peek_d2, next_d); + try std.testing.expectEqual(iter.peek(), iter.next()); + try std.testing.expectEqual(iter.peek(), iter.next()); + try std.testing.expectEqual(null, iter.peek()); + try std.testing.expectEqual(null, iter.peek()); + try std.testing.expectEqual(iter.peek(), iter.next()); +} + test "Unicode normalization tests" { var arena = heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); -- cgit v1.2.3