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

Feature - Support runtime grammar compilation for unit-testing. #647

Open
TekuConcept opened this issue Mar 30, 2024 · 0 comments
Open

Feature - Support runtime grammar compilation for unit-testing. #647

TekuConcept opened this issue Mar 30, 2024 · 0 comments

Comments

@TekuConcept
Copy link

When running unit tests against grammar definitions, I currently do the following:

async function compileGrammarText(text: string) {
    return new Promise<CompiledRules>((res, rej) => {
        const proc = exec('npx nearleyc -q', (err, stdout, stderr) => {
            if (err) rej(stderr)

            const window = { grammar: null }
            Function("window", stdout)(window)
            res(window.grammar)
        })

        proc.stdin.write(text)
        proc.stdin.end()
    })
}

describe('DEFINITION_NAME', () => {
    let parser: Parser
    let initState: any

    before(async () => {
        const grammar = await Utils.compileGrammarText(`
            @include "${__dirname}/grammar.ne"
            input -> DEFINITION_NAME {% id %}
        `)
        parser = new Parser(Grammar.fromCompiled(grammar))
        initState = parser.save()
    })

    beforeEach(() => parser.restore(initState))

    tests.forEach(test => {
        it(`should parse "${test.value}"`, () => {
            parser.feed(test.value)
            expect(parser.results).to.deep.equal(test.expected)
        })
    })
})

For 500 or so unit tests, this can take about 30 seconds or more to run - most of that time being spent piping data to and from nearleyc. If/when I get some time, I'd like to abstract the compiler logic from nearleyc's IO and expose it as part of the library's API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant