File: test.js

package info (click to toggle)
node-commist 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 92 kB
  • sloc: makefile: 2
file content (198 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

var test = require('tape').test

var commist = require('./')

test('registering a command', function (t) {
  t.plan(2)

  var program = commist()

  var result

  program.register('hello', function (args) {
    t.deepEqual(args, ['a', '-x', '23'])
  })

  result = program.parse(['hello', 'a', '-x', '23'])

  t.notOk(result, 'must return null, the command have been handled')
})

test('registering two commands', function (t) {
  t.plan(1)

  var program = commist()

  program.register('hello', function (args) {
    t.ok(false, 'must pick the right command')
  })

  program.register('world', function (args) {
    t.deepEqual(args, ['a', '-x', '23'])
  })

  program.parse(['world', 'a', '-x', '23'])
})

test('registering two commands (bis)', function (t) {
  t.plan(1)

  var program = commist()

  program.register('hello', function (args) {
    t.deepEqual(args, ['a', '-x', '23'])
  })

  program.register('world', function (args) {
    t.ok(false, 'must pick the right command')
  })

  program.parse(['hello', 'a', '-x', '23'])
})

test('registering two words commands', function (t) {
  t.plan(1)

  var program = commist()

  program.register('hello', function (args) {
    t.ok(false, 'must pick the right command')
  })

  program.register('hello world', function (args) {
    t.deepEqual(args, ['a', '-x', '23'])
  })

  program.parse(['hello', 'world', 'a', '-x', '23'])
})

test('registering two words commands (bis)', function (t) {
  t.plan(1)

  var program = commist()

  program.register('hello', function (args) {
    t.deepEqual(args, ['a', '-x', '23'])
  })

  program.register('hello world', function (args) {
    t.ok(false, 'must pick the right command')
  })

  program.parse(['hello', 'a', '-x', '23'])
})

test('registering ambiguous commands throws exception', function (t) {
  var program = commist()

  function noop () {}

  program.register('hello', noop)
  program.register('hello world', noop)
  program.register('hello world matteo', noop)

  try {
    program.register('hello world', noop)
    t.ok(false, 'must throw if double-registering the same command')
  } catch (err) {
  }

  t.end()
})

test('looking up commands', function (t) {
  var program = commist()

  function noop1 () {}
  function noop2 () {}
  function noop3 () {}

  program.register('hello', noop1)
  program.register('hello world matteo', noop3)
  program.register('hello world', noop2)

  t.equal(program.lookup('hello')[0].func, noop1)
  t.equal(program.lookup('hello world matteo')[0].func, noop3)
  t.equal(program.lookup('hello world')[0].func, noop2)

  t.end()
})

test('looking up commands with abbreviations', function (t) {
  var program = commist()

  function noop1 () {}
  function noop2 () {}
  function noop3 () {}

  program.register('hello', noop1)
  program.register('hello world matteo', noop3)
  program.register('hello world', noop2)

  t.equal(program.lookup('hel')[0].func, noop1)
  t.equal(program.lookup('hel wor mat')[0].func, noop3)
  t.equal(program.lookup('hel wor')[0].func, noop2)

  t.end()
})

test('looking up strict commands', function (t) {
  var program = commist()

  function noop1 () {}
  function noop2 () {}

  program.register({ command: 'restore', strict: true }, noop1)
  program.register({ command: 'rest', strict: true }, noop2)

  t.equal(program.lookup('restore')[0].func, noop1)
  t.equal(program.lookup('rest')[0].func, noop2)
  t.equal(program.lookup('remove')[0], undefined)

  t.end()
})

test('executing commands from abbreviations', function (t) {
  t.plan(1)

  var program = commist()

  program.register('hello', function (args) {
    t.deepEqual(args, ['a', '-x', '23'])
  })

  program.register('hello world', function (args) {
    t.ok(false, 'must pick the right command')
  })

  program.parse(['hel', 'a', '-x', '23'])
})

test('a command must be at least 3 chars', function (t) {
  var program = commist()

  function noop1 () {}

  try {
    program.register('h', noop1)
    t.ok(false, 'not thrown')
  } catch (err) {
  }

  t.end()
})

test('a command part must be at least 3 chars', function (t) {
  var program = commist()

  function noop1 () {}

  try {
    program.register('h b', noop1)
    t.ok(false, 'not thrown')
  } catch (err) {
  }

  t.end()
})