package net.asfun.jangod.lib.tag;

import java.util.HashMap;
import java.util.Map;

import net.asfun.jangod.base.Context;
import net.asfun.jangod.interpret.InterpretException;
import net.asfun.jangod.interpret.JangodInterpreter;
import net.asfun.jangod.lib.TagLibrary;
import net.asfun.jangod.parse.TokenParser;
import android.test.AndroidTestCase;
import android.util.Log;

public class TagTest extends AndroidTestCase {

    static final String TAG = "TagTest";

    JangodInterpreter interpreter;

    @Override
    protected void setUp() throws Exception {

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("var1", "app_name");
        data.put("var2", "bg");

        Context context = new Context();
        context.initBindings(data, Context.SCOPE_GLOBAL);

        interpreter = new JangodInterpreter(context);
    }

    public void testResStrTag() throws Exception {
        Log.e(TAG, "testResStrTag");
        TagLibrary.addTag(new ResStrTag());
        String script = "{% rstr var1 %} is {% rstr 'app_name' %}";
        assertEquals("WifiShare is WifiShare", eval(script));
    }

    public void testResColorTag() throws Exception {
        Log.e(TAG, "testResColorTag");
        TagLibrary.addTag(new ResColorTag());
        String script = "{% rcolor var2 %} or {% rcolor 'bg' %}";
        Log.e(TAG, eval(script));
        assertEquals("#fffffbde or #fffffbde", eval(script));
    }

    public void testUUIDTag() throws Exception {
        Log.e(TAG, "testUUIDTag");
        TagLibrary.addTag(new UUIDTag());
        String script = "{% uuid %} or {% uuid %}";
        Log.e(TAG, eval(script));
    }

    private String eval(String script) throws InterpretException {
        TokenParser parser = new TokenParser(script);
        try {
            return interpreter.render(parser);
        } catch (InterpretException e) {
            throw e;
        }
    }

    @Override
    protected void tearDown() throws Exception {
        interpreter = null;
    }

}

@@ -324,20 +324,64 @@
 oid.
-test.Android
+app.Instrumentation;%0Aimport android.test.Instrumentation
 Test
@@ -445,15 +445,23 @@
 nds 
-Android
+Instrumentation
 Test
@@ -510,16 +510,46 @@
 Test%22;%0A%0A
+    Instrumentation mInstru;%0A%0A
     Jang
@@ -635,16 +635,56 @@
 eption %7B
+%0A        mInstru = getInstrumentation();
 %0A%0A      
@@ -807,18 +807,27 @@
 var2%22, %22
-bg
+result_view
 %22);%0A%0A   
@@ -1064,32 +1064,141 @@
 estResStrTag%22);%0A
+        mInstru.runOnMainSync(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         TagLibra
@@ -1217,32 +1217,40 @@
 w ResStrTag());%0A
+        
         String s
@@ -1313,161 +1313,393 @@
     
-assertEquals(%22WifiShare is WifiShare%22, eval(script));%0A    %7D%0A%0A    public void testResColorTag() throws Exception %7B%0A        Log.e(TAG, %22testResColorTag%22);%0A
+        try %7B%0A                    assertEquals(%22WebServ is WebServ%22, eval(script));%0A                %7D catch (InterpretException e) %7B%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public void testResColorTag() throws Exception %7B%0A        Log.e(TAG, %22testResColorTag%22);%0A        mInstru.runOnMainSync(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
     
@@ -1732,32 +1732,40 @@
 ResColorTag());%0A
+        
         String s
@@ -1809,17 +1809,60 @@
 or '
-bg
+result_view
 ' %25%7D%22;%0A
+                try %7B%0A            
     
@@ -1883,32 +1883,44 @@
  eval(script));%0A
+            
         assertEq
@@ -1930,44 +1930,137 @@
 s(%22#
-fffffbde or #fffffbde%22, eval(script)
+b0000000 or #b0000000%22, eval(script));%0A                %7D catch (InterpretException e) %7B%0A                %7D%0A            %7D%0A        %7D
 );%0A 

fix the old tag test