Skip to content

Commit 62738c5

Browse files
feat: extract common test logic to nursery
1 parent f8ccecc commit 62738c5

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

packages/toml/nursery.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
const { setup } = require('@ast-grep/nursery')
22
const toml = require('./index')
3+
const assert = require('node:assert')
34

45
setup({
56
name: 'toml',
67
packageName: '@tree-sitter-grammars/tree-sitter-toml',
7-
testConfig: {
8-
languageRegistration: toml,
9-
code: `
10-
$NAME = "toml"
11-
`
8+
languageRegistration: toml,
9+
testRunner: (parse) => {
10+
const sg = parse('name = "Tom"')
11+
const root = sg.root()
12+
const pair = root.find('$NAME = "Tom"')
13+
assert.equal(pair.kind(), 'pair')
1214
}
1315
})

scripts/nursery/index.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,25 @@ async function copySrc(packageName) {
88
await cp(src, 'src', { recursive: true })
99
}
1010

11-
function test(name, lang, code) {
12-
registerDynamicLanguage({ [name]: lang })
13-
const tree = parse(name, code)
14-
const root = tree.root()
15-
const node = root.find('$NAME = "toml"')
16-
console.log(node.kind())
11+
function test(setupConfig) {
12+
const { name, languageRegistration, testRunner } = setupConfig
13+
registerDynamicLanguage({ [name]: languageRegistration })
14+
testRunner((code) => parse(name, code))
1715
}
1816

1917
/** Setup ast-grep/lang package's pre-release and
2018
* @param {Object} setupConfig
21-
* @param {string} setupConfig.packageName
2219
* @param {string} setupConfig.name
23-
* @param {Object} setupConfig.testConfig
24-
* @param {Object} setupConfig.testConfig.languageRegistration
25-
* @param {string} setupConfig.testConfig.code
20+
* @param {string} setupConfig.packageName
21+
* @param {Function} setupConfig.testRunner
2622
* @returns {void}
2723
*/
2824
function setup(setupConfig){
2925
const arg = process.argv[2]
3026
if (arg === 'copy') {
3127
copySrc(setupConfig.packageName)
3228
} else if (arg === 'test') {
33-
const testConfig = setupConfig.testConfig
34-
test(setupConfig.name, testConfig.languageRegistration, testConfig.code)
29+
test(setupConfig)
3530
}
3631
}
3732

0 commit comments

Comments
 (0)