Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test options exact match #141

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/init-examples/existing-folder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "existing-folder",
"version": "1.0.0",
"wollokVersion": "4.0.0",
"author": "palumbon",
"author": "ivanjawerbaum",
"license": "ISC"
}
4 changes: 4 additions & 0 deletions examples/test-examples/normal-case/test-one.wtest
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ describe "this describe" {
assert.equals(2, a)
}

test "another test with longer name" {
assert.equals("aaa", "aaa")
}

}
29 changes: 24 additions & 5 deletions src/commands/test.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
}
Expand Down
201 changes: 106 additions & 95 deletions test/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"')
})
})

})
Expand Down Expand Up @@ -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)
})

Expand Down
Loading