aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2016-03-27 10:05:20 +0100
committerStephen Blott2016-03-27 10:05:20 +0100
commit0656f3ef0dd5f3cfa12f6c925298f30168b00e12 (patch)
tree001c1fe8866b0a1f3cedb0e4b7016d5d34e696a6
parentfa67b115ce1a20e64da5b8eaa67e2979fad8f89d (diff)
downloadvimium-0656f3ef0dd5f3cfa12f6c925298f30168b00e12.tar.bz2
Add test for invokeCommandString().
-rw-r--r--lib/utils.coffee12
-rw-r--r--tests/unit_tests/utils_test.coffee20
2 files changed, 25 insertions, 7 deletions
diff --git a/lib/utils.coffee b/lib/utils.coffee
index 31bb47b6..c7f0a085 100644
--- a/lib/utils.coffee
+++ b/lib/utils.coffee
@@ -9,15 +9,13 @@ Utils =
# Returns true whenever the current page is the extension's background page.
isBackgroundPage: -> @isExtensionPage() and chrome.extension.getBackgroundPage?() == window
- # Takes a dot-notation object string and call the function
- # that it points to with the correct value for 'this'.
+ # Takes a dot-notation object string and calls the function that it points to with the correct value for
+ # 'this'.
invokeCommandString: (str, args...) ->
- components = str.split('.')
+ [names..., name] = str.split '.'
obj = window
- for component in components[0...-1]
- obj = obj[component]
- func = obj[components.pop()]
- func.apply(obj, args)
+ obj = obj[component] for component in names
+ obj[name].apply obj, args
# Escape all special characters, so RegExp will parse the string 'as is'.
# Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee
index 93849349..691b26ec 100644
--- a/tests/unit_tests/utils_test.coffee
+++ b/tests/unit_tests/utils_test.coffee
@@ -129,3 +129,23 @@ context "makeIdempotent",
context "distinctCharacters",
should "eliminate duplicate characters", ->
assert.equal "abc", Utils.distinctCharacters "bbabaabbacabbbab"
+
+context "invokeCommandString",
+ setup ->
+ @beenCalled = false
+ window.singleComponentCommand = => @beenCalled = true
+ window.twoComponentCommand = command: window.singleComponentCommand
+
+ tearDown ->
+ delete window.singleComponentCommand
+ delete window.twoComponentCommand
+
+ should "invoke single-component commands", ->
+ assert.isFalse @beenCalled
+ Utils.invokeCommandString "singleComponentCommand"
+ assert.isTrue @beenCalled
+
+ should "invoke multi-component commands", ->
+ assert.isFalse @beenCalled
+ Utils.invokeCommandString "twoComponentCommand.command"
+ assert.isTrue @beenCalled