<commit_before>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);
    }

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

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

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

        testScript(data, transformed, script);
    }

    @Test
    public void testLetSetMove() throws Exception
    {
        String data = "{\"key0\":\"value0\"}";

        String script = "mode normal " +
                "let var = * key0 " +
                "set var -> key1 " +
                "set literal -> key2 ";

        String transformed = "" +
                "{" +
                "    \"key0\":\"value0\"," +
                "    \"key1\":\"value0\"," +
                "    \"key2\":\"literal\"" +
                "}";

        testScript(data, transformed, script);
    }

    @Test
    public void testBasicParenthesis() throws Exception
    {
        String data = "{\"key0\":\"value0\"}";

        String script = "mode normal " +
                "let var = ( * key0 ) " +
                "set ( var ) -> key1 ";

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

        testScript(data, transformed, script);
    }

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

        String script = "mode normal " +
                "let x = 1 + 1 " +
                "set x -> key0 " +
                "let y = 1 + 1 + 2 " +
                "set y -> key1 " +
                "let z = ( 1 + 1 ) * 3 " +
                "set z -> key2 ";

        String transformed = "" +
                "{" +
                "    \"key0\":2," +
                "    \"key1\":4," +
                "    \"key2\":6" +
                "}";

        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);<commit_msg>Add transform script sizeof test case.<commit_after>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);
    }

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

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

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

        testScript(data, transformed, script);
    }

    @Test
    public void testLetSetMove() throws Exception
    {
        String data = "{\"key0\":\"value0\"}";

        String script = "mode normal " +
                "let var = * key0 " +
                "set var -> key1 " +
                "set literal -> key2 ";

        String transformed = "" +
                "{" +
                "    \"key0\":\"value0\"," +
                "    \"key1\":\"value0\"," +
                "    \"key2\":\"literal\"" +
                "}";

        testScript(data, transformed, script);
    }

    @Test
    public void testBasicParenthesis() throws Exception
    {
        String data = "{\"key0\":\"value0\"}";

        String script = "mode normal " +
                "let var = ( * key0 ) " +
                "set ( var ) -> key1 ";

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

        testScript(data, transformed, script);
    }

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

        String script = "mode normal " +
                "let x = 1 + 1 " +
                "set x -> key0 " +
                "let y = 1 + 1 + 2 " +
                "set y -> key1 " +
                "let z = ( 1 + 1 ) * 3 " +
                "set z -> key2 ";

        String transformed = "" +
                "{" +
                "    \"key0\":2," +
                "    \"key1\":4," +
                "    \"key2\":6" +
                "}";

        testScript(data, transformed, script);
    }

    @Test
    public void testSizeOf() throws Exception
    {
        String data = "" +
                "{" +
                "   \"somelist\":" +
                "   [" +
                "       {\"somekey\":\"somevalue\"}," +
                "       {\"somekey\":\"somevalue\"}," +
                "       {\"somekey\":\"somevalue\"}" +
                "   ]" +
                "}";

        String script = "mode normal " +
                "let x = sizeof somelist * 2 " +
                "delete somelist " +
                "set x -> result ";

        String transformed = "" +
                "{" +
                "    \"result\":6" +
                "}";

        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);
{'fraction_non_alphanumeric': 0.14236681092374273, 'fraction_numerical': 0.013430831219221012, 'mean_word_length': 1.292065663474692, 'pattern_counts': {'":': 29, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '408910', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1763, 'n_words': 408}