Skip to content

Commit

Permalink
fix: update tests and bump submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Feb 26, 2024
1 parent a03ce3c commit 20c2559
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
47 changes: 26 additions & 21 deletions tests/test_tree_sitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from typing import List, Optional, Tuple
from unittest import TestCase

from tree_sitter import (Language, LookaheadIterator, Node, Parser, Query,
Range, Tree)
from tree_sitter import Language, LookaheadIterator, Node, Parser, Query, Range, Tree

LIB_PATH = path.join("build", "languages.so")

Expand Down Expand Up @@ -178,7 +177,7 @@ def test_parsing_with_multiple_included_ranges(self):
parser.set_language(JAVASCRIPT)
js_tree = parser.parse(source_code)
template_string_node = js_tree.root_node.descendant_for_byte_range(
source_code.index(b"<div>"), source_code.index(b"Hello")
source_code.index(b"`<"), source_code.index(b">`")
)
if template_string_node is None:
self.fail("template_string_node is None")
Expand All @@ -188,13 +187,13 @@ def test_parsing_with_multiple_included_ranges(self):
open_quote_node = template_string_node.child(0)
if open_quote_node is None:
self.fail("open_quote_node is None")
interpolation_node1 = template_string_node.child(1)
interpolation_node1 = template_string_node.child(2)
if interpolation_node1 is None:
self.fail("interpolation_node1 is None")
interpolation_node2 = template_string_node.child(2)
interpolation_node2 = template_string_node.child(4)
if interpolation_node2 is None:
self.fail("interpolation_node2 is None")
close_quote_node = template_string_node.child(3)
close_quote_node = template_string_node.child(6)
if close_quote_node is None:
self.fail("close_quote_node is None")

Expand Down Expand Up @@ -224,7 +223,7 @@ def test_parsing_with_multiple_included_ranges(self):

