package org.twig.functional;

import org.junit.Assert;
import org.junit.Test;
import org.twig.exception.TwigException;
import org.twig.exception.TwigRuntimeException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RenderDataStructuresTests extends FunctionalTests {
    @Test
    public void canRenderArray() throws TwigException {
        HashMap<String, String> templates = new HashMap<>();
        templates.put("foo.twig", "{{ ['foo', 'bar', 'baz'][1] }}");
        setupEnvironment(templates);

        Assert.assertEquals(
                "2nd element of array should be rendered",
                "bar",
                environment.render("foo.twig")
        );
    }

    @Test(expected = TwigRuntimeException.class)
    public void cantRenderArrayAccessToUndefinedKey() throws TwigException {
        HashMap<String, String> templates = new HashMap<>();
        templates.put("foo.twig", "{{ ['foo', 'bar', 'baz'][5] }}");
        setupEnvironment(templates);

        environment.render("foo.twig");
    }

    @Test
    public void canRenderArrayFromContext() throws TwigException {
        HashMap<String, String> templates = new HashMap<>();
        templates.put("foo.twig", "{{ foo[0] }}");
        setupEnvironment(templates);

        Map<String, List<String>> ctx = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add("bar");
        ctx.put("foo", list);

        Assert.assertEquals(
                "Content of called array element should be rendered",
                "bar",
                environment.render("foo.twig", ctx)
        );
    }

    @Test(expected = TwigRuntimeException.class)
    public void cantRenderArrayAccessToNonArray() throws TwigException {
        HashMap<String, String> templates = new HashMap<>();
        templates.put("foo.twig", "{{ foo[0] }}");
        setupEnvironment(templates);

        Map<String, Integer> ctx = new HashMap<>();
        ctx.put("foo", 1);

        environment.render("foo.twig", ctx);
    }
}

@@ -2058,11 +2058,463 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void canRenderHashmap() throws TwigException %7B%0A        HashMap%3CString, String%3E templates = new HashMap%3C%3E();%0A        templates.put(%22foo.twig%22, %22%7B%7B (%7Bfoo: 'bar': %7B baz: 'qux' %7D %7D).foo.bar %7D%7D%22);%0A        setupEnvironment(templates);%0A%0A        Assert.assertEquals(%0A                %22Contents of key inside hashmap inside hashmap should be rendered%22,%0A                %22qux%22,%0A                environment.render(%22foo.twig%22)%0A        );%0A    %7D
 %0A%7D%0A

Add test for hashmaps