package org.nutz.el;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.nutz.lang.Lang;
import org.nutz.lang.util.Context;

public class ElTest {

	@Test
	public void test_array_access() {
		Context context = new Context();
		context.set("a", Lang.array(3, 5, 7));
		assertEquals(3, El.eval(context, "a[0]").getInteger().intValue());
		assertEquals(5, El.eval(context, "a[1]").getInteger().intValue());
		assertEquals(7, El.eval(context, "a[2]").getInteger().intValue());
		assertEquals(15, El.eval(context, "a[0]+a[1]+a[2]").getInteger().intValue());
		assertEquals(56, El.eval(context, "(a[0]+a[1])*a[2]").getInteger().intValue());
	}

	@Test
	public void test_global_invoke() {
		assertEquals("abc", El.eval("trim(' abc  ')").getString());
	}

	@Test
	public void test_simple_invoke() {
		Context context = new Context();
		context.set("a", Lang.map("{x:10,y:50,txt:'Hello'}"));

		assertEquals(100, El.eval(context, "a.get('x')*10").getInteger().intValue());
		assertEquals(49, El.eval(context, "a.get('y')-1").getInteger().intValue());
		assertEquals("Hello-40", El.eval(context, "a.get('txt')+(a.get('x')-a.get('y'))")
									.getString());
		assertEquals("Hello", El.eval(context, "a.get('txt')").getString());
		assertEquals(3, El.eval(context, "a.size()").getInteger().intValue());
	}

	@Test
	public void test_simple_count() {
		assertEquals((3 + 2 * 5), El.eval("3+2*5").getInteger().intValue());
		assertEquals(((3 + 2) * 5), El.eval("(3+2)*5").getInteger().intValue());

		assertEquals(Float.valueOf(.3f + .2f * .5f), El.eval(".3+.2*.5").getFloat());
		assertEquals(Float.valueOf((.5f + .1f) * .9f), El.eval("(.5 + 0.1)*.9").getFloat());
	}

	@Test
	public void test_invoke() {
		Context context = new Context();
		List<String> list = new ArrayList<String>();
		context.set("b", list);
		assertEquals(0, El.eval(context, "b.size()").getInteger().intValue());
		list.add("");
		assertEquals(1, El.eval(context, "b.size()").getInteger().intValue());
	}

}

@@ -2023,18 +2023,384 @@
 ntValue());%0A
+%09%09%0A%09%09El.eval(context, %22b.add('Q%5CnQ')%22);%0A%09%09assertEquals(2, El.eval(context, %22b.size()%22).getInteger().intValue());%0A%09%09assertFalse(El.eval(context, %22false%22).getBoolean());%0A%09%09%0A%09%09//TODO %E4%B8%80%E4%B8%AA%E4%B8%AA%E6%9D%A5,4%E4%B8%AA%E9%83%BD%E6%8A%A5%E9%94%99,%E5%86%99%E6%B3%95%E4%B8%8D%E5%AF%B9?%0A%0A%09%09//El.eval(context, %22b%5B0%5D.toString()%22);%0A%09%09El.eval(context, %22b.get(0).toString()%22);%0A%09%09//El.eval(context, %22b%5B0%5D.equals(b%5B0%5D)%22);%0A%09%09//El.eval(context, %22b%5B0%5D.equals('')%22);%0A
 %09%7D%0A%0A%7D%0A

修改一下测试用例,再次击垮EL的代码!^_^