3030class CopilotExperimentalProcessorTest {
3131
3232 private static final String EXPERIMENTAL_TYPE_SOURCE = """
33- package test;
34- import com.github.copilot.CopilotExperimental;
35- @CopilotExperimental
36- public class ExperimentalType {
37- public void doSomething() {}
38- }
39- """ ;
33+ package test;
34+ import com.github.copilot.CopilotExperimental;
35+ @CopilotExperimental
36+ public class ExperimentalType {
37+ public void doSomething() {}
38+ }
39+ """ ;
4040
4141 private static final String EXPERIMENTAL_METHOD_SOURCE = """
42- package test;
43- import com.github.copilot.CopilotExperimental;
44- public class StableType {
45- @CopilotExperimental
46- public static void experimentalMethod() {}
47- }
48- """ ;
42+ package test;
43+ import com.github.copilot.CopilotExperimental;
44+ public class StableType {
45+ @CopilotExperimental
46+ public static void experimentalMethod() {}
47+ }
48+ """ ;
4949
5050 private static final String CONSUMER_USES_TYPE_IN_DECLARATIONS = """
51- package consumer;
52- import test.ExperimentalType;
53- public class Consumer {
54- private ExperimentalType field;
55- public ExperimentalType getIt() { return field; }
56- public void setIt(ExperimentalType value) { this.field = value; }
57- }
58- """ ;
51+ package consumer;
52+ import test.ExperimentalType;
53+ public class Consumer {
54+ private ExperimentalType field;
55+ public ExperimentalType getIt() { return field; }
56+ public void setIt(ExperimentalType value) { this.field = value; }
57+ }
58+ """ ;
5959
6060 private static final String CONSUMER_EXTENDS_TYPE = """
61- package consumer;
62- import test.ExperimentalType;
63- public class Consumer extends ExperimentalType {
64- }
65- """ ;
61+ package consumer;
62+ import test.ExperimentalType;
63+ public class Consumer extends ExperimentalType {
64+ }
65+ """ ;
6666
6767 @ Test
6868 void failsByDefault_whenFieldOrSignatureUsesExperimentalType () {
6969 DiagnosticCollector <JavaFileObject > diagnostics = compile (
70- List .of (
71- inMemorySource ("test.ExperimentalType" , EXPERIMENTAL_TYPE_SOURCE ),
72- inMemorySource ("consumer.Consumer" , CONSUMER_USES_TYPE_IN_DECLARATIONS )
73- ),
74- Collections .emptyList ()
75- );
70+ List .of (inMemorySource ("test.ExperimentalType" , EXPERIMENTAL_TYPE_SOURCE ),
71+ inMemorySource ("consumer.Consumer" , CONSUMER_USES_TYPE_IN_DECLARATIONS )),
72+ Collections .emptyList ());
7673
7774 boolean hasError = diagnostics .getDiagnostics ().stream ()
78- .anyMatch (d -> d .getKind () == Diagnostic .Kind .ERROR
79- && d .getMessage (null ).contains ("experimental API" ));
80- assertTrue (hasError , "Expected compile error for experimental type in declarations, got: "
81- + diagnostics .getDiagnostics ());
75+ .anyMatch (d -> d .getKind () == Diagnostic .Kind .ERROR && d .getMessage (null ).contains ("experimental API" ));
76+ assertTrue (hasError ,
77+ "Expected compile error for experimental type in declarations, got: " + diagnostics .getDiagnostics ());
8278 }
8379
8480 @ Test
8581 void failsByDefault_whenExtendingExperimentalType () {
8682 DiagnosticCollector <JavaFileObject > diagnostics = compile (
87- List .of (
88- inMemorySource ("test.ExperimentalType" , EXPERIMENTAL_TYPE_SOURCE ),
89- inMemorySource ("consumer.Consumer" , CONSUMER_EXTENDS_TYPE )
90- ),
91- Collections .emptyList ()
92- );
83+ List .of (inMemorySource ("test.ExperimentalType" , EXPERIMENTAL_TYPE_SOURCE ),
84+ inMemorySource ("consumer.Consumer" , CONSUMER_EXTENDS_TYPE )),
85+ Collections .emptyList ());
9386
9487 boolean hasError = diagnostics .getDiagnostics ().stream ()
95- .anyMatch (d -> d .getKind () == Diagnostic .Kind .ERROR
96- && d .getMessage (null ).contains ("experimental API" ));
97- assertTrue (hasError , "Expected compile error for extending experimental type, got: "
98- + diagnostics .getDiagnostics ());
88+ .anyMatch (d -> d .getKind () == Diagnostic .Kind .ERROR && d .getMessage (null ).contains ("experimental API" ));
89+ assertTrue (hasError ,
90+ "Expected compile error for extending experimental type, got: " + diagnostics .getDiagnostics ());
9991 }
10092
10193 @ Test
10294 void passes_whenOptInFlagIsProvided () {
10395 DiagnosticCollector <JavaFileObject > diagnostics = compile (
104- List .of (
105- inMemorySource ("test.ExperimentalType" , EXPERIMENTAL_TYPE_SOURCE ),
106- inMemorySource ("test.StableType" , EXPERIMENTAL_METHOD_SOURCE ),
107- inMemorySource ("consumer.Consumer" , CONSUMER_USES_TYPE_IN_DECLARATIONS )
108- ),
109- List .of ("-Acopilot.experimental.allowed=true" )
110- );
96+ List .of (inMemorySource ("test.ExperimentalType" , EXPERIMENTAL_TYPE_SOURCE ),
97+ inMemorySource ("test.StableType" , EXPERIMENTAL_METHOD_SOURCE ),
98+ inMemorySource ("consumer.Consumer" , CONSUMER_USES_TYPE_IN_DECLARATIONS )),
99+ List .of ("-Acopilot.experimental.allowed=true" ));
111100
112- boolean hasError = diagnostics .getDiagnostics ().stream ()
113- .anyMatch (d -> d .getKind () == Diagnostic .Kind .ERROR );
114- assertFalse (hasError , "Expected no errors with opt-in flag, got: "
115- + diagnostics .getDiagnostics ());
101+ boolean hasError = diagnostics .getDiagnostics ().stream ().anyMatch (d -> d .getKind () == Diagnostic .Kind .ERROR );
102+ assertFalse (hasError , "Expected no errors with opt-in flag, got: " + diagnostics .getDiagnostics ());
116103 }
117104
118- private DiagnosticCollector <JavaFileObject > compile (
119- List <JavaFileObject > sources , List <String > extraOptions ) {
105+ private DiagnosticCollector <JavaFileObject > compile (List <JavaFileObject > sources , List <String > extraOptions ) {
120106 JavaCompiler compiler = ToolProvider .getSystemJavaCompiler ();
121107 DiagnosticCollector <JavaFileObject > diagnostics = new DiagnosticCollector <>();
122108
@@ -127,8 +113,7 @@ private DiagnosticCollector<JavaFileObject> compile(
127113 options .addAll (List .of ("-d" , System .getProperty ("java.io.tmpdir" )));
128114 options .addAll (extraOptions );
129115
130- JavaCompiler .CompilationTask task = compiler .getTask (
131- null , null , diagnostics , options , null , sources );
116+ JavaCompiler .CompilationTask task = compiler .getTask (null , null , diagnostics , options , null , sources );
132117 task .setProcessors (List .of (new CopilotExperimentalProcessor ()));
133118 task .call ();
134119
@@ -156,10 +141,8 @@ private static String resolveClasspath() {
156141 }
157142
158143 private static JavaFileObject inMemorySource (String className , String code ) {
159- return new SimpleJavaFileObject (
160- URI .create ("string:///" + className .replace ('.' , '/' ) + ".java" ),
161- JavaFileObject .Kind .SOURCE
162- ) {
144+ return new SimpleJavaFileObject (URI .create ("string:///" + className .replace ('.' , '/' ) + ".java" ),
145+ JavaFileObject .Kind .SOURCE ) {
163146 @ Override
164147 public CharSequence getCharContent (boolean ignoreEncodingErrors ) {
165148 return code ;
0 commit comments