package com.ajlopez.ajtalk.language;

import static org.junit.Assert.*;

import org.junit.Test;

public class ExecutionBlockTests {
	@Test
	public void executeMethodWithSimpleReturn() {
		byte[] bytecodes = new byte[] { Bytecodes.GETARGUMENT, 0, Bytecodes.RETURN };
		Method method = new Method(1, 0, bytecodes );
		assertEquals(1, method.execute(null, new Object[] { 1 }));
	}

	@Test
	public void executeBlockWithSimpleReturn() {
		byte[] bytecodes = new byte[] { Bytecodes.GETARGUMENT, 0, Bytecodes.RETURN };
		Block block = new Block(1, 0, bytecodes );
		assertEquals(1, block.execute(new Object[] { 1 }));
	}

	@Test
	public void executeSetAndGetLocalWithSimpleReturn() {
		byte[] bytecodes = new byte[] { Bytecodes.GETARGUMENT, 0, Bytecodes.SETLOCAL, 0, Bytecodes.GETLOCAL, 0, Bytecodes.RETURN };
		Block block = new Block(1, 1, bytecodes );
		assertEquals(1, block.execute(new Object[] { 1 }));
	}

	@Test
	public void executeAddIntegers() {
		byte[] bytecodes = new byte[] { Bytecodes.GETARGUMENT, 0, Bytecodes.GETARGUMENT, 1, Bytecodes.ADD, Bytecodes.RETURN };
		Block block = new Block(1, 1, bytecodes );
		assertEquals(3, block.execute(new Object[] { 1, 2 }));
	}

	@Test
	public void executeSetVariable() {
		BaseClass behavior = new BaseClass(null, new String[] { "x", "y"} );
		BaseObject object = new BaseObject(behavior);
		
		byte[] bytecodes = new byte[] { Bytecodes.GETARGUMENT, 0, Bytecodes.SETVARIABLE, 0 };
		Method method = new Method(1, 1, bytecodes );
		assertEquals(object, method.execute(object, new Object[] { 10 }));
		assertEquals(10, object.getVariable(0));
		assertNull(object.getVariable(1));
	}
}

@@ -1667,12 +1667,541 @@
 ));%0D%0A%09%7D%0D
+%0A%0D%0A%09@Test%0D%0A%09public void executeSetVariables() %7B%0D%0A%09%09BaseClass behavior = new BaseClass(null, new String%5B%5D %7B %22x%22, %22y%22%7D );%0D%0A%09%09BaseObject object = new BaseObject(behavior);%0D%0A%09%09%0D%0A%09%09byte%5B%5D bytecodes = new byte%5B%5D %7B Bytecodes.GETARGUMENT, 0, Bytecodes.SETVARIABLE, 0, Bytecodes.GETARGUMENT, 1, Bytecodes.SETVARIABLE, 1 %7D;%0D%0A%09%09Method method = new Method(1, 1, bytecodes );%0D%0A%09%09assertEquals(object, method.execute(object, new Object%5B%5D %7B 10, 20 %7D));%0D%0A%09%09assertEquals(10, object.getVariable(0));%0D%0A%09%09assertEquals(20, object.getVariable(1));%0D%0A%09%7D%0D
 %0A%7D%0D%0A

Set Variables test