package edu.kit.iti.formal.pse.worthwhile.common.tests;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;
import org.junit.Assert;
import org.junit.Test;

import com.google.inject.Injector;

import de.itemis.xtext.typesystem.testing.IssueCollection;
import de.itemis.xtext.typesystem.testing.XTextTestCase;
import edu.kit.iti.formal.pse.worthwhile.WorthwhileStandaloneSetup;
import edu.kit.iti.formal.pse.worthwhile.model.ast.VariableDeclaration;

/**
 * Test class for the validator.
 *
 */
public class ValidatorTest extends XTextTestCase {

	@Test
	public void testEmptyArrayLiteral() {
		String testProgram = "Integer[]  k := {}\n";
		Assert.assertEquals(0, TestASTProvider.getErrorCount(testProgram));
	}

	@Test
	public void testTypesOfParams() {
		String testProgram = "Integer a := true\n" +
				"a := false \n";

		initialize(testProgram);
		assertConstraints(allIssues.forType(VariableDeclaration.class).sizeIs(1));

	}
	
	private void initialize(String parseText) {
		XtextResource resource = (XtextResource) TestASTProvider.loadProgram(parseText);
		
		Assert.assertTrue(resource.getErrors().isEmpty());
		
		Injector guiceInjector = new WorthwhileStandaloneSetup().createInjectorAndDoEMFRegistration();
		IResourceValidator validator = guiceInjector.getInstance(IResourceValidator.class);
		List<Issue> validationErrors = validator.validate(resource, CheckMode.ALL, null);
		List<String> messages = new ArrayList<String>();
		for (Issue issue : validationErrors) {
			messages.add(issue.getMessage());
		}
		allIssues = new IssueCollection(resource, validationErrors, messages);
	}
}

@@ -1118,16 +1118,327 @@
 s(1));%0A%0A
+%09%7D%0A%09%0A%09/**%0A%09 * Tests that undeclared functions are not checked for their parameter count.%0A%09 */%0A%09@Test%0A%09public final void testUndeclaredFunctionParameterCountCheck() %7B%0A%09%09String testProgram = %22Integer i := g(1)%5CnInteger j := g()%5Cn%22;%0A%09%09TestUtils.assertErrorCountEquals(2, TestASTProvider.loadProgram(testProgram));%0A
 %09%7D%0A%09%0A%09pr

Add test case for #75