package uk.co.benjiweber.expressions;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import static uk.co.benjiweber.expressions.Using.using;

public class UsingTest {

    static class Foo implements AutoCloseable {

        public String result() {
            return "result";
        }

        boolean closed = false;
        public void close() throws Exception {
            closed = true;
        }
    }

    @Test public void should_return_value() {
        String result = using(Foo::new, foo -> {
            return foo.result();
        });
    }

    @Test public void should_close_closeable() {
        Foo closeable = using(Foo::new, foo -> {
            return foo;
        });

        Assert.assertTrue(closeable.closed);
    }

    Foo foo = new Foo();
    Foo getClosable() {
        return foo;
    }

    @Test public void should_close_closeable_when_exception() {
        try {
            using(this::getClosable, foo -> {
                if (true) throw new NullPointerException();
                return "";
            });
        } catch (NullPointerException e) {
            // Expected
        }

        Assert.assertTrue(foo.closed);
    }

}

@@ -201,253 +201,8 @@
  %7B%0A%0A
-    static class Foo implements AutoCloseable %7B%0A%0A        public String result() %7B%0A            return %22result%22;%0A        %7D%0A%0A        boolean closed = false;%0A        public void close() throws Exception %7B%0A            closed = true;%0A        %7D%0A    %7D%0A%0A
     
@@ -239,24 +239,24 @@
 n_value() %7B%0A
+
         Stri
@@ -535,84 +535,8 @@
  %7D%0A%0A
-    Foo foo = new Foo();%0A    Foo getClosable() %7B%0A        return foo;%0A    %7D%0A%0A
     
@@ -832,24 +832,24 @@
 %0A        %7D%0A%0A
-
         Asse
@@ -882,10 +882,331 @@
 %0A    %7D%0A%0A
+    Foo foo = new Foo();%0A    Foo getClosable() %7B%0A        return foo;%0A    %7D%0A%0A    static class Foo implements AutoCloseable %7B%0A%0A        public String result() %7B%0A            return %22result%22;%0A        %7D%0A%0A        boolean closed = false;%0A        public void close() throws Exception %7B%0A            closed = true;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Move tests to the top