package pp.s1184725.boppi.test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import org.junit.Test;

/**
 * Tests for conditionals, i.e. if, if-else branching and while loops.
 * 
 * @author Frank Wibbelink
 */
public class ConditionalTest {

	/**
	 * Correct if-else and loop use
	 */
	@Test
	public void correctConditionalParsing() {
		BoppiTests.parseFile("if.boppi");
		assertThat(BoppiTests.log, empty());

		BoppiTests.parseFile("while.boppi");
		assertThat(BoppiTests.log, empty());
	}

	/**
	 * Correct if-else and loop use
	 */
	@Test
	public void correctConditionalChecking() {
		BoppiTests.checkFile("if.boppi");
		assertThat(BoppiTests.log, is(empty()));

		BoppiTests.checkFile("while.boppi");
		assertThat(BoppiTests.log, is(empty()));
	}

	/**
	 * Correct evaluation of basic programs
	 */
	@Test
	public void basicPrograms() {
		BoppiTests.compileAndRunString("print(5*3)");
		assertThat(BoppiTests.log, is(empty()));
		assertThat(BoppiTests.out, is(arrayContaining("15")));

		BoppiTests.compileAndRunString("print({var int x; x := 8; print(x)})");
		assertThat(BoppiTests.log, is(empty()));
		assertThat(BoppiTests.out, is(arrayContaining("8", "8")));

		BoppiTests.compileAndRunString("print('T', 'e', 's', 't', '!')");
		assertThat(BoppiTests.log, is(empty()));
		assertThat(BoppiTests.out, is(arrayContaining("T", "e", "s", "t", "!")));

		BoppiTests.compileAndRunString("var int x; var int y; x := 3*(y := 4); print(x,y)");
		assertThat(BoppiTests.log, is(empty()));
		assertThat(BoppiTests.out, is(arrayContaining("12", "4")));

		BoppiTests.compileAndRunFile("basicProgram1.boppi", "1", "T");
		assertThat(BoppiTests.log, is(empty()));
		assertThat(BoppiTests.out, is(arrayContaining("T", "T")));

		BoppiTests.compileAndRunFile("fibonacciIterative.boppi", "6");
		assertThat(BoppiTests.log, is(empty()));
		assertThat(BoppiTests.out, is(arrayContaining("13")));
	}

}
