Browse Source

update tests for verydisco exercises

content-update
Louise Foussat 3 years ago committed by Clément
parent
commit
80512837ca
  1. 89
      js/tests/verydisco-forever_test.mjs
  2. 106
      js/tests/verydisco-reverso_test.mjs
  3. 27
      js/tests/verydisco_test.mjs

89
js/tests/verydisco-forever_test.mjs

@ -8,41 +8,66 @@ import * as cp from 'child_process'
const exec = promisify(cp.exec)
const name = 'verydisco-forever'
export const tests = []
export const setup = async () => {
const dir = tmpdir()
// check if already exists and rm?
await mkdir(`${dir}/${name}`)
const randomLetters = (number) =>
Math.random()
.toString(36)
.substring(0, number)
return { tmpPath: `${dir}/${name}` }
}
const withArgAndExpect = async ({ arg, expect, path, eq, ctx }) => {
const scriptPath = join(resolve(), path)
await exec(`node ${scriptPath} "${arg}"`, { cwd: ctx.tmpPath }) // ${name}.mjs
const out = await readFile(`${ctx.tmpPath}/${name}.txt`, 'utf8').catch((err) =>
err.code === 'ENOENT' ? 'output file not found' : err,
)
await exec(`rm ${name}.txt`, { cwd: ctx.tmpPath })
return eq(out, expect)
export const setup = async ({ path }) => {
const run = async (word) => {
await exec(`node ${path} ${word}`)
const fileContent = await readFile('verydisco-forever.txt', 'utf8').catch((err) =>
err.code === 'ENOENT' ? 'output file not found' : err,
)
return { data: fileContent }
}
return { run }
}
tests.push(async ({ path, eq, ctx }) =>
withArgAndExpect({ path, eq, ctx, arg: 'discovery', expect: 'verydisco' }),
)
tests.push(async ({ path, eq, ctx }) =>
withArgAndExpect({ path, eq, ctx, arg: 'kiss cool', expect: 'sski olco' }),
)
tests.push(async ({ path, eq, ctx }) =>
withArgAndExpect({
path,
eq,
ctx,
arg: 'Node is awesome',
expect: 'deNo si omeawes',
}),
)
tests.push(async ({ ctx, eq }) => {
const { data } = await ctx.run(`discovery`)
return eq(data, 'verydisco')
})
tests.push(async ({ ctx, eq }) => {
const { data } = await ctx.run(`"kiss cool"`)
return eq(data, "sski olco")
})
tests.push(async ({ ctx, eq }) => {
const { data } = await ctx.run(`kiss cool`)
return eq(data, "sski")
})
tests.push(async ({ ctx, eq }) => {
const { data } = await ctx.run(`"Node is awesome"`)
return eq(data, "deNo si omeawes")
})
tests.push(async ({ ctx, eq }) => {
const tic = randomLetters(7)
const tac = randomLetters(7)
const { data } = await ctx.run(`"${tic}${tac}"`)
return eq(data, `${tac}${tic}`)
})
tests.push(async ({ ctx, eq }) => {
const ying = randomLetters(8)
const yang = randomLetters(7)
const { data } = await ctx.run(`"${ying}${yang}"`)
return eq(data, `${yang}${ying}`)
})
tests.push(async ({ ctx, eq }) => {
const tic = randomLetters(5)
const tac = randomLetters(5)
const ying = randomLetters(3)
const yang = randomLetters(2)
const { data } = await ctx.run(`"${tic}${tac} ${ying}${yang}"`)
return eq(data, `${tac}${tic} ${yang}${ying}`)
})
Object.freeze(tests)

106
js/tests/verydisco-reverso_test.mjs

@ -1,51 +1,83 @@
import fs from 'fs/promises'
import { mkdir, writeFile } from 'fs/promises'
import { tmpdir } from 'os'
import { join, resolve } from 'path'
const readFile = fs.readFile
const writeFile = fs.writeFile
const mkdir = fs.mkdir
import { join, isAbsolute } from 'path'
import { promisify } from 'util'
import * as cp from 'child_process'
const exec = promisify(cp.exec)
export const tests = []
const name = 'verydisco-reverso'
export const setup = async () => {
const dir = tmpdir()
// check if already exists and rm?
await mkdir(`${dir}/${name}`)
const randomLetters = (number) =>
Math.random()
.toString(36)
.substring(0, number)
export const setup = async ({ path }) => {
const dir = `${tmpdir()}/verydisco-reverso`
await mkdir(dir)
const run = async (cmd) => {
const cmdPath = isAbsolute(cmd || '') ? cmd : join(dir, cmd || '')
const { stdout } = await exec(`node ${path} ${cmdPath}`)
return { tmpPath: `${dir}/${name}` }
return { stdout: stdout.trim() }
}
return { tmpPath: dir, run }
}
const hasContentAndExpect = async ({ content, expect, path, eq, ctx }) => {
const scriptPath = join(resolve(), path)
await writeFile(`${ctx.tmpPath}/${name}.txt`, content, 'utf8')
const { stdout } = await exec(`node ${scriptPath} "${name}.txt"`, { cwd: ctx.tmpPath })
await exec(`rm ${name}.txt`, { cwd: ctx.tmpPath })
tests.push(async ({ path, eq, ctx }) => {
const trueWords = 'kisscool'
await writeFile(`${ctx.tmpPath}/fever.txt`, trueWords, 'utf8')
return eq(stdout.trim(), expect)
}
const { stdout } = await ctx.run(`${ctx.tmpPath}/fever.txt`)
return eq(stdout, 'coolkiss')
})
tests.push(async ({ path, eq, ctx }) => {
const trueWords = 'verydisco'
await writeFile(`${ctx.tmpPath}/fever.txt`, trueWords, 'utf8')
const { stdout } = await ctx.run(`${ctx.tmpPath}/fever.txt`)
return eq(stdout, 'discovery')
})
tests.push(async ({ path, eq, ctx }) => {
const trueWords = 'deNo si omeawes'
await writeFile(`${ctx.tmpPath}/myTruth.txt`, trueWords, 'utf8')
const { stdout } = await ctx.run(`${ctx.tmpPath}/myTruth.txt`)
return eq(stdout, 'Node is awesome')
})
tests.push(async ({ path, eq, ctx }) => {
const recto = randomLetters(3)
const verso = randomLetters(3)
const meaningOfLife = `${recto}${verso}`
await writeFile(`${ctx.tmpPath}/theTruth.txt`, meaningOfLife, 'utf8')
const { stdout } = await ctx.run(`${ctx.tmpPath}/theTruth.txt`)
return eq(stdout, `${verso}${recto}`)
})
tests.push(async ({ path, eq, ctx }) => {
const heads = randomLetters(3)
const tails = randomLetters(4)
const coinFlipping = `${heads}${tails}`
await writeFile(`${ctx.tmpPath}/coins-flipping.txt`, coinFlipping, 'utf8')
const { stdout } = await ctx.run(`${ctx.tmpPath}/coins-flipping.txt`)
return eq(stdout, `${tails}${heads}`)
})
tests.push(async ({ path, eq, ctx }) => {
const ying = randomLetters(8)
const yang = randomLetters(8)
const tic = randomLetters(5)
const tac = randomLetters(6)
const absurd = `${tic}${tac} ${ying}${yang}`
await writeFile(`${ctx.tmpPath}/absurd.txt`, absurd, 'utf8')
tests.push(async ({ path, eq, ctx }) =>
hasContentAndExpect({
path,
eq,
ctx,
content: `deNo si omeawes`,
expect: 'Node is awesome',
}),
)
// tests.push(async ({ path, eq, ctx }) => {
// await hasContentAndExpect({
// path,
// eq,
// ctx,
// content: // newfilename,
// expect: // reverso
// })
// })
const { stdout } = await ctx.run(`${ctx.tmpPath}/absurd.txt`)
return eq(stdout, `${tac}${tic} ${yang}${ying}`)
})
Object.freeze(tests)

27
js/tests/verydisco_test.mjs

@ -4,6 +4,10 @@ import * as cp from 'child_process'
const exec = promisify(cp.exec)
export const tests = []
const randomLetters = (number) =>
Math.random()
.toString(36)
.substring(0, number)
tests.push(async ({ path, eq }) => {
const { stdout } = await exec(`node ${path} discovery`)
@ -24,3 +28,26 @@ tests.push(async ({ path, eq }) => {
const { stdout } = await exec(`node ${path} "Node is awesome"`)
return eq(stdout.trim(), "deNo si omeawes")
})
tests.push(async ({ path, eq }) => {
const tic = randomLetters(7)
const tac = randomLetters(7)
const { stdout } = await exec(`node ${path} "${tic}${tac}"`)
return eq(stdout.trim(), `${tac}${tic}`)
})
tests.push(async ({ path, eq }) => {
const ying = randomLetters(8)
const yang = randomLetters(7)
const { stdout } = await exec(`node ${path} "${ying}${yang}"`)
return eq(stdout.trim(), `${yang}${ying}`)
})
tests.push(async ({ path, eq }) => {
const tic = randomLetters(5)
const tac = randomLetters(5)
const ying = randomLetters(3)
const yang = randomLetters(2)
const { stdout } = await exec(`node ${path} "${tic}${tac} ${ying}${yang}"`)
return eq(stdout.trim(), `${tac}${tic} ${yang}${ying}`)
})

Loading…
Cancel
Save