wechat wss demo

test.js 3.9KB

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