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();

    @Test
    public void value_generatesInterface() throws Exception {
        Spec spec = spec()
                .addValue(
                        valueSpec().name("val")
                )
                .build();
        new SpecCodeGenerator(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 {
        Spec spec = spec()
                .addValue(
                        valueSpec().name("val")
                )
                .build();
        new SpecCodeGenerator(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 {
        Spec spec = spec()
                .addValue(
                        valueSpec().name("val")
                )
                .build();
        new SpecCodeGenerator(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"))
                )
        );
    }
}
@@ -740,83 +740,21 @@
     
-@Test%0A    public void value_generatesInterface() throws Exception %7B%0A       
+private final
  Spe
@@ -752,32 +752,33 @@
 final Spec spec 
+ 
 = spec()%0A       
@@ -774,36 +774,32 @@
 c()%0A            
-    
 .addValue(%0A     
@@ -785,36 +785,32 @@
      .addValue(%0A
-    
                 
@@ -845,38 +845,38 @@
             
+)%0A
     
-)%0A
         
         .bui
@@ -859,32 +859,24 @@
             
-        
 .build();%0A  
@@ -864,32 +864,105 @@
        .build();
+%0A%0A    @Test%0A    public void value_generatesInterface() throws Exception %7B
 %0A        new Spe
@@ -968,32 +968,37 @@
 ecCodeGenerator(
+this.
 spec, %22org.gener
@@ -1276,154 +1276,8 @@
 n %7B%0A
-        Spec spec = spec()%0A                .addValue(%0A                        valueSpec().name(%22val%22)%0A                )%0A                .build();%0A
     
@@ -1294,32 +1294,37 @@
 ecCodeGenerator(
+this.
 spec, %22org.gener
@@ -1621,154 +1621,8 @@
 n %7B%0A
-        Spec spec = spec()%0A                .addValue(%0A                        valueSpec().name(%22val%22)%0A                )%0A                .build();%0A
     
@@ -1647,16 +1647,21 @@
 nerator(
+this.
 spec, %22o

test refactoring