package de.davherrmann.efficiently.immutable;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import java.util.Map;

import org.junit.Test;

import com.google.gson.Gson;

public class ImmutableTypeAdapterTest
{
    private final Immutable<POJO> immutable = new Immutable<>(POJO.class);
    private final POJO path = immutable.path();
    private final Gson gson = new Gson();

    @Test
    public void write_withEmptyImmutable_writesEmptyObject() throws Exception
    {
        // when / then
        assertThat(gson.toJson(immutable), is("{}"));
    }
    @Test
    public void write_setInCustomType_works() throws Exception
    {
        // when
        final Immutable<POJO> newImmutable = immutable.in(path.name().firstname()).set("Foo");

        // then
        assertThat(gson.toJson(newImmutable), is("{\"name\":{\"firstname\":\"Foo\"}}"));
    }

    @Test
    public void write_overwriteCustomType_works() throws Exception
    {
        // when
        final Immutable<POJO> newImmutable = immutable.in(path.name()).set(name("Foo", "Bar"));

        // then
        assertThat(gson.toJson(newImmutable), is("{\"name\":{\"firstname\":\"Foo\",\"lastname\":\"Bar\"}}"));
    }

    private Immutable<POJO.Name> name(String firstname, String lastname)
    {
        final Immutable<POJO.Name> immutable = new Immutable<>(POJO.Name.class);
        final POJO.Name path = immutable.path();

        return immutable //
            .in(path.firstname()).set(firstname) //
            .in(path.lastname()).set(lastname);
    }

    private interface POJO
    {
        String title();

        boolean wantToClose();

        int currentPage();

        Map<String, String> myMap();

        POJO pojo();

        Name[] names();

        Name name();

        interface Name
        {
            String firstname();

            String lastname();
        }
    }
}
@@ -724,32 +724,48 @@
 able = immutable
+ //%0A            
 .in(path.name().
@@ -938,27 +938,397 @@
 ite_
-overwriteCustomType
+setCustomTypeObject_works() throws Exception%0A    %7B%0A        // when%0A        final Immutable%3CPOJO%3E newImmutable = immutable //%0A            .in(path.name()).set(name(%22Foo%22, %22Bar%22));%0A%0A        // then%0A        assertThat(gson.toJson(newImmutable), is(%22%7B%5C%22name%5C%22:%7B%5C%22firstname%5C%22:%5C%22Foo%5C%22,%5C%22lastname%5C%22:%5C%22Bar%5C%22%7D%7D%22));%0A    %7D%0A%0A    @Test%0A    public void write_setInCustomType_withPreviousCustomTypeObject
 _wor
@@ -1425,16 +1425,32 @@
 mmutable
+ //%0A            
 .in(path
@@ -1473,32 +1473,84 @@
 e(%22Foo%22, %22Bar%22))
+ //%0A            .in(path.name().lastname()).set(%22B%22)
 ;%0A%0A        // th
@@ -1643,34 +1643,32 @@
 %5C%22lastname%5C%22:%5C%22B
-ar
 %5C%22%7D%7D%22));%0A    %7D%0A%0A

add test case for TypeAdapter