<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements. See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership. The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License. You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied. See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.codehaus.groovy.ast

import org.codehaus.groovy.ast.expr.ArgumentListExpression
import org.codehaus.groovy.ast.expr.ArrayExpression
import org.codehaus.groovy.ast.expr.AttributeExpression
import org.codehaus.groovy.ast.expr.BinaryExpression
import org.codehaus.groovy.ast.expr.BitwiseNegationExpression
import org.codehaus.groovy.ast.expr.BooleanExpression
import org.codehaus.groovy.ast.expr.CastExpression
import org.codehaus.groovy.ast.expr.ClassExpression
import org.codehaus.groovy.ast.expr.ClosureExpression
import org.codehaus.groovy.ast.expr.ClosureListExpression
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.ConstructorCallExpression
import org.codehaus.groovy.ast.expr.DeclarationExpression
import org.codehaus.groovy.ast.expr.ElvisOperatorExpression
import org.codehaus.groovy.ast.expr.FieldExpression
import org.codehaus.groovy.ast.expr.GStringExpression
import org.codehaus.groovy.ast.expr.ListExpression
import org.codehaus.groovy.ast.expr.MapEntryExpression
import org.codehaus.groovy.ast.expr.MapExpression
import org.codehaus.groovy.ast.expr.MethodCallExpression
import org.codehaus.groovy.ast.expr.MethodPointerExpression
import org.codehaus.groovy.ast.expr.NotExpression
import org.codehaus.groovy.ast.expr.PostfixExpression
import org.codehaus.groovy.ast.expr.PrefixExpression
import org.codehaus.groovy.ast.expr.PropertyExpression
import org.codehaus.groovy.ast.expr.RangeExpression
import org.codehaus.groovy.ast.expr.SpreadExpression
import org.codehaus.groovy.ast.expr.SpreadMapExpression
import org.codehaus.groovy.ast.expr.StaticMethodCallExpression
import org.codehaus.groovy.ast.expr.TernaryExpression
import org.codehaus.groovy.ast.expr.TupleExpression
import org.codehaus.groovy.ast.expr.UnaryMinusExpression
import org.codehaus.groovy.ast.expr.UnaryPlusExpression
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codehaus.groovy.ast.stmt.Statement
import org.codehaus.groovy.control.SourceUnit
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

/**
 * Tests the LineColumn information in file with path specified by the prefix TEST_FILE_PREFIX.
 * The base version contains tests that should work with both the antlr2 and antlr4 parser.
 * The suffixed versions work with just the respective parser. In general, the antlr4 parser
 * has more accurate line/column information in a number of situations.
 * 
 * The file in the specified path should look like:
 * 
 * ###<testname>:::
 * <source code from which the AST will be built>
 * :::<expected AST output>
 * 
 * The section above can be repeated for every new TestCase
 * 
 * The AST output from the visitor is quite big. Also for small source code snippets. Therefore
 * it is possible to only specify the nodes that you want to check and separate them with a semicolon.
 * A semicolon is also needed when you begin with a new line.
 * Example: 
 * [TryCatchStatement,(1:1),(9:2)][BlockStatement,(1:5),(3:3)];
 * [CatchStatement,(3:3),(5:3)][BlockStatement,(3:12),(5:3)];
 * [CatchStatement,(5:3),(7:3)][BlockStatement,(5:12),(7:3)];
 * [BlockStatement,(7:3),(9:2)][BlockStatement,(7:11),(9:2)]
 * 
 * [<NodeType>,(<line>:<column>),(<lastLine>:<lastColumn>)]
 */
@RunWith(Parameterized)
class LineColumnCheckTest extends ASTTest {

    static final String TEST_FILE_PREFIX = './src/test/org/codehaus/groovy/ast/LineColumnCheck'

    private LineCheckVisitor visitor
    private String name
    private String source
    private String[] expected

    @Parameterized.Parameters(name = 'Test {0}: Source: {1} Expected: {2}')
    static Iterable<Object[]> data() {
        List testdata = extractData("${TEST_FILE_PREFIX}.txt")
        //flip if condition as per below and swap antlr2/4 ordering once antlr4 is the default
        //if (System.getProperty('groovy.antlr4') != 'false') {
        if (System.getProperty('groovy.antlr4') != 'true') {
            testdata += extractData("${TEST_FILE_PREFIX}_antlr2.txt")
        } else {
            testdata += extractData("${TEST_FILE_PREFIX}_antlr4.txt")
        }
        testdata
    }

