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