package com.dafrito.lua.script;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Iterator;

import lua.LuaLibrary;

import org.junit.Before;
import org.junit.Test;

public class LuaTableTest {
	LuaLibrary lua = LuaLibrary.INSTANCE;
	private LuaBindings b;
	private LuaScriptContext ctx;

	@Test
	public void getAndPutAValueIntoATable() throws Exception {
		LuaTable t = new LuaTable(b);
		t.set(1, "No time");
		assertEquals("No time", t.get(1));
	}

	@Test
	public void sizeOfOneElementTableIsOne() throws Exception {
		LuaTable t = new LuaTable(b);
		t.add("No time");
		assertEquals(1, t.size());
	}
	
	@Test
	public void testRemovingAnElementMakesTheTableEmpty() throws Exception {
		LuaTable t = new LuaTable(b);
		t.add("No time");
		t.remove(0);
		assertTrue(t.isEmpty());
	}
	
	@Test
	public void iterateOverAnEmptyTable() throws Exception {
		LuaTable t = new LuaTable(b);
		assertFalse(t.iterator().hasNext());
	}
	
	@Test
	public void iterateOverAnOneElementTable() throws Exception {
		LuaTable t = new LuaTable(b);
		t.add("No time");
		t.add(42.0d);
		Iterator<Object> i = t.iterator();
		assertTrue(i.hasNext());
		assertEquals("No time", i.next());	
		assertEquals(42.0d, i.next());	
		assertFalse(i.hasNext());
	}

	@Test
	public void removeAnElementUsingAnIterator() throws Exception {
		LuaTable t = new LuaTable(b);
		t.add("A");
		t.add("B");
		t.add("C");
		int counter=0;
		for(Iterator<Object> i = t.iterator(); i.hasNext();) {
			counter++;
			Object v = i.next();
			if(v.equals("B")) {
				i.remove();
			}
		}
		assertEquals(3, counter);
		assertEquals(2, t.size());
	}
	
	
	@Before
	public void setup() {
		ctx = new LuaScriptContext();
		b = ctx.getGlobals();
	}
	
}

@@ -288,16 +288,60 @@
 eTest %7B%0A
+%09private static final String V = %22No time%22;%0A
 %09LuaLibr
@@ -371,16 +371,16 @@
 STANCE;%0A
-
 %09private
@@ -537,25 +537,17 @@
 .set(1, 
-%22No time%22
+V
 );%0A%09%09ass
@@ -556,25 +556,17 @@
 tEquals(
-%22No time%22
+V
 , t.get(
@@ -678,33 +678,25 @@
 b);%0A%09%09t.add(
-%22No time%22
+V
 );%0A%09%09assertE
@@ -837,33 +837,25 @@
 b);%0A%09%09t.add(
-%22No time%22
+V
 );%0A%09%09t.remov
@@ -1143,25 +1143,17 @@
 %09%09t.add(
-%22No time%22
+V
 );%0A%09%09t.a
@@ -1242,25 +1242,17 @@
 tEquals(
-%22No time%22
+V
 , i.next

Extract constant