Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindings): add node, python, swift tests #3178

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 36 additions & 3 deletions cli/src/generate/grammar_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,25 @@ const INDEX_JS_TEMPLATE: &str = include_str!("./templates/index.js");
const INDEX_D_TS_TEMPLATE: &str = include_str!("./templates/index.d.ts");
const JS_BINDING_CC_TEMPLATE: &str = include_str!("./templates/js-binding.cc");
const BINDING_GYP_TEMPLATE: &str = include_str!("./templates/binding.gyp");
const BINDING_TEST_JS_TEMPLATE: &str = include_str!("./templates/binding_test.js");

const MAKEFILE_TEMPLATE: &str = include_str!("./templates/makefile");
const PARSER_NAME_H_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.h");
const PARSER_NAME_PC_IN_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.pc.in");

const GO_MOD_TEMPLATE: &str = include_str!("./templates/go.mod");
const BINDING_GO_TEMPLATE: &str = include_str!("./templates/binding.go");
const BINDING_GO_TEST_TEMPLATE: &str = include_str!("./templates/binding_test.go");
const BINDING_TEST_GO_TEMPLATE: &str = include_str!("./templates/binding_test.go");

const SETUP_PY_TEMPLATE: &str = include_str!("./templates/setup.py");
const INIT_PY_TEMPLATE: &str = include_str!("./templates/__init__.py");
const INIT_PYI_TEMPLATE: &str = include_str!("./templates/__init__.pyi");
const PYPROJECT_TOML_TEMPLATE: &str = include_str!("./templates/pyproject.toml");
const PY_BINDING_C_TEMPLATE: &str = include_str!("./templates/py-binding.c");
const TEST_BINDING_PY_TEMPLATE: &str = include_str!("./templates/test_binding.py");

const PACKAGE_SWIFT_TEMPLATE: &str = include_str!("./templates/Package.swift");
const PACKAGE_SWIFT_TEMPLATE: &str = include_str!("./templates/package.swift");
const TESTS_SWIFT_TEMPLATE: &str = include_str!("./templates/tests.swift");

#[derive(Deserialize, Debug)]
struct LanguageConfiguration {}
Expand Down Expand Up @@ -339,6 +342,10 @@ pub fn generate_grammar_files(
generate_file(path, INDEX_D_TS_TEMPLATE, language_name)
})?;

missing_path(path.join("binding_test.js"), |path| {
generate_file(path, BINDING_TEST_JS_TEMPLATE, language_name)
})?;

missing_path_else(
path.join("binding.cc"),
|path| generate_file(path, JS_BINDING_CC_TEMPLATE, language_name),
Expand Down Expand Up @@ -397,7 +404,7 @@ pub fn generate_grammar_files(
})?;

missing_path(path.join("binding_test.go"), |path| {
generate_file(path, BINDING_GO_TEST_TEMPLATE, language_name)
generate_file(path, BINDING_TEST_GO_TEMPLATE, language_name)
})?;

missing_path(path.join("go.mod"), |path| {
Expand Down Expand Up @@ -428,6 +435,13 @@ pub fn generate_grammar_files(
generate_file(path, "", language_name) // py.typed is empty
})?;

missing_path(path.join("tests"), create_dir)?.apply(|path| {
missing_path(path.join("test_binding.py"), |path| {
generate_file(path, TEST_BINDING_PY_TEMPLATE, language_name)
})?;
Ok(())
})?;

missing_path(repo_path.join("setup.py"), |path| {
generate_file(path, SETUP_PY_TEMPLATE, language_name)
})?;
Expand All @@ -448,6 +462,25 @@ pub fn generate_grammar_files(
generate_file(path, PARSER_NAME_H_TEMPLATE, language_name)
})?;

missing_path(
path.join(format!(
"TreeSitter{}Tests",
language_name.to_upper_camel_case()
)),
create_dir,
)?
.apply(|path| {
missing_path(
path.join(format!(
"TreeSitter{}Tests.swift",
language_name.to_upper_camel_case()
)),
|path| generate_file(path, TESTS_SWIFT_TEMPLATE, language_name),
)?;

Ok(())
})?;

missing_path(repo_path.join("Package.swift"), |path| {
generate_file(path, PACKAGE_SWIFT_TEMPLATE, language_name)
})?;
Expand Down
47 changes: 0 additions & 47 deletions cli/src/generate/templates/Package.swift

This file was deleted.

9 changes: 9 additions & 0 deletions cli/src/generate/templates/binding_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference types="node" />

const assert = require("node:assert");
const { test } = require("node:test");

test("can load grammar", () => {
const parser = new (require("tree-sitter"))();
assert.doesNotThrow(() => parser.setLanguage(require(".")));
});
1 change: 1 addition & 0 deletions cli/src/generate/templates/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_modules/

# Swift artifacts
.build/
Package.resolved

# Go artifacts
go.sum
Expand Down
60 changes: 60 additions & 0 deletions cli/src/generate/templates/package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// swift-tools-version:5.3
import PackageDescription

let package = Package(
name: "TreeSitterCAMEL_PARSER_NAME",
products: [
.library(name: "TreeSitterCAMEL_PARSER_NAME", targets: ["TreeSitterCAMEL_PARSER_NAME"]),
],
dependencies: [
.package(url: "https://github.com/ChimeHQ/SwiftTreeSitter", from: "0.8.0"),
],
targets: [
.target(
name: "TreeSitterCAMEL_PARSER_NAME",
dependencies: [],
path: ".",
exclude: [
"Cargo.toml",
"Makefile",
"binding.gyp",
"bindings/c",
"bindings/go",
"bindings/node",
"bindings/python",
"bindings/rust",
"prebuilds",
"grammar.js",
"package.json",
"package-lock.json",
"pyproject.toml",
"setup.py",
"test",
"examples",
".editorconfig",
".github",
".gitignore",
".gitattributes",
".gitmodules",
],
sources: [
"src/parser.c",
// NOTE: if your language has an external scanner, add it here.
],
resources: [
.copy("queries")
],
publicHeadersPath: "bindings/swift",
cSettings: [.headerSearchPath("src")]
),
.testTarget(
name: "TreeSitterCAMEL_PARSER_NAMETests",
dependencies: [
"SwiftTreeSitter",
"TreeSitterCAMEL_PARSER_NAME",
],
path: "bindings/swift/TreeSitterCAMEL_PARSER_NAMETests"
)
],
cLanguageStandard: .c11
)
11 changes: 11 additions & 0 deletions cli/src/generate/templates/test_binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from unittest import TestCase

import tree_sitter, tree_sitter_LOWER_PARSER_NAME


class TestLanguage(TestCase):
def test_can_load_grammar(self):
try:
tree_sitter.Language(tree_sitter_LOWER_PARSER_NAME.language())
except Exception:
self.fail("Error loading CAMEL_PARSER_NAME grammar")
12 changes: 12 additions & 0 deletions cli/src/generate/templates/tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import XCTest
import SwiftTreeSitter
import TreeSitterCAMEL_PARSER_NAME

final class TreeSitterCAMEL_PARSER_NAMETests: XCTestCase {
func testCanLoadGrammar() throws {
let parser = Parser()
let language = Language(language: tree_sitter_LOWER_PARSER_NAME())
XCTAssertNoThrow(try parser.setLanguage(language),
"Error loading CAMEL_PARSER_NAME grammar")
}
}