Skip to content

Commit 1a04f81

Browse files
committed
add tests
1 parent 079f9ee commit 1a04f81

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

packages/yaml/nursery.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
11
const { setup } = require('@ast-grep/nursery')
22
const languageRegistration = require('./index')
3+
const assert = require('node:assert')
34

45
setup({
56
dirname: __dirname,
67
name: 'yaml',
78
treeSitterPackage: '@tree-sitter-grammars/tree-sitter-yaml',
89
languageRegistration,
910
testRunner: (parse) => {
10-
// add test here
11+
// Test simple value matching
12+
const sg1 = parse('123')
13+
const root1 = sg1.root()
14+
const node1 = root1.find('123')
15+
assert.equal(node1.kind(), 'integer_scalar')
16+
17+
// Test pattern matching with key-value pairs
18+
const sg2 = parse('foo: 123')
19+
const root2 = sg2.root()
20+
const node2 = root2.find('foo: $BAR')
21+
assert.equal(node2.kind(), 'block_mapping_pair')
22+
assert.equal(node2.find('$BAR').text(), '123')
23+
24+
// Test non-matching pattern
25+
const node3 = root2.find('bar: $BAR')
26+
assert.equal(node3, null)
27+
28+
// Test nested structures
29+
const sg3 = parse(`
30+
foo:
31+
- item1
32+
- item2
33+
`)
34+
const root3 = sg3.root()
35+
const node4 = root3.find('foo: $$$')
36+
assert.equal(node4.kind(), 'block_mapping_pair')
37+
38+
// Test replacement functionality
39+
const source = `
40+
key: value
41+
list:
42+
- item1
43+
- item2
44+
`
45+
const sg4 = parse(source)
46+
const root4 = sg4.root()
47+
const node5 = root4.find('$KEY: value')
48+
const replaced = node5.replace('value: $KEY')
49+
assert.equal(replaced.includes('value: key'), true)
1150
}
1251
})

0 commit comments

Comments
 (0)