forked from launchql/libpg-query-node
-
Notifications
You must be signed in to change notification settings - Fork 5
/
generateTests.ts
37 lines (31 loc) · 990 Bytes
/
generateTests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import fs from 'node:fs'
import path from 'node:path'
const testDir = 'libpg_query/test/sql/postgres_regress'
const testFiles = fs.readdirSync(testDir)
const cwd = process.cwd()
const renderTest = (testFile: string) => `
import { describe, test, expect } from "vitest"
import { parseTestFile } from "../parseTestFile"
import { parseQuery } from "../../index"
describe("${testFile}", () => {
const tests = parseTestFile("${path.relative(cwd, path.join(testDir, testFile))}")
for (const { line, stmt } of tests) {
test("line " + line, async () => {
try {
const ast = await parseQuery(stmt)
expect([stmt, ast]).toMatchSnapshot()
} catch (error) {
expect([stmt, error]).toMatchSnapshot()
}
})
}
})
`
const outDir = 'test/postgres_regress'
fs.mkdirSync(outDir, { recursive: true })
for (const testFile of testFiles) {
fs.writeFileSync(
path.join(outDir, testFile.replace(/\.sql$/, '.test.ts')),
renderTest(testFile),
)
}