package com.github.rschmitt.dynamicobject;

import org.junit.Test;

import java.util.Optional;

import static com.github.rschmitt.dynamicobject.DynamicObject.deserialize;
import static com.github.rschmitt.dynamicobject.DynamicObject.newInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class OptionalTest {
    @Test
    public void valuePresent() {
        OptWrapper instance = deserialize("{:str \"value\"}", OptWrapper.class);
        OptWrapper expected = newInstance(OptWrapper.class).str(Optional.of("value"));

        assertEquals("value", instance.str().get());
        assertEquals(expected, instance);
    }

    @Test
    public void valueMissing() {
        OptWrapper instance = deserialize("{:str nil}", OptWrapper.class);
        OptWrapper expected = newInstance(OptWrapper.class).str(Optional.empty());

        assertFalse(instance.str().isPresent());
        assertEquals(expected, instance);
    }

    @Test
    public void intPresent() {
        OptWrapper instance = deserialize("{:i 24601}", OptWrapper.class);
        OptWrapper expected = newInstance(OptWrapper.class).i(Optional.of(24601));

        assertEquals(Integer.valueOf(24601), instance.i().get());
        assertEquals(expected, instance);
    }
}

interface OptWrapper extends DynamicObject<OptWrapper> {
    Optional<String> str();
    Optional<Integer> i();

    OptWrapper str(Optional<String> str);
    OptWrapper i(Optional<Integer> i);
}
@@ -61,24 +61,75 @@
 it.Test;%0D%0A%0D%0A
+import java.time.Instant;%0D%0Aimport java.util.List;%0D%0A
 import java.
@@ -294,24 +294,138 @@
 wInstance;%0D%0A
+import static com.github.rschmitt.dynamicobject.DynamicObject.serialize;%0D%0Aimport static java.util.Arrays.asList;%0D%0A
 import stati
@@ -1492,209 +1492,1730 @@
  %7D%0D%0A
-%7D%0D%0A%0D%0Ainterface OptWrapper extends DynamicObject%3COptWrapper%3E %7B%0D%0A    Optional%3CString%3E str();%0D%0A    Optional%3CInteger%3E i();%0D%0A%0D%0A    OptWrapper str(Optional%3CString%3E str);%0D%0A    OptWrapper i(Optional%3CInteger%3E i
+%0D%0A    @Test%0D%0A    public void listPresent() %7B%0D%0A        OptWrapper instance = deserialize(%22%7B:ints %5B1 2 3%5D%7D%22, OptWrapper.class);%0D%0A        OptWrapper expected = newInstance(OptWrapper.class).ints(Optional.of(asList(1, 2, 3)));%0D%0A%0D%0A        assertEquals(asList(1, 2, 3), instance.ints().get());%0D%0A        assertEquals(expected, instance);%0D%0A    %7D%0D%0A%0D%0A    @Test%0D%0A    public void instantPresent() %7B%0D%0A        String edn = %22%7B:inst #inst %5C%221985-04-12T23:20:50.520-00:00%5C%22%7D%22;%0D%0A        Instant expected = Instant.parse(%221985-04-12T23:20:50.52Z%22);%0D%0A%0D%0A        OptWrapper instance = deserialize(edn, OptWrapper.class);%0D%0A%0D%0A        assertEquals(expected, instance.inst().get());%0D%0A        assertEquals(edn, serialize(instance));%0D%0A    %7D%0D%0A%0D%0A    @Test%0D%0A    public void dynamicObjectPresent() %7B%0D%0A        DynamicObject.registerTag(OptWrapper.class, %22OptWrapper%22);%0D%0A%0D%0A        OptWrapper instance = deserialize(%22#OptWrapper%7B:wrapper #OptWrapper%7B:i 24%7D%7D%22, OptWrapper.class);%0D%0A        OptWrapper expected = newInstance(OptWrapper.class).wrapper(Optional.of(newInstance(OptWrapper.class).i(Optional.of(24))));%0D%0A%0D%0A        assertEquals(expected.wrapper().get(), instance.wrapper().get());%0D%0A        assertEquals(expected, instance);%0D%0A%0D%0A        DynamicObject.deregisterTag(OptWrapper.class);%0D%0A    %7D%0D%0A%7D%0D%0A%0D%0Ainterface OptWrapper extends DynamicObject%3COptWrapper%3E %7B%0D%0A    Optional%3CString%3E str();%0D%0A    Optional%3CInteger%3E i();%0D%0A    Optional%3CList%3CInteger%3E%3E ints();%0D%0A    Optional%3CInstant%3E inst();%0D%0A    Optional%3COptWrapper%3E wrapper();%0D%0A%0D%0A    OptWrapper str(Optional%3CString%3E str);%0D%0A    OptWrapper i(Optional%3CInteger%3E i);%0D%0A    OptWrapper ints(Optional%3CList%3CInteger%3E%3E ints);%0D%0A    OptWrapper inst(Optional%3CInstant%3E inst);%0D%0A    OptWrapper wrapper(Optional%3COptWrapper%3E wrapper
 );%0D%0A

Add more tests for Optional wrapping