diff --git a/plugins/ultraplan-local/examples/02-real-cli/tests/tally.test.mjs b/plugins/ultraplan-local/examples/02-real-cli/tests/tally.test.mjs index 0b132bb..e01d798 100644 --- a/plugins/ultraplan-local/examples/02-real-cli/tests/tally.test.mjs +++ b/plugins/ultraplan-local/examples/02-real-cli/tests/tally.test.mjs @@ -91,3 +91,37 @@ test('--help: stdout contains "Usage:", exit 0', () => { assert.match(r.stdout, /Usage:/); assert.match(r.stdout, /--ignore-case/); }); + +// --- Tests for --regex / -r mode (added in plan step 4, Spor B B3) --- + +test("--regex 'fo+' counts more matches than literal 'foo' (long form, exit 0)", () => { + const literal = run('foo', SAMPLE); + const regex = run('--regex', 'fo+', SAMPLE); + assert.equal(literal.status, 0); + assert.equal(regex.status, 0); + assert.ok(Number(regex.stdout) >= Number(literal.stdout), + `regex count (${regex.stdout.trim()}) should be >= literal count (${literal.stdout.trim()})`); +}); + +test("-r short form equals --regex long form (same stdout)", () => { + const short = run('-r', 'fo+', SAMPLE); + const long = run('--regex', 'fo+', SAMPLE); + assert.equal(short.status, 0); + assert.equal(long.status, 0); + assert.equal(short.stdout, long.stdout); +}); + +test("--regex '[' exits 2 with stderr 'tally: invalid regex'", () => { + const r = run('--regex', '[', SAMPLE); + assert.equal(r.status, 2); + assert.equal(r.stdout, ''); + assert.match(r.stderr, /^tally: invalid regex/); +}); + +test("--json --regex 'fo+' includes flags.regex === true in output", () => { + const r = run('--json', '--regex', 'fo+', SAMPLE); + assert.equal(r.status, 0); + const parsed = JSON.parse(r.stdout); + assert.equal(parsed.flags.regex, true); + assert.ok(typeof parsed.count === 'number' && parsed.count > 0); +});