|
| 1 | +/* global describe, it */ |
| 2 | +import { expect, should } from 'chai' |
| 3 | +import { tokenizeArgString } from '../lib/tokenize-arg-string' |
| 4 | + |
| 5 | +should() |
| 6 | + |
| 7 | +describe('TokenizeArgString', function () { |
| 8 | + it('handles unquoted string', function () { |
| 9 | + const args = tokenizeArgString('--foo 99') |
| 10 | + args[0].should.equal('--foo') |
| 11 | + args[1].should.equal('99') |
| 12 | + }) |
| 13 | + |
| 14 | + it('handles unquoted numbers', function () { |
| 15 | + const args = tokenizeArgString(['--foo', 9]) |
| 16 | + args[0].should.equal('--foo') |
| 17 | + args[1].should.equal('9') |
| 18 | + }) |
| 19 | + |
| 20 | + it('handles quoted string with no spaces', function () { |
| 21 | + const args = tokenizeArgString("--foo 'hello'") |
| 22 | + args[0].should.equal('--foo') |
| 23 | + args[1].should.equal("'hello'") |
| 24 | + }) |
| 25 | + |
| 26 | + it('handles single quoted string with spaces', function () { |
| 27 | + const args = tokenizeArgString("--foo 'hello world' --bar='foo bar'") |
| 28 | + args[0].should.equal('--foo') |
| 29 | + args[1].should.equal("'hello world'") |
| 30 | + args[2].should.equal("--bar='foo bar'") |
| 31 | + }) |
| 32 | + |
| 33 | + it('handles double quoted string with spaces', function () { |
| 34 | + const args = tokenizeArgString('--foo "hello world" --bar="foo bar"') |
| 35 | + args[0].should.equal('--foo') |
| 36 | + args[1].should.equal('"hello world"') |
| 37 | + args[2].should.equal('--bar="foo bar"') |
| 38 | + }) |
| 39 | + |
| 40 | + it('handles single quoted empty string', function () { |
| 41 | + const args = tokenizeArgString('--foo \'\' --bar=\'\'') |
| 42 | + args[0].should.equal('--foo') |
| 43 | + args[1].should.equal("''") |
| 44 | + args[2].should.equal("--bar=''") |
| 45 | + }) |
| 46 | + |
| 47 | + it('handles double quoted empty string', function () { |
| 48 | + const args = tokenizeArgString('--foo "" --bar=""') |
| 49 | + args[0].should.equal('--foo') |
| 50 | + args[1].should.equal('""') |
| 51 | + args[2].should.equal('--bar=""') |
| 52 | + }) |
| 53 | + |
| 54 | + it('handles quoted string with embedded quotes', function () { |
| 55 | + var args = tokenizeArgString('--foo "hello \'world\'" --bar=\'foo "bar"\'') |
| 56 | + args[0].should.equal('--foo') |
| 57 | + args[1].should.equal('"hello \'world\'"') |
| 58 | + args[2].should.equal('--bar=\'foo "bar"\'') |
| 59 | + }) |
| 60 | + |
| 61 | + // https://github.com/yargs/yargs-parser/pull/100 |
| 62 | + // https://github.com/yargs/yargs-parser/pull/106 |
| 63 | + it('ignores unneeded spaces', function () { |
| 64 | + const args = tokenizeArgString(' foo bar "foo bar" ') |
| 65 | + args[0].should.equal('foo') |
| 66 | + expect(args[1]).equal('bar') |
| 67 | + expect(args[2]).equal('"foo bar"') |
| 68 | + }) |
| 69 | + |
| 70 | + it('handles boolean options', function () { |
| 71 | + const args = tokenizeArgString('--foo -bar') |
| 72 | + expect(args[0]).to.equal(('--foo')) |
| 73 | + expect(args[1]).to.equal(('-bar')) |
| 74 | + }) |
| 75 | + |
| 76 | + it('handles empty string', function () { |
| 77 | + const args = tokenizeArgString('') |
| 78 | + expect(args.length).to.equal(0) |
| 79 | + }) |
| 80 | + |
| 81 | + it('handles array with unquoted string', function () { |
| 82 | + const args = tokenizeArgString(['--foo', '99']) |
| 83 | + args[0].should.equal('--foo') |
| 84 | + args[1].should.equal('99') |
| 85 | + }) |
| 86 | + |
| 87 | + it('handles array with quoted string with no spaces', function () { |
| 88 | + const args = tokenizeArgString(['--foo', "'hello'"]) |
| 89 | + args[0].should.equal('--foo') |
| 90 | + args[1].should.equal("'hello'") |
| 91 | + }) |
| 92 | + |
| 93 | + it('handles array with single quoted string with spaces', function () { |
| 94 | + const args = tokenizeArgString(['--foo', "'hello world'", "--bar='foo bar'"]) |
| 95 | + args[0].should.equal('--foo') |
| 96 | + args[1].should.equal("'hello world'") |
| 97 | + args[2].should.equal("--bar='foo bar'") |
| 98 | + }) |
| 99 | + |
| 100 | + it('handles array with double quoted string with spaces', function () { |
| 101 | + const args = tokenizeArgString(['--foo', '"hello world"', '--bar="foo bar"']) |
| 102 | + args[0].should.equal('--foo') |
| 103 | + args[1].should.equal('"hello world"') |
| 104 | + args[2].should.equal('--bar="foo bar"') |
| 105 | + }) |
| 106 | + |
| 107 | + it('handles array with single quoted empty string', function () { |
| 108 | + const args = tokenizeArgString(['--foo', "''", "--bar=''"]) |
| 109 | + args[0].should.equal('--foo') |
| 110 | + args[1].should.equal("''") |
| 111 | + args[2].should.equal("--bar=''") |
| 112 | + }) |
| 113 | + |
| 114 | + it('handles array with double quoted empty string', function () { |
| 115 | + const args = tokenizeArgString(['--foo', '""', '--bar=""']) |
| 116 | + args[0].should.equal('--foo') |
| 117 | + args[1].should.equal('""') |
| 118 | + args[2].should.equal('--bar=""') |
| 119 | + }) |
| 120 | + |
| 121 | + it('handles array with quoted string with embedded quotes', function () { |
| 122 | + var args = tokenizeArgString(['--foo', '"hello \'world\'"', '--bar=\'foo "bar"\'']) |
| 123 | + args[0].should.equal('--foo') |
| 124 | + args[1].should.equal('"hello \'world\'"') |
| 125 | + args[2].should.equal('--bar=\'foo "bar"\'') |
| 126 | + }) |
| 127 | + |
| 128 | + it('handles array with boolean options', function () { |
| 129 | + const args = tokenizeArgString(['--foo', '-bar']) |
| 130 | + expect(args[0]).to.equal('--foo') |
| 131 | + expect(args[1]).to.equal('-bar') |
| 132 | + }) |
| 133 | +}) |
0 commit comments