voyage/tests/lib/arg-parser-tokenize.test.mjs
2026-10-28 15:18:55 +01:00

19 lines
1.1 KiB
JavaScript

import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { parseArgs, tokenize } from '../../lib/parsers/arg-parser.mjs';
const values = (text) => tokenize(text).map((t) => t.value);
test('[TOK] a quoted value never swallows a later flag, and an apostrophe inside a word keeps its value', () => {
const text = '--project "my dir"/x --since "HEAD~1"';
assert.equal(parseArgs(text, 'trekreview').flags['--since'], 'HEAD~1',
'the quote after `dir` is followed by `/`, so it does not close; the search stops at the quote that starts `"HEAD~1"`');
assert.deepEqual(values(text), ['--project', '"my', 'dir"/x', '--since', 'HEAD~1']);
// Control: an apostrophe inside a word in single quotes keeps one value.
assert.deepEqual(values(`'Fix the user's login'`), ["Fix the user's login"]);
// Control: two adjacent quoted words stay two values.
assert.deepEqual(values(`"a" "b"`), ['a', 'b']);
// Control: the other kind of quote inside a quoted value does not split it.
assert.deepEqual(values(`"say 'hi' now"`), ["say 'hi' now"]);
});