Skip to content

Commit

Permalink
fix: improve rules.xml and added tests, fix #375, #362
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfaller committed Oct 23, 2024
1 parent d1f398a commit b9a42bc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 14 deletions.
32 changes: 21 additions & 11 deletions gen.treesitter-ng/src/main/resources/rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go:
- '{'
- '}'
- '.'
aliased: []
aliased: {}
label_ignored: []
java:
flattened:
Expand Down Expand Up @@ -93,20 +93,31 @@ java:
ocaml:
flattened:
- string
aliased: []
aliased: {}
ignored: []
label_ignored: []
python:
flattened:
- string
aliased:
'==': comparison_operator
'<=': comparison_operator
'>=': comparison_operator
'!=': comparison_operator
'<': comparison_operator
'>': comparison_operator
'<>': comparison_operator
'==': comparison_operator_literal
'<=': comparison_operator_literal
'>=': comparison_operator_literal
'!=': comparison_operator_literal
'<': comparison_operator_literal
'>': comparison_operator_literal
'<>': comparison_operator_literal
'and': logical_operator_literal
'or': logical_operator_literal
'augmented_assignment': assignment
'=': assignment_operator_literal
'-=': assignment_operator_literal
'+=': assignment_operator_literal
'*=': assignment_operator_literal
'/=': assignment_operator_literal
'//=': assignment_operator_literal
'%=': assignment_operator_literal
'**=': assignment_operator_literal
ignored:
- '('
- ')'
Expand All @@ -118,13 +129,12 @@ python:
- ':'
- ','
- 'default_parameter ='
- 'import'
- 'wildcard_import *'
- 'def'
- 'for'
- 'in'
- 'if'
- 'with'
- 'as'
- 'return'
label_ignored: []
rust:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of GumTree.
*
* GumTree is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GumTree is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2022 Jean-Rémy Falleri <jr.falleri@gmail.com>
*/
package com.github.gumtreediff.gen.treesitterng;

import com.github.gumtreediff.tree.TreeContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class GoTreeSitterNgTreeGeneratorTest {
private final GoTreeSitterNgTreeGenerator generator = new GoTreeSitterNgTreeGenerator();

@Test
public void testHelloWorld() throws IOException {
TreeContext src = generator.generateFrom().string("package main\n" +
"import \"fmt\"\n" +
"func main() {\n" +
" fmt.Println(\"hello world\")\n" +
"}");
assertEquals(20, src.getRoot().getMetrics().size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
package com.github.gumtreediff.gen.treesitterng;

import com.github.gumtreediff.tree.Tree;
import com.github.gumtreediff.tree.TreeContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PythonTreeSitterNgTreeGeneratorTest {
private final PythonTreeSitterNgTreeGenerator generator = new PythonTreeSitterNgTreeGenerator();
Expand All @@ -35,5 +35,26 @@ public void testString() throws IOException {
TreeContext src = generator.generateFrom().file("testData/python/foo.py");
assertEquals(12, src.getRoot().getMetrics().size);
}


@ParameterizedTest
@ValueSource(strings = { "<", "<=", ">", ">=", "==", "!=" })
public void testComparisonOperators(String operator) throws IOException {
TreeContext src = generator.generateFrom().string("3 " + operator + " 2");
assertEquals("comparison_operator_literal", src.getRoot().getChild("0.0.1").getType().name);
}

@ParameterizedTest
@ValueSource(strings = { "and", "or"})
public void testBooleanOperators(String operator) throws IOException {
TreeContext src = generator.generateFrom().string("true " + operator + " false");
assertEquals("logical_operator_literal", src.getRoot().getChild("0.0.1").getType().name);
}

@ParameterizedTest
@ValueSource(strings = { "=", "+=", "-=", "*=", "/=", "//=", "%=", "**="})
public void testAssignmentOperators(String operator) throws IOException {
TreeContext src = generator.generateFrom().string("x " + operator + " 12");
assertEquals("assignment", src.getRoot().getChild("0.0").getType().name);
assertEquals("assignment_operator_literal", src.getRoot().getChild("0.0.1").getType().name);
}
}

0 comments on commit b9a42bc

Please sign in to comment.