self.assertEqual(
html_tree.root_node.sexp(),
"(fragment (element"
"(document (element"
+ " (start_tag (tag_name))"
+ " (text)"
+ " (element (start_tag (tag_name)) (end_tag (tag_name)))"
Expand Down Expand Up @@ -282,7 +281,7 @@ def test_parsing_with_included_range_containing_mismatched_positions(self):

self.assertEqual(
html_tree.root_node.sexp(),
"(fragment (element (start_tag (tag_name)) (text) (end_tag (tag_name))))",
"(document (element (start_tag (tag_name)) (text) (end_tag (tag_name))))",
)

def test_parsing_error_in_invalid_included_ranges(self):
Expand Down Expand Up @@ -418,7 +417,7 @@ def test_parsing_with_a_newly_excluded_range(self):

self.assertEqual(
tree.root_node.sexp(),
"(fragment (text) (element"
"(document (text) (element"
+ " (start_tag (tag_name))"
+ " (element (start_tag (tag_name)) (end_tag (tag_name)))"
+ " (end_tag (tag_name))))",
Expand Down Expand Up @@ -1276,7 +1275,7 @@ def assert_query_matches(
language: Language,
query: Query,
source: bytes,
expected: List[Tuple[int, List[Tuple[str, str]]]]
expected: List[Tuple[int, List[Tuple[str, str]]]],
):
parser = Parser()
parser.set_language(language)
Expand All @@ -1291,17 +1290,19 @@ def test_matches_with_simple_pattern(self):
JAVASCRIPT,
query,
b"function one() { two(); function three() {} }",
[(0, [('fn-name', 'one')]), (0, [('fn-name', 'three')])]
[(0, [("fn-name", "one")]), (0, [("fn-name", "three")])],
)

def test_matches_with_multiple_on_same_root(self):
query = JAVASCRIPT.query("""
query = JAVASCRIPT.query(
"""
(class_declaration
name: (identifier) @the-class-name
(class_body
(method_definition
name: (property_identifier) @the-method-name)))
""")
"""
)
self.assert_query_matches(
JAVASCRIPT,
query,
Expand All @@ -1317,14 +1318,16 @@ class Person {
[
(0, [("the-class-name", "Person"), ("the-method-name", "constructor")]),
(0, [("the-class-name", "Person"), ("the-method-name", "getFullName")]),
]
],
)

def test_matches_with_multiple_patterns_different_roots(self):
query = JAVASCRIPT.query("""
query = JAVASCRIPT.query(
"""
(function_declaration name:(identifier) @fn-def)
(call_expression function:(identifier) @fn-ref)
""")
"""
)
self.assert_query_matches(
JAVASCRIPT,
query,
Expand All @@ -1333,16 +1336,18 @@ def test_matches_with_multiple_patterns_different_roots(self):
f2(f3());
}
""",
[(0, [("fn-def", "f1")]), (1, [("fn-ref", "f2")]), (1, [("fn-ref", "f3")])]
[(0, [("fn-def", "f1")]), (1, [("fn-ref", "f2")]), (1, [("fn-ref", "f3")])],
)

def test_matches_with_nesting_and_no_fields(self):
query = JAVASCRIPT.query("""
query = JAVASCRIPT.query(
"""
(array
(array
(identifier) @x1
(identifier) @x2))
""")
"""
)
self.assert_query_matches(
JAVASCRIPT,
query,
Expand All @@ -1359,7 +1364,7 @@ def test_matches_with_nesting_and_no_fields(self):
(0, [("x1", "e"), ("x2", "h")]),
(0, [("x1", "f"), ("x2", "h")]),
(0, [("x1", "g"), ("x2", "h")]),
]
],
)

def test_captures(self):
Expand Down Expand Up @@ -1829,7 +1834,7 @@ def test_lookahead_iterator(self):
self.assertEqual(cursor.node.grammar_name, "identifier")
self.assertNotEqual(cursor.node.grammar_id, cursor.node.kind_id)

expected_symbols = ["identifier", "block_comment", "line_comment"]
expected_symbols = ["//", "/*", "identifier", "line_comment", "block_comment"]
lookahead: LookaheadIterator = RUST.lookahead_iterator(next_state)
self.assertEqual(lookahead.language, RUST.language_id)
self.assertEqual(list(lookahead.iter_names()), expected_symbols)
Expand Down
2 changes: 1 addition & 1 deletion tree_sitter/core
Submodule core updated 42 files
+2 −0 .cargo/config.toml
+5 −1 .github/workflows/release.yml
+194 −73 Cargo.lock
+18 −1 Cargo.toml
+7 −19 cli/Cargo.toml
+15 −18 cli/build.rs
+3 −11 cli/loader/Cargo.toml
+3 −4 cli/src/generate/binding_files.rs
+460 −0 cli/src/generate/grammar_files.rs
+53 −20 cli/src/generate/mod.rs
+16 −0 cli/src/generate/templates/PARSER_NAME.h
+11 −0 cli/src/generate/templates/PARSER_NAME.pc.in
+24 −0 cli/src/generate/templates/Package.swift
+3 −0 cli/src/generate/templates/__init__.py
+1 −0 cli/src/generate/templates/__init__.pyi
+0 −28 cli/src/generate/templates/binding.cc
+13 −0 cli/src/generate/templates/binding.go
+6 −3 cli/src/generate/templates/binding.gyp
+15 −0 cli/src/generate/templates/binding_test.go
+3 −24 cli/src/generate/templates/build.rs
+8 −11 cli/src/generate/templates/cargo.toml
+41 −0 cli/src/generate/templates/editorconfig
+10 −0 cli/src/generate/templates/gitattributes
+36 −0 cli/src/generate/templates/gitignore
+5 −0 cli/src/generate/templates/go.mod
+11 −0 cli/src/generate/templates/grammar.js
+2 −2 cli/src/generate/templates/index.js
+29 −0 cli/src/generate/templates/js-binding.cc
+12 −10 cli/src/generate/templates/lib.rs
+94 −0 cli/src/generate/templates/makefile
+17 −5 cli/src/generate/templates/package.json
+27 −0 cli/src/generate/templates/py-binding.c
+26 −0 cli/src/generate/templates/pyproject.toml
+49 −0 cli/src/generate/templates/setup.py
+4 −4 docs/section-2-using-parsers.md
+35 −3 docs/section-3-creating-parsers.md
+1 −3 highlight/Cargo.toml
+0 −62 script/version
+2 −4 tags/Cargo.toml
+20 −0 xtask/Cargo.toml
+291 −0 xtask/src/bump.rs
+35 −0 xtask/src/main.rs

0 comments on commit 20c2559

Please sign in to comment.