package transform;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import whelk.util.TransformScript;
import java.util.Map;

public class ScriptTest
{
    static ObjectMapper mapper = new ObjectMapper();

    @Test
    public void testBasicSet() throws Exception
    {
        String data = "{}";

        String script = "mode normal " +
                "set value0 -> key0";

        String transformed = "" +
                "{" +
                "    \"key0\":\"value0\"" +
                "}";

        testScript(data, transformed, script);
    }

    private void testScript(String beforeTransformText, String expectedResultText, String transformScript) throws Exception
    {
        Map oldData = mapper.readValue(beforeTransformText, Map.class);
        Map expectedResultData = mapper.readValue(expectedResultText, Map.class);

        TransformScript executableScript = new TransformScript(transformScript);
        Map transformed = executableScript.executeOn(oldData);

        if ( ! Utils.rdfEquals( expectedResultData, transformed ) )
        {
            System.out.println("expected transformation result:\n"+expectedResultData);
            System.out.println("\nactual transformation result:\n"+transformed);
            Assert.assertTrue( false );
        }
    }
}

@@ -594,16 +594,2001 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testBasicLet() throws Exception%0A    %7B%0A        String data = %22%7B%7D%22;%0A%0A        String script = %22mode normal %22 +%0A                %22set value0 -%3E key0 %22 +%0A                %22let x = 1%22;%0A%0A        String transformed = %22%22 +%0A                %22%7B%22 +%0A                %22    %5C%22key0%5C%22:%5C%22value0%5C%22%22 +%0A                %22%7D%22;%0A%0A        testScript(data, transformed, script);%0A    %7D%0A%0A    @Test%0A    public void testLetSetMove() throws Exception%0A    %7B%0A        String data = %22%7B%5C%22key0%5C%22:%5C%22value0%5C%22%7D%22;%0A%0A        String script = %22mode normal %22 +%0A                %22let var = * key0 %22 +%0A                %22set var -%3E key1 %22 +%0A                %22set literal -%3E key2 %22;%0A%0A        String transformed = %22%22 +%0A                %22%7B%22 +%0A                %22    %5C%22key0%5C%22:%5C%22value0%5C%22,%22 +%0A                %22    %5C%22key1%5C%22:%5C%22value0%5C%22,%22 +%0A                %22    %5C%22key2%5C%22:%5C%22literal%5C%22%22 +%0A                %22%7D%22;%0A%0A        testScript(data, transformed, script);%0A    %7D%0A%0A    @Test%0A    public void testBasicParenthesis() throws Exception%0A    %7B%0A        String data = %22%7B%5C%22key0%5C%22:%5C%22value0%5C%22%7D%22;%0A%0A        String script = %22mode normal %22 +%0A                %22let var = ( * key0 ) %22 +%0A                %22set ( var ) -%3E key1 %22;%0A%0A        String transformed = %22%22 +%0A                %22%7B%22 +%0A                %22    %5C%22key0%5C%22:%5C%22value0%5C%22,%22 +%0A                %22    %5C%22key1%5C%22:%5C%22value0%5C%22%22 +%0A                %22%7D%22;%0A%0A        testScript(data, transformed, script);%0A    %7D%0A%0A    @Test%0A    public void testBasicArithmetic() throws Exception%0A    %7B%0A        String data = %22%7B%7D%22;%0A%0A        String script = %22mode normal %22 +%0A                %22let x = 1 + 1 %22 +%0A                %22set x -%3E key0 %22 +%0A                %22let y = 1 + 1 + 2 %22 +%0A                %22set y -%3E key1 %22 +%0A                %22let z = ( 1 + 1 ) * 3 %22 +%0A                %22set z -%3E key2 %22;%0A%0A        String transformed = %22%22 +%0A                %22%7B%22 +%0A                %22    %5C%22key0%5C%22:2,%22 +%0A                %22    %5C%22key1%5C%22:4,%22 +%0A                %22    %5C%22key2%5C%22:6%22 +%0A                %22%7D%22;%0A%0A        testScript(data, transformed, script);%0A    %7D%0A%0A
     priv

Add more transform script tests.