Skip to content

Instantly share code, notes, and snippets.

@erukiti
Last active November 12, 2017 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erukiti/2222af751458e443d395bf1199f3a46b to your computer and use it in GitHub Desktop.
Save erukiti/2222af751458e443d395bf1199f3a46b to your computer and use it in GitHub Desktop.
s2s-handler-pegjs
//@flow
import peg from 'pegjs'
import type { Code } from 'types'
export default function pegjsHandler(code: Code): Code {
if (code === '') {
return ''
}
return peg.generate(code, { output: 'source', format: 'commonjs' })
}
//@flow
import fs from 'fs'
import os from 'os'
import path from 'path'
import handler from '.'
const code = 'start = [a-z]+ {return text()}'
test('hndlerは返還後のコードを返す', () => {
const result = handler(code)
const dir = fs.mkdtempSync(`${os.tmpdir()}${path.sep}`)
const filename = `${dir}/index.js`
fs.writeFileSync(filename, result)
// FIXME: Flowの警告が消せない…
// eslint-disable-next-line global-require, import/no-dynamic-require
const parser = require(dir)
expect(parser.parse('hoge')).toBe('hoge')
})
test('codeが""の場合、""が返ること', () => {
const result = handler('')
expect(result).toBe('')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment