package slogo.backend.test;

import static org.junit.Assert.*;

import org.junit.Test;

import slogo.Constants;
import slogo.ExecutionException;
import slogo.IModel;
import slogo.backend.evaluation.IExecutionContext;
import slogo.backend.impl.Backend;
import slogo.backend.impl.InitializationException;

public class BackendTest {
	@Test
	public void testExecute() throws ExecutionException{
		IModel backend = null;
		try {
			backend = new Backend();
		} catch (InitializationException e) {
			e.printStackTrace();
			fail();
		}
		IExecutionContext result = backend.execute("Minus Difference 80 50");
		assertEquals("-30.0", result.environment().get(Constants.RETURN_VALUE_ENVIRONMENT));
		IExecutionContext foo = backend.execute("Forward 50");
		assertEquals(1, foo.turtles().get("1").lineSequence().size());
		IExecutionContext bar = backend.execute("And 1 0");
		assertEquals("0", bar.environment().get(Constants.RETURN_VALUE_ENVIRONMENT));
		assertEquals(1, bar.turtles().get("1").lineSequence().size());
	}
	@Test(expected = ExecutionException.class)
	public void testExecuteNegative() throws Exception {
		IModel backend = new Backend();
		backend.execute("!!@#!@$!@#$@#$");
	}
}

@@ -57,16 +57,41 @@
 ert.*;%0A%0A
+import org.junit.Before;%0A
 import o
@@ -354,207 +354,188 @@
  %7B%0A%09
-@Test%0A%09public void testExecute() throws ExecutionException%7B%0A%09%09IModel backend = null;%0A%09%09try %7B%0A%09%09%09backend = new Backend();%0A%09%09%7D catch (InitializationException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%09fail();%0A%09%09%7D
+private IModel backend;%0A%09@Before%0A%09public void init() throws InitializationException%7B%0A%09%09this.backend = new Backend();%0A%09%7D%0A%09@Test%0A%09public void testExecute() throws ExecutionException%7B
 %0A%09%09I
@@ -1118,74 +1118,1547 @@
 %7B%0A%09%09
-IModel backend = new Backend();%0A%09%09backend.execute(%22!!@#!@$!@#$@#$%22
+backend.execute(%22!!@#!@$!@#$@#$%22);%0A%09%7D%0A%09@Test%0A%09public void testMake() throws Exception %7B%0A%09%09IExecutionContext result = backend.execute(%22Make :foo bar%22);%0A%09%09assertEquals(%22bar%22, result.environment().get(%22foo%22));%0A%09%7D%0A%09@Test%0A%09public void testRepeat() throws Exception %7B%0A%09%09IExecutionContext result = backend.execute(%22Repeat 3 %5B Sum 1 1 %5D%22);%0A%09%09assertEquals(%226.0%22, result.environment().get(Constants.RETURN_VALUE_ENVIRONMENT));%0A%09%7D%0A%09@Test%0A%09public void testDoTimes() throws Exception %7B%0A%09%09IExecutionContext result = backend.execute(%22DoTimes %5B :foo 3 %5D %5B Sum :foo :foo%5D%22);%0A%09%09assertEquals(%2212.0%22, result.environment().get(Constants.RETURN_VALUE_ENVIRONMENT));%0A%09%7D%0A%09@Test%0A%09public void testFor() throws Exception %7B%0A%09%09IExecutionContext result = backend.execute(%22For %5B :foo 3 6 2 %5D %5B Sum :foo :foo %5D%22);%0A%09%09assertEquals(%2216.0%22, result.environment().get(Constants.RETURN_VALUE_ENVIRONMENT));%0A%09%7D%0A%09@Test%0A%09public void testIf() throws Exception %7B%0A%09%09IExecutionContext result = backend.execute(%22If 0 %5B Minus 1 %5D%22);%0A%09%09assertNotEquals(%22-1.0%22, result.environment().get(Constants.RETURN_VALUE_ENVIRONMENT));%0A%09%7D%0A%09@Test%0A%09public void testIfElse() throws Exception %7B%0A%09%09IExecutionContext result = backend.execute(%22IfElse 0 %5B Minus 1 %5D %5B Minus 2 %5D%22);%0A%09%09assertEquals(%22-2.0%22, result.environment().get(Constants.RETURN_VALUE_ENVIRONMENT));%0A%09%7D%0A%09@Test%0A%09public void testTo() throws Exception %7B%0A%09%09backend.execute(%22To foo %5B :bar %5D %5B Minus :bar %5D%22);%0A%09%09IExecutionContext result = backend.execute(%22foo 1%22);%0A%09%09assertEquals(%22-1.0%22, result.environment().get(Constants.RETURN_VALUE_ENVIRONMENT)
 );%0A%09

add tests for control commands