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

script to output test cases as JSON files #139

Closed
wants to merge 1 commit into from
Closed
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
94 changes: 94 additions & 0 deletions scripts/generate-test-json-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env node

const fs = require('fs-plus')
const path = require('path')

const enolib = require('enolib')
// const scsLib = require('../lib/standard-clojure-style.js')

const rootDir = path.join(__dirname, '../')

// // returns an Array of all the .eno files in the test_format/ folder
// const enoFilesInTestFormatDir = () => {
// const allFiles = fs.readdirSync(path.join(rootDir, 'test_format/'))
// return allFiles.filter(isEnoFile)
// }

function enoFilesInDir (dir) {
const allFiles = fs.readdirSync(path.join(rootDir, dir))
return allFiles.filter(isEnoFile)
}

let formatTestCases = []

// parses a test .eno file and returns an Array of Objects like { input, expected, name }
const parseTestFile = (txt) => {
const document = enolib.parse(txt)
const sections = document.sections()

const tests = []
sections.forEach((s) => {
tests.push({
filename: s.filename,
name: s._instruction.key,
input: s.field('Input').requiredStringValue(),
expected: s.field('Expected').requiredStringValue()
})
})
return tests
}

enoFilesInDir('test_format/').forEach((f) => {
const testFileContents = fs.readFileSync(path.join(rootDir, 'test_format', f), 'utf8')
const testsInFile = parseTestFile(testFileContents)
const testsWithFilename = testsInFile.map(t => {
t.filename = f
return t
})
formatTestCases = formatTestCases.concat(testsWithFilename)
})

let parserTestCases = []

enoFilesInDir('test_parser/').forEach((f) => {
const testFileContents = fs.readFileSync(path.join(rootDir, 'test_parser', f), 'utf8')
const testsInFile = parseTestFile(testFileContents)
const testsWithFilename = testsInFile.map(t => {
t.filename = f
return t
})
parserTestCases = parserTestCases.concat(testsWithFilename)
})

let parseNsTestCases = []

enoFilesInDir('test_parse_ns/').forEach((f) => {
const testFileContents = fs.readFileSync(path.join(rootDir, 'test_parse_ns', f), 'utf8')
const testsInFile = parseTestFile(testFileContents)
const testsWithFilename = testsInFile.map(t => {
t.filename = f
return t
})
parseNsTestCases = parseNsTestCases.concat(testsWithFilename)
})

fs.writeFileSync('test_cases_json/format_tests.json', JSON.stringify(formatTestCases, null, 2))
fs.writeFileSync('test_cases_json/parser_tests.json', JSON.stringify(parserTestCases, null, 2))
fs.writeFileSync('test_cases_json/parse_ns_tests.json', JSON.stringify(parseNsTestCases, null, 2))

// -----------------------------------------------------------------------------
// Util

function isEnoFile (f) {
return path.extname(f) === '.eno'
}

// function compareTestCases (testCaseA, testCaseB) {
// if (testCaseB.name > testCaseA.name) return -1
// else if (testCaseB.name < testCaseA.name) return 1
// else return 0
// }

// function isString (s) {
// return typeof s === 'string'
// }
Loading