mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
test(cli): add commands/run/test
This commit is contained in:
93
cli/commands/run/__test__/test.test.ts
Normal file
93
cli/commands/run/__test__/test.test.ts
Normal file
@ -0,0 +1,93 @@
|
||||
import { PassThrough } from 'node:stream'
|
||||
|
||||
import chalk from 'chalk'
|
||||
|
||||
import { cli } from '../../../cli'
|
||||
import { Test } from '../../../services/Test'
|
||||
|
||||
const input = ['run', 'test']
|
||||
const challenge = 'hello-world'
|
||||
const language = 'c'
|
||||
const solution = 'function'
|
||||
const inputChallenge = `--challenge=${challenge}`
|
||||
const inputLanguage = `--language=${language}`
|
||||
const inputSolution = `--solution=${solution}`
|
||||
|
||||
describe('programming-challenges run test', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it('succeeds', async () => {
|
||||
console.log = jest.fn()
|
||||
const stream = new PassThrough()
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputChallenge, inputSolution, inputLanguage],
|
||||
{
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
stderr: stream
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(0)
|
||||
expect(console.log).toHaveBeenNthCalledWith(2, `${chalk.bold('Name:')} ${challenge}/${language}/${solution}\n`)
|
||||
expect(console.log).toHaveBeenNthCalledWith(4, `${chalk.bold('Tests:')} ${chalk.bold.green('3 passed')}, 3 total`)
|
||||
expect(console.log).toHaveBeenNthCalledWith(6, Test.SUCCESS_MESSAGE)
|
||||
})
|
||||
|
||||
it("fails with solution that doesn't exist", async () => {
|
||||
console.error = jest.fn()
|
||||
const stream = new PassThrough()
|
||||
const invalidSolution = 'invalid'
|
||||
const inputInvalidSolution = `--solution=${invalidSolution}`
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputChallenge, inputInvalidSolution, inputLanguage],
|
||||
{
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
stderr: stream
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
chalk.bold.red('Error:') + ' The solution was not found.'
|
||||
)
|
||||
})
|
||||
|
||||
it('fails with invalid language', async () => {
|
||||
console.error = jest.fn()
|
||||
const stream = new PassThrough()
|
||||
const invalidLanguage = 'invalid'
|
||||
const inputInvalidLanguage = `--language=${invalidLanguage}`
|
||||
const exitCode = await cli.run(
|
||||
[...input, inputChallenge, inputSolution, inputInvalidLanguage],
|
||||
{
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
stderr: stream
|
||||
}
|
||||
)
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
chalk.bold.red('Error:') + ' This programming language is not supported yet.'
|
||||
)
|
||||
})
|
||||
|
||||
it('fails without options', async () => {
|
||||
console.error = jest.fn()
|
||||
const stream = new PassThrough()
|
||||
const exitCode = await cli.run(input, {
|
||||
stdin: process.stdin,
|
||||
stdout: stream,
|
||||
stderr: stream
|
||||
})
|
||||
stream.end()
|
||||
expect(exitCode).toEqual(1)
|
||||
expect(console.error).toHaveBeenCalledWith(
|
||||
`${chalk.bold.red('Error:')} You must specify all the options (\`--challenge\`, \`--solution\`, \`--language\`).`
|
||||
)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user