From 967aa7bf31998f667616f19f9b83e409340b66d1 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 2 Oct 2016 09:01:29 +0100 Subject: Rework key-sequence parsing. This reworks the parsing of key sequences like `x`: Advantages over the status quo: - Parses key sequences in one only place (previously two), - Removes two hideous regular expression. - Admits multi-modifier bindings (like ``). - Adds more (and better) tests. - (And it should be easier to maintain.) Replaces #2210 (this also simplifies key parsing substantially). --- tests/unit_tests/commands_test.coffee | 43 +++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/unit_tests/commands_test.coffee b/tests/unit_tests/commands_test.coffee index f501a960..1915e062 100644 --- a/tests/unit_tests/commands_test.coffee +++ b/tests/unit_tests/commands_test.coffee @@ -4,12 +4,45 @@ global.Settings = {postUpdateHooks: {}, get: (-> ""), set: ->} {Commands} = require "../../background_scripts/commands.js" context "Key mappings", + setup -> + @testKeySequence = (key, expectedKeyText, expectedKeyLength) -> + keySequence = Commands.normalizeKey key + assert.equal expectedKeyText, keySequence.join "/" + assert.equal expectedKeyLength, keySequence.length + should "lowercase keys correctly", -> - assert.equal (Commands.normalizeKey ''), '' - assert.equal (Commands.normalizeKey ''), '' - assert.equal (Commands.normalizeKey ''), '' - assert.equal (Commands.normalizeKey ''), '' - assert.equal (Commands.normalizeKey ''), '' + @testKeySequence "a", "a", 1 + @testKeySequence "A", "A", 1 + @testKeySequence "ab", "a/b", 2 + + should "parse keys with modifiers", -> + @testKeySequence "", "", 1 + @testKeySequence "", "", 1 + @testKeySequence "", "/", 2 + @testKeySequence "", "", 1 + + should "normalize with modifiers", -> + # Modifiers should be in alphabetical order. + @testKeySequence "", "", 1 + + should "parse and normalize named keys", -> + @testKeySequence "", "", 1 + @testKeySequence "", "", 1 + @testKeySequence "", "", 1 + @testKeySequence "", "", 1 + @testKeySequence "", "", 1 + + should "handle angle brackets", -> + @testKeySequence "<", "<", 1 + @testKeySequence ">", ">", 1 + + @testKeySequence "<<", ">", ">/>", 2 + + @testKeySequence "<>", "", 2 + @testKeySequence "<>", "", 2 + + @testKeySequence "<", "", 2 context "Validate commands and options", should "have either noRepeat or repeatLimit, but not both", -> -- cgit v1.2.3