PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/test/option_parser.coffee

http://github.com/jashkenas/coffee-script
CoffeeScript | 94 lines | 75 code | 16 blank | 3 comment | 1 complexity | d62616d99cb1fcf48c69a30b67aaeb46 MD5 | raw file
  1. # Option Parser
  2. # -------------
  3. # Ensure that the OptionParser handles arguments correctly.
  4. return unless require?
  5. {OptionParser} = require './../lib/coffeescript/optparse'
  6. flags = [
  7. ['-r', '--required [DIR]', 'desc required']
  8. ['-o', '--optional', 'desc optional']
  9. ['-l', '--list [FILES*]', 'desc list']
  10. ]
  11. banner = '''
  12. banner text
  13. '''
  14. opt = new OptionParser flags, banner
  15. test "basic arguments", ->
  16. args = ['one', 'two', 'three', '-r', 'dir']
  17. result = opt.parse args
  18. arrayEq args, result.arguments
  19. eq undefined, result.required
  20. test "boolean and parameterised options", ->
  21. result = opt.parse ['--optional', '-r', 'folder', 'one', 'two']
  22. ok result.optional
  23. eq 'folder', result.required
  24. arrayEq ['one', 'two'], result.arguments
  25. test "list options", ->
  26. result = opt.parse ['-l', 'one.txt', '-l', 'two.txt', 'three']
  27. arrayEq ['one.txt', 'two.txt'], result.list
  28. arrayEq ['three'], result.arguments
  29. test "-- and interesting combinations", ->
  30. result = opt.parse ['-o','-r','a','-r','b','-o','--','-a','b','--c','d']
  31. arrayEq ['-a', 'b', '--c', 'd'], result.arguments
  32. ok result.optional
  33. eq 'b', result.required
  34. args = ['--','-o','a','-r','c','-o','--','-a','arg0','-b','arg1']
  35. result = opt.parse args
  36. eq undefined, result.optional
  37. eq undefined, result.required
  38. arrayEq args[1..], result.arguments
  39. test "throw if multiple flags try to use the same short or long name", ->
  40. throws -> new OptionParser [
  41. ['-r', '--required [DIR]', 'required']
  42. ['-r', '--long', 'switch']
  43. ]
  44. throws -> new OptionParser [
  45. ['-a', '--append [STR]', 'append']
  46. ['-b', '--append', 'append with -b short opt']
  47. ]
  48. throws -> new OptionParser [
  49. ['--just-long', 'desc']
  50. ['--just-long', 'another desc']
  51. ]
  52. throws -> new OptionParser [
  53. ['-j', '--just-long', 'desc']
  54. ['--just-long', 'another desc']
  55. ]
  56. throws -> new OptionParser [
  57. ['--just-long', 'desc']
  58. ['-j', '--just-long', 'another desc']
  59. ]
  60. test "outputs expected help text", ->
  61. expectedBanner = '''
  62. banner text
  63. -r, --required desc required
  64. -o, --optional desc optional
  65. -l, --list desc list
  66. '''
  67. ok opt.help() is expectedBanner
  68. expected = [
  69. ''
  70. ' -r, --required desc required'
  71. ' -o, --optional desc optional'
  72. ' -l, --list desc list'
  73. ''
  74. ].join('\n')
  75. ok new OptionParser(flags).help() is expected