]> git.gir.st - VimFx.git/blob - extension/test/test-api-frame.coffee
Make it possible to create custom hint commands
[VimFx.git] / extension / test / test-api-frame.coffee
1 ###
2 # Copyright Simon Lydell 2016.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 testUtils = require('./utils')
21 createConfigAPI = require('../lib/api-frame')
22
23 {throws} = testUtils
24
25 exports['test exports'] = (assert, $vim) ->
26 vimfx = createConfigAPI($vim)
27
28 assert.equal(typeof vimfx.listen, 'function', 'listen')
29 assert.equal(typeof vimfx.setHintMatcher, 'function', 'setHintMatcher')
30 assert.equal(typeof vimfx.getMarkerElement, 'function', 'getMarkerElement')
31
32 exports['test vimfx.listen'] = (assert, $vim, teardown) ->
33 shutdownHandlers = []
34 onShutdown = (fn) -> shutdownHandlers.push(fn)
35 vimfx = createConfigAPI($vim, onShutdown)
36
37 resets = []
38 teardown(->
39 reset() for reset in resets
40 return
41 )
42
43 messageManager = new testUtils.MockMessageManager()
44 for name, fn of messageManager when typeof fn == 'function'
45 resets.push(
46 testUtils.stub(FRAME_SCRIPT_ENVIRONMENT, name, fn.bind(messageManager))
47 )
48
49 vimfx.listen('message', ->)
50 assert.equal(messageManager.sendAsyncMessageCalls, 0)
51 assert.equal(messageManager.addMessageListenerCalls, 1)
52 assert.equal(messageManager.removeMessageListenerCalls, 0)
53
54 assert.equal(shutdownHandlers.length, 1)
55 shutdownHandlers[0]()
56 assert.equal(messageManager.sendAsyncMessageCalls, 0)
57 assert.equal(messageManager.addMessageListenerCalls, 1)
58 assert.equal(messageManager.removeMessageListenerCalls, 1)
59
60 exports['test vimfx.setHintMatcher'] = (assert, $vim) ->
61 shutdownHandlers = []
62 onShutdown = (fn) -> shutdownHandlers.push(fn)
63 vimfx = createConfigAPI($vim, onShutdown)
64
65 assert.ok(not $vim.hintMatcher)
66
67 hintMatcher = ->
68 vimfx.setHintMatcher(hintMatcher)
69 assert.equal($vim.hintMatcher, hintMatcher)
70
71 assert.equal(shutdownHandlers.length, 1)
72 shutdownHandlers[0]()
73 assert.ok(not $vim.hintMatcher)
74
75 exports['test vimfx.getMarkerElement'] = (assert, $vim, teardown) ->
76 teardown(->
77 $vim.state.markerElements = []
78 )
79
80 vimfx = createConfigAPI($vim)
81 element = {}
82 $vim.state.markerElements = [{element}]
83
84 assert.equal(vimfx.getMarkerElement(0), element)
85 assert.equal(vimfx.getMarkerElement(1), null)
86 assert.equal(vimfx.getMarkerElement(null), null)
87
88 $vim.state.markerElements = []
89 assert.equal(vimfx.getMarkerElement(0), null)
90
91 exports['test vimfx.listen errors'] = (assert, $vim) ->
92 vimfx = createConfigAPI($vim)
93
94 throws(assert, /message string/i, 'undefined', ->
95 vimfx.listen()
96 )
97
98 throws(assert, /message string/i, 'false', ->
99 vimfx.listen(false)
100 )
101
102 throws(assert, /listener function/i, 'undefined', ->
103 vimfx.listen('message')
104 )
105
106 throws(assert, /listener function/i, 'false', ->
107 vimfx.listen('message', false)
108 )
109
110 exports['test vimfx.setHintMatcher errors'] = (assert, $vim) ->
111 vimfx = createConfigAPI($vim)
112
113 throws(assert, /function/i, 'undefined', ->
114 vimfx.setHintMatcher()
115 )
116
117 throws(assert, /function/i, 'false', ->
118 vimfx.setHintMatcher(false)
119 )
Imprint / Impressum