Skip to content

Commit b39bcb1

Browse files
committed
add tests
1 parent 079f9ee commit b39bcb1

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

packages/yaml/nursery.js

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { setup } = require('@ast-grep/nursery')
2+
const assert = require('node:assert')
23
const languageRegistration = require('./index')
34

45
setup({
@@ -7,6 +8,46 @@ setup({
78
treeSitterPackage: '@tree-sitter-grammars/tree-sitter-yaml',
89
languageRegistration,
910
testRunner: (parse) => {
10-
// add test here
11+
// Based on test_yaml_str
12+
let sg = parse('123')
13+
let node = sg.root().find('123')
14+
assert.ok(node, 'should match simple scalar')
15+
node = sg.root().find("'123'")
16+
assert.ok(!node, 'should not match quoted scalar')
17+
18+
// Based on test_yaml_pattern
19+
sg = parse('foo: 123')
20+
node = sg.root().find('foo: $BAR')
21+
assert.ok(node, 'should match key-value with metavariable')
22+
let match = node.getMatch('BAR')
23+
assert.equal(match.text(), '123')
24+
25+
sg = parse('foo: [1, 2, 3]')
26+
node = sg.root().find('foo: $$$')
27+
assert.ok(node, 'should match key-list with multi-metavariable')
28+
29+
sg = parse(`foo:
30+
- a`)
31+
node = sg.root().find('foo: $BAR')
32+
assert.ok(node, 'should match key-sequence with metavariable')
33+
match = node.getMatch('BAR')
34+
assert.equal(match.kind(), 'block_node') // Updated expected kind
35+
36+
sg = parse('bar: bar')
37+
node = sg.root().find('foo: $BAR')
38+
assert.ok(!node, 'should not match incorrect key')
39+
40+
// Based on test_yaml_replace (testing find, as replace is not directly available)
41+
const SOURCE = `
42+
key: value
43+
list:
44+
- item1
45+
- item2
46+
`
47+
sg = parse(SOURCE)
48+
node = sg.root().find('$KEY: value')
49+
assert.ok(node, 'should find key-value pair for replacement test')
50+
match = node.getMatch('KEY')
51+
assert.equal(match.text(), 'key')
1152
}
1253
})

0 commit comments

Comments
 (0)