|
1 | 1 | const { setup } = require('@ast-grep/nursery')
|
2 | 2 | const languageRegistration = require('./index')
|
| 3 | +const assert = require('node:assert') |
3 | 4 |
|
4 | 5 | setup({
|
5 | 6 | dirname: __dirname,
|
6 | 7 | name: 'yaml',
|
7 | 8 | treeSitterPackage: '@tree-sitter-grammars/tree-sitter-yaml',
|
8 | 9 | languageRegistration,
|
9 | 10 | 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) |
11 | 50 | }
|
12 | 51 | })
|
0 commit comments