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("programs/if.boppi");
		assertThat(BoppiTests.log, empty());

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

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

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

	/**
	 * Wrong if-else and loop use
	 */
	@Test
	public void wrongConditionalChecking() {
		BoppiTests.checkString("if 5 then 0 fi");
		assertThat(BoppiTests.log, hasSize(1));

		BoppiTests.checkString("5 + if true then 3 fi");
		assertThat(BoppiTests.log, hasSize(1));

		BoppiTests.checkString("while 'a' do 1+1 od");
		assertThat(BoppiTests.log, hasSize(1));
	}

	/**
	 * Correct evaluation of basic programs
	 */
	@Test
	public void basicPrograms() {
		BoppiTests.compileAndRunFile("programs/basicProgram1.boppi", "1", "T");
		assertThat(BoppiTests.log, is(empty()));
		assertThat(BoppiTests.out, is(arrayContaining("T", "T")));

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

}
