/*
 * $Id$
 *
 * SARL is an general-purpose agent programming language.
 * More details on http://www.sarl.io
 *
 * Copyright (C) 2014-2021 the original authors or authors.
 *
 * Licensed 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 io.sarl.lang.tests.general.parsing.general;

import static io.sarl.tests.api.tools.TestEObjects.file;
import static io.sarl.tests.api.tools.TestUtils.multilineString;
import static io.sarl.tests.api.tools.TestValidator.validate;

import org.eclipse.xtext.xbase.XbasePackage;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.sarl.lang.sarl.SarlPackage;
import io.sarl.lang.sarl.SarlScript;
import io.sarl.lang.validation.IssueCodes;
import io.sarl.tests.api.AbstractSarlTest;

/**
 * @author $Author: sgalland$
 * @version $Name$ $Revision$ $Date$
 * @mavengroupid $GroupId$
 * @mavenartifactid $ArtifactId$
 */
@SuppressWarnings("all")
@DisplayName("Syntax: continue")
@Tag("core")
public class ContinueKeywordTest extends AbstractSarlTest {

	@Test
	@Tag("sarlValidation")
	public void insideFunction() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct {",
				"    continue",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertError(
				SarlPackage.eINSTANCE.getSarlContinueExpression(),
				IssueCodes.INVALID_USE_OF_LOOP_BREAKING_KEYWORD,
				"Invalid use of the continue keyword");
	}

	@Test
	@Tag("sarlValidation")
	public void insideIfThen() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    if (a == 1) {",
				"      continue",
				"    }",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertError(
				SarlPackage.eINSTANCE.getSarlContinueExpression(),
				IssueCodes.INVALID_USE_OF_LOOP_BREAKING_KEYWORD,
				"Invalid use of the continue keyword");
	}

	@Test
	@Tag("sarlValidation")
	public void insideField() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  var field = [",
				"    continue",
				"  ]",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertError(
				SarlPackage.eINSTANCE.getSarlContinueExpression(),
				IssueCodes.INVALID_USE_OF_LOOP_BREAKING_KEYWORD,
				"Invalid use of the continue keyword");
	}

	@Test
	@Tag("sarlValidation")
	public void insideWhileWithoutBranch() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    var b = a",
				"    while (b > 0) {",
				"      continue",
				"      b--",
				"    }",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertError(
				XbasePackage.eINSTANCE.getXPostfixOperation(),
				org.eclipse.xtext.xbase.validation.IssueCodes.UNREACHABLE_CODE,
				"Unreachable expression");
	}

	@Test
	@Tag("sarlValidation")
	public void insideWhileWithBranch() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    var b = a",
				"    while (b > 0) {",
				"      if (a == 5) continue",
				"      b--",
				"    }",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertNoIssues();
	}

	@Test
	@Tag("sarlValidation")
	public void insideDoWhileWithoutBranch() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    var b = a",
				"    do {",
				"      continue",
				"      b--",
				"    } while (b > 0)",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertError(
				XbasePackage.eINSTANCE.getXPostfixOperation(),
				org.eclipse.xtext.xbase.validation.IssueCodes.UNREACHABLE_CODE,
				"Unreachable expression");
	}

	@Test
	@Tag("sarlValidation")
	public void insideDoWhileWithBranch() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    var b = a",
				"    do {",
				"      if (a == 5) continue",
				"      b--",
				"    } while (b > 0)",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertNoIssues();
	}

	@Test
	@Tag("sarlValidation")
	public void insideForWithoutBranch() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    for(b : 0..a) {",
				"      continue",
				"    }",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertNoIssues();
	}

	@Test
	@Tag("sarlValidation")
	public void insideForWithBranch() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    for(b : 0..a) {",
				"      if (b == 5) continue",
				"    }",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertNoIssues();
	}

	@Test
	@Tag("sarlValidation")
	public void insideBasicForWithoutBranch() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    for(var b = 0; b < a; b++) {",
				"      continue",
				"    }",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertWarning(
				SarlPackage.eINSTANCE.getSarlContinueExpression(),
				IssueCodes.DISCOURAGED_LOOP_BREAKING_KEYWORD_USE,
				"Discouraged use of the continue keyword inside a basic loop");
	}

	@Test
	@Tag("sarlValidation")
	public void insideBasicForWithBranch() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    for(var b = 0; b < a; b++) {",
				"      if (b == 5) continue",
				"    }",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertWarning(
				SarlPackage.eINSTANCE.getSarlContinueExpression(),
				IssueCodes.DISCOURAGED_LOOP_BREAKING_KEYWORD_USE,
				"Discouraged use of the continue keyword inside a basic loop");
	}

	@Test
	@Tag("sarlValidation")
	public void unreachableCode() throws Exception {
		SarlScript mas = file(getParseHelper(), multilineString(
				"agent A1 {",
				"  def fct(a : int) {",
				"    for(b : 0..a) {",
				"      if (b == 5) {",
				"        continue",
				"        println(b)",
				"      }",
				"    }",
				"  }",
				"}"
				));
		validate(getValidationHelper(), getInjector(), mas).assertError(
				XbasePackage.eINSTANCE.getXFeatureCall(),
				org.eclipse.xtext.xbase.validation.IssueCodes.UNREACHABLE_CODE,
				"Unreachable expression");
	}

}
