package net.zerobuilder.compiler;

import com.google.common.collect.ImmutableList;
import org.junit.Test;

import javax.tools.JavaFileObject;
import java.util.Arrays;
import java.util.List;

import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaFileObjects.forSourceLines;
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
import static java.util.Collections.singletonList;

public class FailTest {

  @Test
  public void twoUnnamedConstructors() {
    String badLine = "  @Builder Centipede(int a) {}";
    List<String> sourceLines = Arrays.asList(
        "package test;",
        "import net.zerobuilder.*;",
        "class Centipede {",
        "  @Builder Centipede(int a, int b) {}",
        badLine,
        "}");
    JavaFileObject javaFile = forSourceLines("test.Centipede", sourceLines);
    int line = sourceLines.indexOf(badLine);
    assertAbout(javaSources()).that(singletonList(javaFile))
        .processedWith(new ZeroProcessor())
        .failsToCompile()
        .withErrorContaining("another goal with this name")
        .in(javaFile)
        .onLine(line + 1);
  }

  @Test
  public void constructorVersusFactory() {
    String badLine = "  @Builder static Centipede create (int a) {}";
    List<String> sourceLines = Arrays.asList(
        "package test;",
        "import net.zerobuilder.*;",
        "class Centipede {",
        "  @Builder Centipede(int a) {}",
        badLine,
        "}");
    JavaFileObject javaFile = forSourceLines("test.Centipede", sourceLines);
    int line = sourceLines.indexOf(badLine);
    assertAbout(javaSources()).that(ImmutableList.of(javaFile))
        .processedWith(new ZeroProcessor())
        .failsToCompile()
        .withErrorContaining("another goal with this name")
        .in(javaFile)
        .onLine(line + 1);
  }
}

@@ -524,63 +524,8 @@
 ) %7B%0A
-    String badLine = %22  @Builder Centipede(int a) %7B%7D%22;%0A
     
@@ -558,32 +558,32 @@
  Arrays.asList(%0A
+
         %22package
@@ -706,39 +706,64 @@
 b) %7B%7D%22,%0A        
-badLine
+%22  @Builder Centipede(int a) %7B%7D%22
 ,%0A        %22%7D%22);%0A
@@ -843,53 +843,8 @@
 s);%0A
-    int line = sourceLines.indexOf(badLine);%0A
     
@@ -1055,34 +1055,8 @@
 ile)
-%0A        .onLine(line + 1)
 ;%0A  
@@ -1113,78 +1113,8 @@
 ) %7B%0A
-    String badLine = %22  @Builder static Centipede create (int a) %7B%7D%22;%0A
     
@@ -1296,23 +1296,63 @@
         
-badLine
+%22  @Builder static Centipede create (int a) %7B%7D%22
 ,%0A      
@@ -1440,53 +1440,8 @@
 s);%0A
-    int line = sourceLines.indexOf(badLine);%0A
     
@@ -1655,33 +1655,561 @@
 ile)
+;
 %0A  
-      .onLine(line + 1
+%7D%0A%0A  @Test%0A  public void missingProjection() %7B%0A    List%3CString%3E sourceLines = Arrays.asList(%0A        %22package test;%22,%0A        %22import net.zerobuilder.*;%22,%0A        %22class Bu %7B%22,%0A        %22  final int foo = 5;%22,%0A        %22  @Updater Bu(int foo, int nah) %7B%7D%22,%0A        %22%7D%22);%0A    JavaFileObject javaFile = forSourceLines(%22test.Bu%22, sourceLines);%0A    assertAbout(javaSources()).that(ImmutableList.of(javaFile))%0A        .processedWith(new ZeroProcessor())%0A        .failsToCompile()%0A        .withErrorContaining(%22Missing projection: nah%22)%0A        .in(javaFile
 );%0A 

test missing projection