test_split.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* eslint-env mocha */
  2. 'use strict'
  3. const { assert } = require('chai')
  4. const shlex = require('../shlex')
  5. describe('shlex.split()', function () {
  6. // The original test data set was from shellwords, by Hartmut Goebel
  7. const posixTestcases = [
  8. ['x', 'x'],
  9. ['foo bar', 'foo', 'bar'],
  10. [' foo bar', 'foo', 'bar'],
  11. [' foo bar ', 'foo', 'bar'],
  12. ['foo bar bla fasel', 'foo', 'bar', 'bla', 'fasel'],
  13. ['x y z xxxx', 'x', 'y', 'z', 'xxxx'],
  14. ['\\x bar', 'x', 'bar'],
  15. ['\\ x bar', ' x', 'bar'],
  16. ['\\ bar', ' bar'],
  17. ['foo \\x bar', 'foo', 'x', 'bar'],
  18. ['foo \\ x bar', 'foo', ' x', 'bar'],
  19. ['foo \\ bar', 'foo', ' bar'],
  20. ['foo "bar" bla', 'foo', 'bar', 'bla'],
  21. ['"foo" "bar" "bla"', 'foo', 'bar', 'bla'],
  22. ['"foo" bar "bla"', 'foo', 'bar', 'bla'],
  23. ['"foo" bar bla', 'foo', 'bar', 'bla'],
  24. ["foo 'bar' bla", 'foo', 'bar', 'bla'],
  25. ["'foo' 'bar' 'bla'", 'foo', 'bar', 'bla'],
  26. ["'foo' bar 'bla'", 'foo', 'bar', 'bla'],
  27. ["'foo' bar bla", 'foo', 'bar', 'bla'],
  28. ['blurb foo"bar"bar"fasel" baz', 'blurb', 'foobarbarfasel', 'baz'],
  29. ["blurb foo'bar'bar'fasel' baz", 'blurb', 'foobarbarfasel', 'baz'],
  30. ['""', ''],
  31. ["''", ''],
  32. ['foo "" bar', 'foo', '', 'bar'],
  33. ["foo '' bar", 'foo', '', 'bar'],
  34. ['foo "" "" "" bar', 'foo', '', '', '', 'bar'],
  35. ["foo '' '' '' bar", 'foo', '', '', '', 'bar'],
  36. ['\\"', '"'],
  37. ['"\\""', '"'],
  38. ['"foo\\ bar"', 'foo\\ bar'],
  39. ['"foo\\\\ bar"', 'foo\\ bar'],
  40. ['"foo\\\\ bar\\""', 'foo\\ bar"'],
  41. ['"foo\\\\" bar\\"', 'foo\\', 'bar"'],
  42. ['"foo\\\\ bar\\" dfadf"', 'foo\\ bar" dfadf'],
  43. ['"foo\\\\\\ bar\\" dfadf"', 'foo\\\\ bar" dfadf'],
  44. ['"foo\\\\\\x bar\\" dfadf"', 'foo\\\\x bar" dfadf'],
  45. ['"foo\\x bar\\" dfadf"', 'foo\\x bar" dfadf'],
  46. ["\\'", "'"],
  47. ["'foo\\ bar'", 'foo\\ bar'],
  48. ["'foo\\\\ bar'", 'foo\\\\ bar'],
  49. ["\"foo\\\\\\x bar\\\" df'a\\ 'df\"", "foo\\\\x bar\" df'a\\ 'df"],
  50. ['\\"foo', '"foo'],
  51. ['\\"foo\\x', '"foox'],
  52. ['"foo\\x"', 'foo\\x'],
  53. ['"foo\\ "', 'foo\\ '],
  54. ['foo\\ xx', 'foo xx'],
  55. ['foo\\ x\\x', 'foo xx'],
  56. ['foo\\ x\\x\\"', 'foo xx"'],
  57. ['"foo\\ x\\x"', 'foo\\ x\\x'],
  58. ['"foo\\ x\\x\\\\"', 'foo\\ x\\x\\'],
  59. ['"foo\\ x\\x\\\\""foobar"', 'foo\\ x\\x\\foobar'],
  60. ["\"foo\\ x\\x\\\\\"\\'\"foobar\"", "foo\\ x\\x\\'foobar"],
  61. ["\"foo\\ x\\x\\\\\"\\'\"fo'obar\"", "foo\\ x\\x\\'fo'obar"],
  62. ["\"foo\\ x\\x\\\\\"\\'\"fo'obar\" 'don'\\''t'", "foo\\ x\\x\\'fo'obar", "don't"],
  63. ["\"foo\\ x\\x\\\\\"\\'\"fo'obar\" 'don'\\''t' \\\\", "foo\\ x\\x\\'fo'obar", "don't", '\\'],
  64. ["'foo\\ bar'", 'foo\\ bar'],
  65. ["'foo\\\\ bar'", 'foo\\\\ bar'],
  66. ['foo\\ bar', 'foo bar'],
  67. // ["foo#bar\nbaz", "foo", "baz"], // FIXME: Comments are not implemented
  68. [':-) ;-)', ':-)', ';-)'],
  69. ['\u00e1\u00e9\u00ed\u00f3\u00fa', '\u00e1\u00e9\u00ed\u00f3\u00fa'],
  70. ['hello \\\n world', 'hello', 'world']
  71. ]
  72. const ansiCTestcases = [
  73. ['$\'x\'', 'x'], // non-escaped character
  74. ['$\'\\a\'', '\x07'], // alert (bell)
  75. ['$\'\\b\'', '\x08'], // backspace
  76. ['$\'\\e\'', '\x1b'], // escape character
  77. ['$\'\\E\'', '\x1b'], // escape character
  78. ['$\'\\f\'', '\x0c'], // form feed / new page
  79. ['$\'\\n\'', '\x0a'], // newline
  80. ['$\'\\r\'', '\x0d'], // carriage return
  81. ['$\'\\t\'', '\x09'], // horizontal tab
  82. ['$\'\\v\'', '\x0b'], // vertical tab
  83. ['$\'\\\\\'', '\\'], // backslash
  84. ['$\'\\\'\'', '\''], // single quote
  85. ['$\'\\"\'', '"'], // double quote
  86. ['$\'\\?\'', '?'], // question mark
  87. ['$\'\\79\'', '\x07\x39'], // octal + non-octal
  88. ['$\'\\07\'', '\x07'], // octal, zero prefix
  89. ['$\'\\xfx\'', '\x0f\x78'], // hex (one digit) + non-hex
  90. ['$\'\\xffx\'', '\xff\x78'], // hex (two digits) + non-hex
  91. ['$\'\\xxx\'', '\\xxx'], // invalid hex
  92. ['$\'\\u2603\'', '☃'], // unicode character
  93. ['$\'\\U2603\'', '☃'], // unicode character
  94. ['$\'\\ca\'', '\x01'], // control-a character
  95. ['$\'\\cA\'', '\x01'], // control-A character, same as above
  96. ['$\'\\c@\'', '\x00'], // control-@ character: null
  97. ['$\'\\c?\'', '\x7f'], // control-? character: del
  98. ['$\'\\\\x30\'', '\\x30'],
  99. ['x$\'y\'z', 'xyz'],
  100. ['"x"$\'y\'"z"', 'xyz'],
  101. ['$\'x\'"y"$\'z\'', 'xyz'],
  102. ['x"$\'y\'"z', 'x$\'y\'z']
  103. ]
  104. const localeTestcases = [
  105. ['$"x"', 'x'], // non-escaped character
  106. ['$"\\""', '"'], // escaped quotation mark
  107. ['$"\\\\"', '\\'], // escaped escape character
  108. ['$"\\x33"', '\\x33'], // other escape sequences do not work
  109. ['x$"y"z', 'xyz'],
  110. ['"x"$"y""z"', 'xyz'],
  111. ['$"x""y"$"z"', 'xyz'],
  112. ['x"$"y""z', 'x$yz']
  113. ]
  114. it('should split according to POSIX rules', function () {
  115. posixTestcases.forEach(function (test) {
  116. const input = test[0]
  117. const expected = test.slice(1)
  118. assert.deepEqual(shlex.split(input), expected)
  119. })
  120. })
  121. it('should split ANSI C strings', function () {
  122. ansiCTestcases.forEach(function (test) {
  123. const input = test[0]
  124. const expected = test.slice(1)
  125. assert.deepEqual(shlex.split(input), expected)
  126. })
  127. })
  128. it('should split localized strings', function () {
  129. localeTestcases.forEach(function (test) {
  130. const input = test[0]
  131. const expected = test.slice(1)
  132. assert.deepEqual(shlex.split(input), expected)
  133. })
  134. })
  135. })