/node_modules/prompt/test/prompt-test.js

https://github.com/endor/kanso · JavaScript · 390 lines · 339 code · 27 blank · 24 comment · 0 complexity · 245fa293f84d08d239643087df1a8e24 MD5 · raw file

  1. /*
  2. * prompt-test.js: Tests for node-prompt.
  3. *
  4. * (C) 2010, Nodejitsu Inc.
  5. *
  6. */
  7. var assert = require('assert'),
  8. vows = require('vows'),
  9. prompt = require('../lib/prompt'),
  10. helpers = require('./helpers');
  11. vows.describe('prompt').addBatch({
  12. "When using prompt": {
  13. topic: function () {
  14. //
  15. // Reset the prompt for mock testing
  16. //
  17. prompt.started = false;
  18. prompt.start({
  19. stdin: helpers.stdin,
  20. stdout: helpers.stdout
  21. });
  22. return null;
  23. },
  24. "the readLine() method": {
  25. topic: function () {
  26. prompt.readLine(this.callback);
  27. helpers.stdin.write('testing\n');
  28. },
  29. "should respond with data from the stdin stream": function (err, input) {
  30. assert.isNull(err);
  31. assert.equal(input, 'testing');
  32. }
  33. },
  34. "the readLineHidden() method": {
  35. "when given backspaces": {
  36. topic: function () {
  37. prompt.readLineHidden(this.callback);
  38. helpers.stdin.write('no-\x08backspace.\x7f');
  39. helpers.stdin.write('\n');
  40. },
  41. "should remove the proper characters": function (err,input) {
  42. assert.isNull(err);
  43. assert.equal(input, 'nobackspace');
  44. }
  45. },
  46. topic: function () {
  47. prompt.readLineHidden(this.callback);
  48. helpers.stdin.write('testing');
  49. helpers.stdin.write('\r\n');
  50. },
  51. "should respond with data from the stdin stream": function (err, input) {
  52. assert.isNull(err);
  53. assert.equal(input, 'testing');
  54. }
  55. },
  56. "the getInput() method": {
  57. "with a simple string prompt": {
  58. topic: function () {
  59. var that = this;
  60. helpers.stdout.once('data', function (msg) {
  61. that.msg = msg;
  62. })
  63. prompt.getInput('test input', this.callback);
  64. helpers.stdin.write('test value\n');
  65. },
  66. "should prompt to stdout and respond with data": function (err, input) {
  67. assert.isNull(err);
  68. assert.equal(input, 'test value');
  69. assert.isTrue(this.msg.indexOf('test input') !== -1);
  70. }
  71. },
  72. "with any field that is not supposed to be empty": {
  73. "and we don't provide any input": {
  74. topic: function () {
  75. var that = this;
  76. helpers.stdout.once('data', function (msg) {
  77. that.msg = msg;
  78. });
  79. helpers.stderr.once('data', function (msg) {
  80. that.errmsg = msg;
  81. });
  82. prompt.getInput(helpers.properties.notblank, function () {});
  83. prompt.once('invalid', this.callback.bind(null, null))
  84. helpers.stdin.write('\n');
  85. },
  86. "should prompt with an error": function (ign, prop, input) {
  87. assert.isObject(prop);
  88. assert.equal(input, '');
  89. assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1);
  90. assert.isTrue(this.msg.indexOf('notblank') !== -1);
  91. }
  92. }
  93. },
  94. "with a hidden field that is not supposed to be empty": {
  95. "and we provide valid input": {
  96. topic: function () {
  97. var that = this;
  98. helpers.stdout.once('data', function (msg) {
  99. that.msg = msg;
  100. });
  101. prompt.getInput('password', this.callback);
  102. helpers.stdin.write('trustno1\n');
  103. },
  104. "should prompt to stdout and respond with data": function (err, input) {
  105. assert.isNull(err);
  106. assert.equal(input, 'trustno1');
  107. assert.isTrue(this.msg.indexOf('password') !== -1);
  108. }
  109. },
  110. "and we don't provide an input": {
  111. topic: function () {
  112. var that = this;
  113. helpers.stdout.once('data', function (msg) {
  114. that.msg = msg;
  115. });
  116. helpers.stderr.once('data', function (msg) {
  117. that.errmsg = msg;
  118. });
  119. prompt.getInput(helpers.properties.password, function () {});
  120. prompt.once('invalid', this.callback.bind(null, null))
  121. helpers.stdin.write('\n');
  122. },
  123. "should prompt with an error": function (ign, prop, input) {
  124. assert.isObject(prop);
  125. assert.equal(input, '');
  126. assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1);
  127. assert.isTrue(this.msg.indexOf('password') !== -1);
  128. }
  129. }
  130. },
  131. "with a complex property prompt": {
  132. "and a valid input": {
  133. topic: function () {
  134. var that = this;
  135. helpers.stdout.once('data', function (msg) {
  136. that.msg = msg;
  137. });
  138. prompt.getInput(helpers.properties.username, this.callback);
  139. helpers.stdin.write('some-user\n');
  140. },
  141. "should prompt to stdout and respond with data": function (err, input) {
  142. assert.isNull(err);
  143. assert.equal(input, 'some-user');
  144. assert.isTrue(this.msg.indexOf('username') !== -1);
  145. }
  146. },
  147. "and an invalid input": {
  148. topic: function () {
  149. var that = this;
  150. helpers.stdout.once('data', function (msg) {
  151. that.msg = msg;
  152. });
  153. helpers.stderr.once('data', function (msg) {
  154. that.errmsg = msg;
  155. })
  156. prompt.getInput(helpers.properties.username, this.callback);
  157. prompt.once('invalid', function () {
  158. prompt.once('prompt', function () {
  159. process.nextTick(function () {
  160. helpers.stdin.write('some-user\n');
  161. })
  162. })
  163. });
  164. helpers.stdin.write('some -user\n');
  165. },
  166. "should prompt with an error before completing the operation": function (err, input) {
  167. assert.isNull(err);
  168. assert.equal(input, 'some-user');
  169. assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1);
  170. assert.isTrue(this.msg.indexOf('username') !== -1);
  171. }
  172. },
  173. "with an invalid validator (array)": {
  174. topic: function () {
  175. prompt.getInput(helpers.properties.badValidator, this.callback);
  176. },
  177. "should respond with an error": function (err, ign) {
  178. assert.isTrue(!!err);
  179. }
  180. }
  181. }
  182. },
  183. "the get() method": {
  184. "with a simple string prompt": {
  185. "that is not a property in prompt.properties": {
  186. topic: function () {
  187. var that = this;
  188. helpers.stdout.once('data', function (msg) {
  189. that.msg = msg;
  190. })
  191. prompt.get('test input', this.callback);
  192. helpers.stdin.write('test value\n');
  193. },
  194. "should prompt to stdout and respond with the value": function (err, result) {
  195. assert.isNull(err);
  196. assert.include(result, 'test input');
  197. assert.equal(result['test input'], 'test value');
  198. assert.isTrue(this.msg.indexOf('test input') !== -1);
  199. }
  200. },
  201. "that is a property name in prompt.properties": {
  202. "with a default value": {
  203. topic: function () {
  204. var that = this;
  205. helpers.stdout.once('data', function (msg) {
  206. that.msg = msg;
  207. });
  208. prompt.properties['riffwabbles'] = helpers.properties['riffwabbles'];
  209. prompt.get('riffwabbles', this.callback);
  210. helpers.stdin.write('\n');
  211. },
  212. "should prompt to stdout and respond with the default value": function (err, result) {
  213. assert.isNull(err);
  214. assert.isTrue(this.msg.indexOf('riffwabbles') !== -1);
  215. assert.isTrue(this.msg.indexOf('(foobizzles)') !== -1);
  216. assert.include(result, 'riffwabbles');
  217. assert.equal(result['riffwabbles'], helpers.properties['riffwabbles'].default);
  218. }
  219. },
  220. "with a sync function validator": {
  221. topic: function () {
  222. var that = this;
  223. helpers.stdout.once('data', function (msg) {
  224. that.msg = msg;
  225. });
  226. prompt.get(helpers.properties.fnvalidator, this.callback);
  227. helpers.stdin.write('fn123\n');
  228. },
  229. "should accept a value that is checked": function (err, result) {
  230. assert.isNull(err);
  231. assert.equal(result['fnvalidator'],'fn123');
  232. }
  233. },
  234. "with a callback validator": {
  235. topic: function () {
  236. var that = this;
  237. helpers.stdout.once('data', function (msg) {
  238. that.msg = msg;
  239. });
  240. prompt.get(helpers.properties.cbvalidator, this.callback);
  241. helpers.stdin.write('cb123\n');
  242. },
  243. "should not accept a value that is correct": function (err, result) {
  244. assert.isNull(err);
  245. assert.equal(result['cbvalidator'],'cb123');
  246. }
  247. }
  248. }
  249. }/*,
  250. "skip prompt with prompt.overide": {
  251. topic: function () {
  252. prompt.overide = { coconihet: 'whatever' }
  253. prompt.get('coconihet', this.callback);
  254. },
  255. "skips prompt and uses overide": function (err, results) {
  256. assert.equal(results.coconihet, 'whatever')
  257. }
  258. }*/
  259. },
  260. "the addProperties() method": {
  261. topic: function () {
  262. prompt.addProperties({}, ['foo', 'bar'], this.callback);
  263. helpers.stdin.write('foo\n');
  264. helpers.stdin.write('bar\n');
  265. },
  266. "should add the properties to the object": function (err, obj) {
  267. assert.isNull(err);
  268. assert.isObject(obj);
  269. assert.equal(obj.foo, 'foo');
  270. assert.equal(obj.bar, 'bar');
  271. }
  272. }
  273. }
  274. }).addBatch({
  275. "When using node-prompt": {
  276. "the history() method": {
  277. "when used inside of a complex property": {
  278. "with correct value(s)": {
  279. topic: function () {
  280. prompt.get([helpers.properties.animal, helpers.properties.sound], this.callback);
  281. helpers.stdin.write('dog\n');
  282. helpers.stdin.write('woof\n');
  283. },
  284. "should respond with the values entered": function (err, result) {
  285. assert.isTrue(!err);
  286. assert.equal(result.animal, 'dog');
  287. assert.equal(result.sound, 'woof');
  288. }
  289. },
  290. "with an incorrect value": {
  291. topic: function () {
  292. prompt.get([helpers.properties.animal, helpers.properties.sound], function () {});
  293. prompt.once('invalid', this.callback.bind(null, null));
  294. helpers.stdin.write('dog\n');
  295. helpers.stdin.write('meow\n');
  296. },
  297. "should prompt for the error": function (ign, property, line) {
  298. assert.equal(property.name, 'sound');
  299. assert.equal(line, 'meow');
  300. }
  301. }
  302. }
  303. }
  304. }
  305. }).addBatch({
  306. "when using node-prompt": {
  307. topic: function () {
  308. //
  309. // Reset the prompt for mock testing
  310. //
  311. prompt.started = false;
  312. prompt.start({
  313. stdin: helpers.stdin,
  314. stdout: helpers.stdout
  315. });
  316. return null;
  317. },
  318. "the get() method": {
  319. topic: function () {
  320. prompt.override = { xyz: 468, abc: 123 }
  321. prompt.get(['xyz', 'abc'], this.callback);
  322. },
  323. "should respond with overrides": function (err, results) {
  324. assert.isNull(err);
  325. assert.deepEqual(results, { xyz: 468, abc: 123 });
  326. }
  327. }
  328. }
  329. }).addBatch({
  330. "when using node-prompt": {
  331. topic: function () {
  332. //
  333. // Reset the prompt for mock testing
  334. //
  335. prompt.started = false;
  336. prompt.start({
  337. stdin: helpers.stdin,
  338. stdout: helpers.stdout
  339. });
  340. return null;
  341. },
  342. "with fancy properties": {
  343. "the get() method": {
  344. topic: function () {
  345. prompt.override = { UVW: 5423, DEF: 64235 }
  346. prompt.get([{
  347. name:'UVW',
  348. message: 'a custom message',
  349. default: 6
  350. },{
  351. name:'DEF',
  352. message: 'a custom message',
  353. default: 6
  354. }], this.callback);
  355. },
  356. "should respond with overrides": function (err, results) {
  357. assert.isNull(err);
  358. assert.deepEqual(results, { UVW: 5423, DEF: 64235 });
  359. }
  360. }
  361. }
  362. }
  363. }).export(module);