package org.codingmatters.value.objects;

import org.codingmatters.tests.compile.CompiledCode;
import org.codingmatters.value.objects.spec.Spec;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.codingmatters.tests.reflect.ReflectMatchers.aClass;
import static org.codingmatters.tests.reflect.ReflectMatchers.anInterface;
import static org.codingmatters.value.objects.spec.Spec.spec;
import static org.codingmatters.value.objects.spec.ValueSpec.valueSpec;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

/**
 * Created by nelt on 9/13/16.
 */
public class SpecCodeGeneratorTest {

    @Rule
    public TemporaryFolder dir = new TemporaryFolder();

    private final Spec spec  = spec()
            .addValue(
                    valueSpec().name("val")
            )
            .build();

    @Test
    public void value_generatesInterface() throws Exception {
        new SpecCodeGenerator(this.spec, "org.generated").generateTo(dir.getRoot());
        CompiledCode compiled = CompiledCode.compile(this.dir.getRoot());

        assertThat(compiled.getClass("org.generated.Val"), is(anInterface().public_()));
    }

    @Test
    public void value_generatesBuilder() throws Exception {
        new SpecCodeGenerator(this.spec, "org.generated").generateTo(dir.getRoot());
        CompiledCode compiled = CompiledCode.compile(this.dir.getRoot());

        assertThat(compiled.getClass("org.generated.Val$Builder"), is(aClass()));
    }

    @Test
    public void value_generatesPackagePrivateImplementationClass() throws Exception {
        new SpecCodeGenerator(this.spec, "org.generated").generateTo(dir.getRoot());
        CompiledCode compiled = CompiledCode.compile(this.dir.getRoot());

        assertThat(compiled.getClass("org.generated.ValImpl"),
                is(
                        aClass().packagePrivate()
                                .implementing(compiled.getClass("org.generated.Val"))
                )
        );
    }
}
@@ -134,24 +134,49 @@
 .spec.Spec;%0A
+import org.junit.Before;%0A
 import org.j
@@ -902,59 +902,77 @@
 ();%0A
-%0A    @Test%0A    public void value_generatesInterface
+    private CompiledCode compiled;%0A%0A    @Before%0A    public void setUp
 () t
@@ -1078,37 +1078,29 @@
 ));%0A        
-CompiledCode 
+this.
 compiled = C
@@ -1132,32 +1132,100 @@
 dir.getRoot());%0A
+    %7D%0A%0A    @Test%0A    public void valueInterface() throws Exception %7B
 %0A        assertT
@@ -1336,26 +1336,16 @@
 id value
-_generates
 Builder(
@@ -1369,168 +1369,8 @@
 n %7B%0A
-        new SpecCodeGenerator(this.spec, %22org.generated%22).generateTo(dir.getRoot());%0A        CompiledCode compiled = CompiledCode.compile(this.dir.getRoot());%0A%0A
     
@@ -1443,16 +1443,36 @@
 aClass()
+.public_().static_()
 ));%0A    
@@ -1509,32 +1509,8 @@
 alue
-_generatesPackagePrivate
 Impl
@@ -1550,168 +1550,8 @@
 n %7B%0A
-        new SpecCodeGenerator(this.spec, %22org.generated%22).generateTo(dir.getRoot());%0A        CompiledCode compiled = CompiledCode.compile(this.dir.getRoot());%0A%0A
     

test refactoring