    private static List extractData(String test_file_path) {<commit_msg>Choose the right data for node position<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements. See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership. The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License. You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied. See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.codehaus.groovy.ast

import org.codehaus.groovy.ast.expr.ArgumentListExpression
import org.codehaus.groovy.ast.expr.ArrayExpression
import org.codehaus.groovy.ast.expr.AttributeExpression
import org.codehaus.groovy.ast.expr.BinaryExpression
import org.codehaus.groovy.ast.expr.BitwiseNegationExpression
import org.codehaus.groovy.ast.expr.BooleanExpression
import org.codehaus.groovy.ast.expr.CastExpression
import org.codehaus.groovy.ast.expr.ClassExpression
import org.codehaus.groovy.ast.expr.ClosureExpression
import org.codehaus.groovy.ast.expr.ClosureListExpression
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.ConstructorCallExpression
import org.codehaus.groovy.ast.expr.DeclarationExpression
import org.codehaus.groovy.ast.expr.ElvisOperatorExpression
import org.codehaus.groovy.ast.expr.FieldExpression
import org.codehaus.groovy.ast.expr.GStringExpression
import org.codehaus.groovy.ast.expr.ListExpression
import org.codehaus.groovy.ast.expr.MapEntryExpression
import org.codehaus.groovy.ast.expr.MapExpression
import org.codehaus.groovy.ast.expr.MethodCallExpression
import org.codehaus.groovy.ast.expr.MethodPointerExpression
import org.codehaus.groovy.ast.expr.NotExpression
import org.codehaus.groovy.ast.expr.PostfixExpression
import org.codehaus.groovy.ast.expr.PrefixExpression
import org.codehaus.groovy.ast.expr.PropertyExpression
import org.codehaus.groovy.ast.expr.RangeExpression
import org.codehaus.groovy.ast.expr.SpreadExpression
import org.codehaus.groovy.ast.expr.SpreadMapExpression
import org.codehaus.groovy.ast.expr.StaticMethodCallExpression
import org.codehaus.groovy.ast.expr.TernaryExpression
import org.codehaus.groovy.ast.expr.TupleExpression
import org.codehaus.groovy.ast.expr.UnaryMinusExpression
import org.codehaus.groovy.ast.expr.UnaryPlusExpression
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codehaus.groovy.ast.stmt.Statement
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.ParserVersion
import org.codehaus.groovy.control.SourceUnit
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

/**
 * Tests the LineColumn information in file with path specified by the prefix TEST_FILE_PREFIX.
 * The base version contains tests that should work with both the antlr2 and antlr4 parser.
 * The suffixed versions work with just the respective parser. In general, the antlr4 parser
 * has more accurate line/column information in a number of situations.
 * 
 * The file in the specified path should look like:
 * 
 * ###<testname>:::
 * <source code from which the AST will be built>
 * :::<expected AST output>
 * 
 * The section above can be repeated for every new TestCase
 * 
 * The AST output from the visitor is quite big. Also for small source code snippets. Therefore
 * it is possible to only specify the nodes that you want to check and separate them with a semicolon.
 * A semicolon is also needed when you begin with a new line.
 * Example: 
 * [TryCatchStatement,(1:1),(9:2)][BlockStatement,(1:5),(3:3)];
 * [CatchStatement,(3:3),(5:3)][BlockStatement,(3:12),(5:3)];
 * [CatchStatement,(5:3),(7:3)][BlockStatement,(5:12),(7:3)];
 * [BlockStatement,(7:3),(9:2)][BlockStatement,(7:11),(9:2)]
 * 
 * [<NodeType>,(<line>:<column>),(<lastLine>:<lastColumn>)]
 */
@RunWith(Parameterized)
class LineColumnCheckTest extends ASTTest {

    static final String TEST_FILE_PREFIX = './src/test/org/codehaus/groovy/ast/LineColumnCheck'

    private LineCheckVisitor visitor
    private String name
    private String source
    private String[] expected

    @Parameterized.Parameters(name = 'Test {0}: Source: {1} Expected: {2}')
    static Iterable<Object[]> data() {
        List testdata = extractData("${TEST_FILE_PREFIX}.txt")
        //flip if condition as per below and swap antlr2/4 ordering once antlr4 is the default
        //if (System.getProperty('groovy.antlr4') != 'false') {
        if (ParserVersion.V_2 == CompilerConfiguration.DEFAULT.parserVersion) {
            testdata += extractData("${TEST_FILE_PREFIX}_antlr2.txt")
        } else {
            testdata += extractData("${TEST_FILE_PREFIX}_antlr4.txt")
        }
        testdata
    }

    private static List extractData(String test_file_path) {
{'fraction_non_alphanumeric': 0.0990570623116555, 'fraction_numerical': 0.010109847380188587, 'mean_word_length': 4.786276715410573, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3748297', 'n_tokens_mistral': 3112, 'n_tokens_neox': 2968, 'n_words': 808}