diff --git a/examples/init-examples/existing-folder/package.json b/examples/init-examples/existing-folder/package.json index ef8cb6a..94b9f99 100644 --- a/examples/init-examples/existing-folder/package.json +++ b/examples/init-examples/existing-folder/package.json @@ -2,6 +2,6 @@ "name": "existing-folder", "version": "1.0.0", "wollokVersion": "4.0.0", - "author": "palumbon", + "author": "ivanjawerbaum", "license": "ISC" } diff --git a/examples/test-examples/normal-case/test-one.wtest b/examples/test-examples/normal-case/test-one.wtest index fecd1a4..0a802ab 100644 --- a/examples/test-examples/normal-case/test-one.wtest +++ b/examples/test-examples/normal-case/test-one.wtest @@ -9,4 +9,8 @@ describe "this describe" { assert.equals(2, a) } + test "another test with longer name" { + assert.equals("aaa", "aaa") + } + } \ No newline at end of file diff --git a/src/commands/test.ts b/src/commands/test.ts index a0d8a28..2cc3d8c 100644 --- a/src/commands/test.ts +++ b/src/commands/test.ts @@ -1,10 +1,11 @@ import { bold } from 'chalk' import { time, timeEnd } from 'console' import logger from 'loglevel' -import { Entity, Environment, Node, Test, is, match, when, WRENatives as natives, interpret } from 'wollok-ts' +import { Entity, Environment, Node, Test, is, match, when, WRENatives as natives, interpret, Describe } from 'wollok-ts' import { buildEnvironmentForProject, failureDescription, successDescription, valueDescription, validateEnvironment, handleError, ENTER, stackTrace, buildEnvironmentIcon, testIcon } from '../utils' import { logger as fileLogger } from '../logger' import { TimeMeasurer } from '../time-measurer' +import { Package } from 'wollok-ts' const { log } = console @@ -24,15 +25,33 @@ export function sanitize(value?: string): string | undefined { return value?.replaceAll('"', '') } -export function getTarget(environment: Environment, filter: string | undefined, { file, describe, test }: Options): Test[] { - const fqnByOptionalParameters = [file, describe, test].filter(Boolean).join('.') - const filterTest = sanitize(filter) ?? fqnByOptionalParameters ?? '' - const possibleTargets = environment.descendants.filter(is(Test)) +export function getTarget(environment: Environment, filter: string | undefined, options: Options): Test[] { + const possibleTargets = getBaseNode(environment, filter, options).descendants.filter(getTestFilter(filter, options)) const onlyTarget = possibleTargets.find((test: Test) => test.isOnly) const testMatches = (filter: string) => (test: Test) => !filter || sanitize(test.fullyQualifiedName)!.includes(filter) + const filterTest = sanitize(filter) ?? '' return onlyTarget ? [onlyTarget] : possibleTargets.filter(testMatches(filterTest)) } +function getBaseNode(environment: Environment, filter: string | undefined, options: Options): Environment | Package | Describe { + if (filter) return environment + + const { file, describe } = options + let nodeToFilter: Environment | Package | Describe = environment + if (file) { + nodeToFilter = nodeToFilter.descendants.find(node => node.is(Package) && node.name === file) as Package | undefined ?? nodeToFilter + } + if (describe) { + nodeToFilter = nodeToFilter.descendants.find(node => node.is(Describe) && node.name === `"${describe}"`) as Describe | undefined ?? nodeToFilter + } + return nodeToFilter +} + +function getTestFilter(filter: string | undefined, options: Options): (node: Node) => node is Test { + return filter || !options.test ? + is(Test) : + (node: Node): node is Test => node.is(Test) && node.name === `"${options.test}"` +} export function tabulationForNode({ fullyQualifiedName }: { fullyQualifiedName: string }): string { return ' '.repeat(fullyQualifiedName.split('.').length - 1) } diff --git a/test/test.test.ts b/test/test.test.ts index 02fa1a2..bcc552a 100644 --- a/test/test.test.ts +++ b/test/test.test.ts @@ -30,116 +30,127 @@ describe('Test', () => { environment = await buildEnvironmentForProject(projectPath) }) - it('should filter by test using filter option', () => { - const tests = getTarget(environment, 'another test', emptyOptions) - expect(tests.length).to.equal(1) - expect(tests[0].name).to.equal('"another test"') - }) + describe('using filter option', () => { + it('should filter by test using filter option', () => { + const tests = getTarget(environment, 'another test', emptyOptions) + expect(tests.length).to.equal(2) + expect(tests[0].name).to.equal('"another test"') + expect(tests[1].name).to.equal('"another test with longer name"') + }) - it('should filter by test using filter option - case insensitive', () => { - const tests = getTarget(environment, 'aNothEr Test', emptyOptions) - expect(tests.length).to.equal(0) - }) + it('should filter by test using filter option - case insensitive', () => { + const tests = getTarget(environment, 'aNothEr Test', emptyOptions) + expect(tests.length).to.equal(0) + }) - it('should filter by test using test option', () => { - const tests = getTarget(environment, undefined, { - ...emptyOptions, - test: 'another test', + it('should filter by describe using filter option', () => { + const tests = getTarget(environment, 'second describe', emptyOptions) + expect(tests.length).to.equal(2) + expect(tests[0].name).to.equal('"second test"') + expect(tests[1].name).to.equal('"another second test"') }) - expect(tests.length).to.equal(1) - expect(tests[0].name).to.equal('"another test"') - }) - it('should filter by test using test option - case insensitive', () => { - const tests = getTarget(environment, undefined, { - ...emptyOptions, - test: 'aNother Test', + it('should filter by describe using filter option - case insensitive', () => { + const tests = getTarget(environment, 'SeCOND describe', emptyOptions) + expect(tests.length).to.equal(0) }) - expect(tests.length).to.equal(0) - }) - it('should filter by describe using filter option', () => { - const tests = getTarget(environment, 'second describe', emptyOptions) - expect(tests.length).to.equal(2) - expect(tests[0].name).to.equal('"second test"') - expect(tests[1].name).to.equal('"another second test"') - }) + it('should filter by file using filter option', () => { + const tests = getTarget(environment, 'test-one', emptyOptions) + expect(tests.length).to.equal(3) + expect(tests[0].name).to.equal('"a test"') + expect(tests[1].name).to.equal('"another test"') + expect(tests[2].name).to.equal('"another test with longer name"') + }) - it('should filter by describe using filter option - case insensitive', () => { - const tests = getTarget(environment, 'SeCOND describe', emptyOptions) - expect(tests.length).to.equal(0) - }) + it('should filter by file & describe using filter option', () => { + const tests = getTarget(environment, 'test-one.this describe', emptyOptions) + expect(tests.length).to.equal(3) + expect(tests[0].name).to.equal('"a test"') + expect(tests[1].name).to.equal('"another test"') + expect(tests[2].name).to.equal('"another test with longer name"') + }) - it('should filter by describe using describe option', () => { - const tests = getTarget(environment, undefined, { - ...emptyOptions, - describe: 'second describe', + it('should filter by file using filter option', () => { + const tests = getTarget(environment, 'test-one.this describe.another test', emptyOptions) + expect(tests.length).to.equal(2) + expect(tests[0].name).to.equal('"another test"') + expect(tests[1].name).to.equal('"another test with longer name"') }) - expect(tests.length).to.equal(2) - expect(tests[0].name).to.equal('"second test"') - expect(tests[1].name).to.equal('"another second test"') + }) - it('should filter by describe & test using describe & test option', () => { - const tests = getTarget(environment, undefined, { - ...emptyOptions, - describe: 'second describe', - test: 'another second test', + describe('with file/describe/test options', () => { + it('should filter by test using test option', () => { + const tests = getTarget(environment, undefined, { + ...emptyOptions, + test: 'another test', + }) + expect(tests.length).to.equal(1) + expect(tests[0].name).to.equal('"another test"') }) - expect(tests.length).to.equal(1) - expect(tests[0].name).to.equal('"another second test"') - }) - it('should filter by file using filter option', () => { - const tests = getTarget(environment, 'test-one', emptyOptions) - expect(tests.length).to.equal(2) - expect(tests[0].name).to.equal('"a test"') - expect(tests[1].name).to.equal('"another test"') - }) + it('should filter by test using test option - case sensitive', () => { + const tests = getTarget(environment, undefined, { + ...emptyOptions, + test: 'aNother Test', + }) + expect(tests.length).to.equal(0) + }) - it('should filter by file using file option', () => { - const tests = getTarget(environment, undefined, { - ...emptyOptions, - file: 'test-one', + it('should filter by describe using describe option', () => { + const tests = getTarget(environment, undefined, { + ...emptyOptions, + describe: 'second describe', + }) + expect(tests.length).to.equal(2) + expect(tests[0].name).to.equal('"second test"') + expect(tests[1].name).to.equal('"another second test"') }) - expect(tests.length).to.equal(2) - expect(tests[0].name).to.equal('"a test"') - expect(tests[1].name).to.equal('"another test"') - }) - it('should filter by file & describe using file & describe option', () => { - const tests = getTarget(environment, undefined, { - ...emptyOptions, - file: 'test-one', - describe: 'this describe', + it('should filter by describe & test using describe & test option', () => { + const tests = getTarget(environment, undefined, { + ...emptyOptions, + describe: 'second describe', + test: 'another second test', + }) + expect(tests.length).to.equal(1) + expect(tests[0].name).to.equal('"another second test"') }) - expect(tests.length).to.equal(2) - expect(tests[0].name).to.equal('"a test"') - expect(tests[1].name).to.equal('"another test"') - }) - it('should filter by file & describe using filter option', () => { - const tests = getTarget(environment, 'test-one.this describe', emptyOptions) - expect(tests.length).to.equal(2) - expect(tests[0].name).to.equal('"a test"') - expect(tests[1].name).to.equal('"another test"') - }) + it('should filter by file using file option', () => { + const tests = getTarget(environment, undefined, { + ...emptyOptions, + file: 'test-one', + }) + expect(tests.length).to.equal(3) + expect(tests[0].name).to.equal('"a test"') + expect(tests[1].name).to.equal('"another test"') + expect(tests[2].name).to.equal('"another test with longer name"') + }) - it('should filter by file & describe & test using file & describe & test option', () => { - const tests = getTarget(environment, undefined, { - ...emptyOptions, - file: 'test-one', - describe: 'this describe', - test: 'another test', + it('should filter by file & describe using file & describe option', () => { + const tests = getTarget(environment, undefined, { + ...emptyOptions, + file: 'test-one', + describe: 'this describe', + }) + expect(tests.length).to.equal(3) + expect(tests[0].name).to.equal('"a test"') + expect(tests[1].name).to.equal('"another test"') + expect(tests[2].name).to.equal('"another test with longer name"') }) - expect(tests.length).to.equal(1) - expect(tests[0].name).to.equal('"another test"') - }) - it('should filter by file using filter option', () => { - const tests = getTarget(environment, 'test-one.this describe.another test', emptyOptions) - expect(tests.length).to.equal(1) - expect(tests[0].name).to.equal('"another test"') + it('should filter by file & describe & test using file & describe & test option', () => { + const tests = getTarget(environment, undefined, { + ...emptyOptions, + file: 'test-one', + describe: 'this describe', + test: 'another test', + }) + expect(tests.length).to.equal(1) + expect(tests[0].name).to.equal('"another test"') + }) }) }) @@ -283,23 +294,23 @@ describe('Test', () => { }) expect(processExitSpy.callCount).to.equal(0) - expect(spyCalledWithSubstring(loggerInfoSpy, 'Running 2 tests')).to.be.true - expect(spyCalledWithSubstring(loggerInfoSpy, '2 passing')).to.be.true + expect(spyCalledWithSubstring(loggerInfoSpy, 'Running 3 tests')).to.be.true + expect(spyCalledWithSubstring(loggerInfoSpy, '3 passing')).to.be.true expect(spyCalledWithSubstring(loggerInfoSpy, '0 failing')).to.be.false // old version expect(fileLoggerInfoSpy.calledOnce).to.be.true - expect(fileLoggerInfoSpy.firstCall.firstArg.result).to.deep.equal({ ok: 2, failed: 0 }) + expect(fileLoggerInfoSpy.firstCall.firstArg.result).to.deep.equal({ ok: 3, failed: 0 }) }) it('returns exit code 2 if one or more tests fail', async () => { await test(undefined, emptyOptions) expect(processExitSpy.calledWith(2)).to.be.true - expect(spyCalledWithSubstring(loggerInfoSpy, 'Running 5 tests')).to.be.true - expect(spyCalledWithSubstring(loggerInfoSpy, '4 passing')).to.be.true + expect(spyCalledWithSubstring(loggerInfoSpy, 'Running 6 tests')).to.be.true + expect(spyCalledWithSubstring(loggerInfoSpy, '5 passing')).to.be.true expect(spyCalledWithSubstring(loggerInfoSpy, '1 failing')).to.be.true expect(fileLoggerInfoSpy.calledOnce).to.be.true const fileLoggerArg = fileLoggerInfoSpy.firstCall.firstArg - expect(fileLoggerArg.result).to.deep.equal({ ok: 4, failed: 1 }) + expect(fileLoggerArg.result).to.deep.equal({ ok: 5, failed: 1 }) expect(fileLoggerArg.failures.length).to.equal(1) })