test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. var test = require('tape').test
  2. var commist = require('./')
  3. test('registering a command', function (t) {
  4. t.plan(2)
  5. var program = commist()
  6. var result
  7. program.register('hello', function (args) {
  8. t.deepEqual(args, ['a', '-x', '23'])
  9. })
  10. result = program.parse(['hello', 'a', '-x', '23'])
  11. t.notOk(result, 'must return null, the command have been handled')
  12. })
  13. test('registering two commands', function (t) {
  14. t.plan(1)
  15. var program = commist()
  16. program.register('hello', function (args) {
  17. t.ok(false, 'must pick the right command')
  18. })
  19. program.register('world', function (args) {
  20. t.deepEqual(args, ['a', '-x', '23'])
  21. })
  22. program.parse(['world', 'a', '-x', '23'])
  23. })
  24. test('registering two commands (bis)', function (t) {
  25. t.plan(1)
  26. var program = commist()
  27. program.register('hello', function (args) {
  28. t.deepEqual(args, ['a', '-x', '23'])
  29. })
  30. program.register('world', function (args) {
  31. t.ok(false, 'must pick the right command')
  32. })
  33. program.parse(['hello', 'a', '-x', '23'])
  34. })
  35. test('registering two words commands', function (t) {
  36. t.plan(1)
  37. var program = commist()
  38. program.register('hello', function (args) {
  39. t.ok(false, 'must pick the right command')
  40. })
  41. program.register('hello world', function (args) {
  42. t.deepEqual(args, ['a', '-x', '23'])
  43. })
  44. program.parse(['hello', 'world', 'a', '-x', '23'])
  45. })
  46. test('registering two words commands (bis)', function (t) {
  47. t.plan(1)
  48. var program = commist()
  49. program.register('hello', function (args) {
  50. t.deepEqual(args, ['a', '-x', '23'])
  51. })
  52. program.register('hello world', function (args) {
  53. t.ok(false, 'must pick the right command')
  54. })
  55. program.parse(['hello', 'a', '-x', '23'])
  56. })
  57. test('registering ambiguous commands throws exception', function (t) {
  58. var program = commist()
  59. function noop () {}
  60. program.register('hello', noop)
  61. program.register('hello world', noop)
  62. program.register('hello world matteo', noop)
  63. try {
  64. program.register('hello world', noop)
  65. t.ok(false, 'must throw if double-registering the same command')
  66. } catch (err) {
  67. }
  68. t.end()
  69. })
  70. test('looking up commands', function (t) {
  71. var program = commist()
  72. function noop1 () {}
  73. function noop2 () {}
  74. function noop3 () {}
  75. program.register('hello', noop1)
  76. program.register('hello world matteo', noop3)
  77. program.register('hello world', noop2)
  78. t.equal(program.lookup('hello')[0].func, noop1)
  79. t.equal(program.lookup('hello world matteo')[0].func, noop3)
  80. t.equal(program.lookup('hello world')[0].func, noop2)
  81. t.end()
  82. })
  83. test('looking up commands with abbreviations', function (t) {
  84. var program = commist()
  85. function noop1 () {}
  86. function noop2 () {}
  87. function noop3 () {}
  88. program.register('hello', noop1)
  89. program.register('hello world matteo', noop3)
  90. program.register('hello world', noop2)
  91. t.equal(program.lookup('hel')[0].func, noop1)
  92. t.equal(program.lookup('hel wor mat')[0].func, noop3)
  93. t.equal(program.lookup('hel wor')[0].func, noop2)
  94. t.end()
  95. })
  96. test('looking up strict commands', function (t) {
  97. var program = commist()
  98. function noop1 () {}
  99. function noop2 () {}
  100. program.register({ command: 'restore', strict: true }, noop1)
  101. program.register({ command: 'rest', strict: true }, noop2)
  102. t.equal(program.lookup('restore')[0].func, noop1)
  103. t.equal(program.lookup('rest')[0].func, noop2)
  104. t.equal(program.lookup('remove')[0], undefined)
  105. t.end()
  106. })
  107. test('executing commands from abbreviations', function (t) {
  108. t.plan(1)
  109. var program = commist()
  110. program.register('hello', function (args) {
  111. t.deepEqual(args, ['a', '-x', '23'])
  112. })
  113. program.register('hello world', function (args) {
  114. t.ok(false, 'must pick the right command')
  115. })
  116. program.parse(['hel', 'a', '-x', '23'])
  117. })
  118. test('a command must be at least 3 chars', function (t) {
  119. var program = commist()
  120. function noop1 () {}
  121. try {
  122. program.register('h', noop1)
  123. t.ok(false, 'not thrown')
  124. } catch (err) {
  125. }
  126. t.end()
  127. })
  128. test('a command part must be at least 3 chars', function (t) {
  129. var program = commist()
  130. function noop1 () {}
  131. try {
  132. program.register('h b', noop1)
  133. t.ok(false, 'not thrown')
  134. } catch (err) {
  135. }
  136. t.end()
  137. })