package io.norberg.automatter;

import com.google.common.base.Optional;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

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

public class GuavaOptionalTest {

  public @Rule ExpectedException expectedException = ExpectedException.none();

  @AutoMatter
  interface GuavaOptionals {

    Optional<String> foo();
    Optional<String> bar();
  }

  GuavaOptionalsBuilder builder;

  @Before
  public void setup() {
    builder = new GuavaOptionalsBuilder();
  }

  @Test
  public void testDefaults() {
    final GuavaOptionals foobar = builder.build();
    assertThat(foobar.foo(), is(Optional.<String>absent()));
    assertThat(foobar.bar(), is(Optional.<String>absent()));
  }

  @Test
  public void verifySettingNullGivesAbsent() {
    builder.foo((String)null);
    final GuavaOptionals foobar = builder.build();
    assertThat(foobar.foo(), is(Optional.<String>absent()));
  }

  @Test
  public void verifySettingValueGivesPresent() {
    builder.foo("bar");
    final GuavaOptionals foobar = builder.build();
    assertThat(foobar.foo(), is(Optional.of("bar")));
  }
}

@@ -180,16 +180,98 @@
 ption;%0A%0A
+import javax.annotation.Nullable;%0A%0Aimport static org.hamcrest.Matchers.nullValue;%0A
 import s
@@ -301,16 +301,16 @@
 .Is.is;%0A
-
 import s
@@ -554,24 +554,63 @@
 ring%3E bar();
+%0A%0A    @Nullable Optional%3CString%3E baz();
 %0A  %7D%0A%0A  Guav
@@ -923,32 +923,79 @@
 ing%3Eabsent()));%0A
+    assertThat(foobar.baz(), is(nullValue()));%0A
   %7D%0A%0A  @Test%0A  p
@@ -1310,32 +1310,32 @@
 uilder.build();%0A
-
     assertThat(f
@@ -1375,11 +1375,441 @@
 )));%0A  %7D
+%0A%0A  @Test%0A  public void verifySettingNonNullableNullNPEs() %7B%0A    expectedException.expect(NullPointerException.class);%0A    expectedException.expectMessage(%22foo%22);%0A    builder.foo((Optional%3CString%3E)null);%0A  %7D%0A%0A  @Test%0A  public void verifySettingNullableNull() %7B%0A    builder.bar(%22hello%22);%0A    builder.baz((Optional%3CString%3E) null);%0A    final GuavaOptionals foobar = builder.build();%0A    assertThat(foobar.baz(), is(nullValue()));%0A  %7D
 %0A%7D%0A

test nullable optional