<commit_before>package org.drools.lang;

import java.util.LinkedList;

import junit.framework.TestCase;

import org.antlr.runtime.RecognitionException;
import org.drools.compiler.DrlParser;
import org.drools.compiler.DroolsParserException;
import org.drools.lang.descr.ImportDescr;
import org.drools.lang.descr.PackageDescr;
import org.drools.lang.descr.RuleDescr;

public class DRLIncompleteCodeTest extends TestCase {

	public void testIncompleteCode1() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.* query MyQuery rule MyRule when Class ( property memberOf collexction ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNotNull(descr);
		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertEquals(Location.LOCATION_LHS_INSIDE_CONDITION_END,
				getLastIntegerValue(parser.getEditorSentences().get(2)
						.getContent()));
	}

	public void testIncompleteCode2() throws DroolsParserException,
			RecognitionException {
		String input = "rule MyRule when Class ( property memberOf collection ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNull(descr);
		assertEquals(Location.LOCATION_LHS_INSIDE_CONDITION_END,
				getLastIntegerValue(parser.getEditorSentences().get(0)
						.getContent()));
	}

	public void testIncompleteCode3() throws DroolsParserException,
			RecognitionException {
		String input = "rule MyRule when Class ( property memberOf collection ) then end query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNotNull(descr);
		assertEquals("MyRule", ((RuleDescr) descr.getRules().get(0)).getName());

		assertNotNull(descr);
		assertEquals("MyQuery", ((RuleDescr) descr.getRules().get(1)).getName());

		assertEquals(Location.LOCATION_RHS, getLastIntegerValue(parser
				.getEditorSentences().get(0).getContent()));
	}

	public void testIncompleteCode4() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertNotNull(descr);
		assertEquals("MyRule", ((RuleDescr) descr.getRules().get(0)).getName());

		assertNotNull(descr);
		assertEquals("MyQuery", ((RuleDescr) descr.getRules().get(1)).getName());
	}

	public void testIncompleteCode5() throws DroolsParserException,
			RecognitionException {
		String input = "packe a.b.c import a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNull(descr);
	}

	public void testIncompleteCode6() throws DroolsParserException,
			RecognitionException {
		String input = "packe 1111.111 import a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNull(descr);
	}

	public void testIncompleteCode7() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c imrt a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNull(descr);
	}

	public void testIncompleteCode8() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.1111.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals(0, descr.getRules().size());
	}

	public void testIncompleteCode9() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.*"
				+ " rule MyRule xxxxx Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertEquals(1, descr.getRules().size());
		assertEquals("MyQuery", ((RuleDescr) descr.getRules().get(0)).getName());
	}

	public void testIncompleteCode10() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.*"
				+ " rule MyRule xxxxx Class ( property memberOf "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertEquals(0, descr.getRules().size());
	}

	public void testIncompleteCode11() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " qzzzzuery MyQuery Class ( property ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertNotNull(descr);
		assertEquals("MyRule", ((RuleDescr) descr.getRules().get(0)).getName());
	}

	@SuppressWarnings("unchecked")
	private int getLastIntegerValue(LinkedList list) {
		System.out.println(list.toString());
		int lastIntergerValue = -1;
		for (Object object : list) {
			if (object instanceof Integer) {<commit_msg>JBRULES-1750: Update IDE to new parser<commit_after>package org.drools.lang;

import java.util.LinkedList;

import junit.framework.TestCase;

import org.antlr.runtime.RecognitionException;
import org.drools.compiler.DrlParser;
import org.drools.compiler.DroolsParserException;
import org.drools.lang.descr.ImportDescr;
import org.drools.lang.descr.PackageDescr;
import org.drools.lang.descr.RuleDescr;

public class DRLIncompleteCodeTest extends TestCase {

	public void testIncompleteCode1() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.* query MyQuery rule MyRule when Class ( property memberOf collexction ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNotNull(descr);
		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertEquals(Location.LOCATION_LHS_INSIDE_CONDITION_END,
				getLastIntegerValue(parser.getEditorSentences().get(2)
						.getContent()));
	}

	public void testIncompleteCode2() throws DroolsParserException,
			RecognitionException {
		String input = "rule MyRule when Class ( property memberOf collection ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNull(descr);
		assertEquals(Location.LOCATION_LHS_INSIDE_CONDITION_END,
				getLastIntegerValue(parser.getEditorSentences().get(0)
						.getContent()));
	}

	public void testIncompleteCode3() throws DroolsParserException,
			RecognitionException {
		String input = "rule MyRule when Class ( property memberOf collection ) then end query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNotNull(descr);
		assertEquals("MyRule", ((RuleDescr) descr.getRules().get(0)).getName());

		assertNotNull(descr);
		assertEquals("MyQuery", ((RuleDescr) descr.getRules().get(1)).getName());

		assertEquals(Location.LOCATION_RHS, getLastIntegerValue(parser
				.getEditorSentences().get(0).getContent()));
	}

	public void testIncompleteCode4() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertNotNull(descr);
		assertEquals("MyRule", ((RuleDescr) descr.getRules().get(0)).getName());

		assertNotNull(descr);
		assertEquals("MyQuery", ((RuleDescr) descr.getRules().get(1)).getName());
	}

	public void testIncompleteCode5() throws DroolsParserException,
			RecognitionException {
		String input = "packe a.b.c import a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNull(descr);
	}

	public void testIncompleteCode6() throws DroolsParserException,
			RecognitionException {
		String input = "packe 1111.111 import a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNull(descr);
	}

	public void testIncompleteCode7() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c imrt a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertNull(descr);
	}

	public void testIncompleteCode8() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.1111.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals(0, descr.getRules().size());
	}

	public void testIncompleteCode9() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.*"
				+ " rule MyRule xxxxx Class ( property memberOf collection ) then end "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertEquals(1, descr.getRules().size());
		assertEquals("MyQuery", ((RuleDescr) descr.getRules().get(0)).getName());
	}

	public void testIncompleteCode10() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.*"
				+ " rule MyRule xxxxx Class ( property memberOf "
				+ " query MyQuery Class ( property memberOf collection ) end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertEquals(0, descr.getRules().size());
	}

	public void testIncompleteCode11() throws DroolsParserException,
			RecognitionException {
		String input = "package a.b.c import a.b.c.*"
				+ " rule MyRule when Class ( property memberOf collection ) then end "
				+ " qzzzzuery MyQuery Class ( property ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);

		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0))
				.getTarget());

		assertNotNull(descr);
		assertEquals("MyRule", ((RuleDescr) descr.getRules().get(0)).getName());
	}

	public void testIncompleteCode12() throws DroolsParserException, RecognitionException {
		String input = 
			"package a.b.c " +
			"import a.b.c.* " +
			"rule MyRule" +
			"  when " +
			"    m: Message(  ) " +
			"    " +
			"  then" +
			"end ";
		DrlParser parser = new DrlParser();
		PackageDescr descr = parser.parse(true, input);
		assertNotNull(descr);
		assertEquals("a.b.c", descr.getNamespace());
		assertEquals("a.b.c.*", ((ImportDescr) descr.getImports().get(0)).getTarget());
	}
	
	@SuppressWarnings("unchecked")
	private int getLastIntegerValue(LinkedList list) {
		System.out.println(list.toString());
		int lastIntergerValue = -1;
		for (Object object : list) {
			if (object instanceof Integer) {
{'fraction_non_alphanumeric': 0.1282951235904712, 'fraction_numerical': 0.006795608991113434, 'mean_word_length': 4.566084788029925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4265805', 'n_tokens_mistral': 4608, 'n_tokens_neox': 4095, 'n_words': 1000}