From 64a68eeaff8cccf5627051eda17edb855c3846d0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 15 Aug 2024 19:03:38 -0700 Subject: [PATCH] input: more parseAndPut tests --- src/input/Binding.zig | 96 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/src/input/Binding.zig b/src/input/Binding.zig index a14a7159e..02876df09 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -879,8 +879,6 @@ pub const Set = struct { .leader => |t| leader: { // If we have a leader, we need to upsert a set for it. if (set.get(t)) |entry| switch (entry) { - // TODO: test BOTH code paths - .leader => |s| { // We have an existing leader for this key already // so reuse the set. @@ -1434,6 +1432,100 @@ test "set: parseAndPut sequence" { } } +test "set: parseAndPut sequence with two actions" { + const testing = std.testing; + const alloc = testing.allocator; + + var s: Set = .{}; + defer s.deinit(alloc); + + try s.parseAndPut(alloc, "a>b=new_window"); + try s.parseAndPut(alloc, "a>c=new_tab"); + var current: *Set = &s; + { + const t: Trigger = .{ .key = .{ .translated = .a } }; + const e = current.get(t).?; + try testing.expect(e == .leader); + current = e.leader; + } + { + const t: Trigger = .{ .key = .{ .translated = .b } }; + const e = current.get(t).?; + try testing.expect(e == .action); + try testing.expect(e.action == .new_window); + } + { + const t: Trigger = .{ .key = .{ .translated = .c } }; + const e = current.get(t).?; + try testing.expect(e == .action); + try testing.expect(e.action == .new_tab); + } +} + +test "set: parseAndPut overwrite sequence" { + const testing = std.testing; + const alloc = testing.allocator; + + var s: Set = .{}; + defer s.deinit(alloc); + + try s.parseAndPut(alloc, "a>b=new_tab"); + try s.parseAndPut(alloc, "a>b=new_window"); + var current: *Set = &s; + { + const t: Trigger = .{ .key = .{ .translated = .a } }; + const e = current.get(t).?; + try testing.expect(e == .leader); + current = e.leader; + } + { + const t: Trigger = .{ .key = .{ .translated = .b } }; + const e = current.get(t).?; + try testing.expect(e == .action); + try testing.expect(e.action == .new_window); + } +} + +test "set: parseAndPut overwrite leader" { + const testing = std.testing; + const alloc = testing.allocator; + + var s: Set = .{}; + defer s.deinit(alloc); + + try s.parseAndPut(alloc, "a=new_tab"); + try s.parseAndPut(alloc, "a>b=new_window"); + var current: *Set = &s; + { + const t: Trigger = .{ .key = .{ .translated = .a } }; + const e = current.get(t).?; + try testing.expect(e == .leader); + current = e.leader; + } + { + const t: Trigger = .{ .key = .{ .translated = .b } }; + const e = current.get(t).?; + try testing.expect(e == .action); + try testing.expect(e.action == .new_window); + } +} + +test "set: parseAndPut unbind sequence unbinds leader" { + const testing = std.testing; + const alloc = testing.allocator; + + var s: Set = .{}; + defer s.deinit(alloc); + + try s.parseAndPut(alloc, "a>b=new_window"); + try s.parseAndPut(alloc, "a>b=unbind"); + var current: *Set = &s; + { + const t: Trigger = .{ .key = .{ .translated = .a } }; + try testing.expect(current.get(t) == null); + } +} + test "set: maintains reverse mapping" { const testing = std.testing; const alloc = testing.allocator;