-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
TreeSitterParserTests.swift
75 lines (68 loc) · 3.06 KB
/
TreeSitterParserTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@testable import Runestone
import TestTreeSitterLanguages
import TreeSitter
import XCTest
final class TreeSitterParserTests: XCTestCase {
private let delegate = MockTreeSitterParserDelegate()
func testParseString() {
let string: NSString = "let foo = \"Hello world\""
let parser = TreeSitterParser(encoding: TSInputEncodingUTF16)
parser.delegate = delegate
parser.language = tree_sitter_javascript()
let tree = parser.parse(string)
let expressionString = "(program (lexical_declaration (variable_declarator name: (identifier) value: (string))))"
XCTAssertEqual(tree?.rootNode.expressionString, expressionString)
}
func testReplaceShortTextWithSameShortText() {
let string: NSString = "let foo = \"Hello world\""
let parser = TreeSitterParser(encoding: TSInputEncodingUTF16)
parser.delegate = delegate
parser.language = tree_sitter_javascript()
let oldTree = parser.parse(string)
// Replace the entire text but with the same text (Select all and paste: CMD + A, CMD + V)
let inputEdit = TreeSitterInputEdit(
startByte: 0,
oldEndByte: string.byteCount,
newEndByte: string.byteCount,
startPoint: TreeSitterTextPoint(row: 0, column: 0),
oldEndPoint: TreeSitterTextPoint(row: 0, column: 23),
newEndPoint: TreeSitterTextPoint(row: 0, column: 23))
oldTree?.apply(inputEdit)
delegate.string = string
let newTree = parser.parse(oldTree: oldTree)
XCTAssertEqual(newTree!.rootNode.expressionString!, oldTree!.rootNode.expressionString!)
}
func testReplaceLongTextWithSameLongText() {
let string: NSString = """
/**
* This is a Runestone text view with syntax highlighting
* for the JavaScript programming language.
*/
let names = ["Steve Jobs", "Tim Cook", "Eddy Cue"]
let years = [1955, 1960, 1964]
printNamesAndYears(names, years)
// Print the year each person was born.
function printNamesAndYears(names, years) {
for (let i = 0; i < names.length; i++) {
console.log(names[i] + " was born in " + years[i])
}
}
"""
let parser = TreeSitterParser(encoding: TSInputEncodingUTF16)
parser.delegate = delegate
parser.language = tree_sitter_javascript()
let oldTree = parser.parse(string)
// Replace the entire text but with the same text (Select all and paste: CMD + A, CMD + V)
let inputEdit = TreeSitterInputEdit(
startByte: 0,
oldEndByte: 830,
newEndByte: 830,
startPoint: TreeSitterTextPoint(row: 0, column: 0),
oldEndPoint: TreeSitterTextPoint(row: 15, column: 0),
newEndPoint: TreeSitterTextPoint(row: 15, column: 0))
oldTree?.apply(inputEdit)
delegate.string = string
let newTree = parser.parse(oldTree: oldTree)
XCTAssertEqual(newTree!.rootNode.expressionString!, oldTree!.rootNode.expressionString!)
}
}