From 952f634b89d02aec781ab3bb7d02729b3b33c65f Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sun, 14 Apr 2024 14:04:13 +0200 Subject: [PATCH] Replace concatenates Strings by Text-Blocks in CompilerTests This improves the readability of Strings defining the test-input and expected output. It avoids the need to explicitly add new-line characters and the need to escape double-quotes. --- org.eclipse.xtext.xbase.tests/.classpath | 2 +- .../.settings/org.eclipse.jdt.core.prefs | 6 +- .../META-INF/MANIFEST.MF | 2 +- .../xbase/tests/compiler/Compiler2Tests.java | 3488 +++++++++-------- .../xbase/tests/compiler/CompilerTest.java | 1458 +++---- 5 files changed, 2546 insertions(+), 2410 deletions(-) diff --git a/org.eclipse.xtext.xbase.tests/.classpath b/org.eclipse.xtext.xbase.tests/.classpath index 208e191b577..87c648da44f 100644 --- a/org.eclipse.xtext.xbase.tests/.classpath +++ b/org.eclipse.xtext.xbase.tests/.classpath @@ -20,7 +20,7 @@ - + diff --git a/org.eclipse.xtext.xbase.tests/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.xtext.xbase.tests/.settings/org.eclipse.jdt.core.prefs index cb7cea341a0..d552ac6e151 100644 --- a/org.eclipse.xtext.xbase.tests/.settings/org.eclipse.jdt.core.prefs +++ b/org.eclipse.xtext.xbase.tests/.settings/org.eclipse.jdt.core.prefs @@ -24,9 +24,9 @@ org.eclipse.jdt.core.compiler.annotation.nonnullisdefault=disabled org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.compliance=17 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate @@ -132,7 +132,7 @@ org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=11 +org.eclipse.jdt.core.compiler.source=17 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false org.eclipse.jdt.core.formatter.alignment_for_additive_operator=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 diff --git a/org.eclipse.xtext.xbase.tests/META-INF/MANIFEST.MF b/org.eclipse.xtext.xbase.tests/META-INF/MANIFEST.MF index 0fda0e00b46..801365a50f4 100644 --- a/org.eclipse.xtext.xbase.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.xtext.xbase.tests/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: Xbase Runtime Tests Bundle-SymbolicName: org.eclipse.xtext.xbase.tests;singleton:=true Bundle-Version: 2.35.0.qualifier -Bundle-RequiredExecutionEnvironment: JavaSE-11 +Bundle-RequiredExecutionEnvironment: JavaSE-17 Require-Bundle: org.eclipse.xtext.xbase;bundle-version="2.35.0", org.eclipse.xtext;bundle-version="2.35.0", org.eclipse.xtext.testing;bundle-version="2.35.0", diff --git a/org.eclipse.xtext.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/Compiler2Tests.java b/org.eclipse.xtext.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/Compiler2Tests.java index ecbb8d09f51..9af6d971b18 100644 --- a/org.eclipse.xtext.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/Compiler2Tests.java +++ b/org.eclipse.xtext.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/Compiler2Tests.java @@ -3,7 +3,7 @@ * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. - * + * * SPDX-License-Identifier: EPL-2.0 */ package org.eclipse.xtext.xbase.tests.compiler; @@ -27,982 +27,1034 @@ protected FakeTreeAppendable createAppendable() { */ @Test public void testBug_424279() throws Exception { - compilesTo( - "{\n" + - " val treeIt = newArrayList.iterator;\n" + - " while (treeIt.hasNext) {\n" + - " val o = treeIt.next;\n" + - " if (o instanceof java.util.List) {\n" + - " return o;\n" + - " } else {\n" + - " return o;\n" + - " }\n" + - " }\n" + - " return null;\n" + - "}\n", - "\n" + - "final java.util.Iterator treeIt = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList().iterator();\n" + - "while (treeIt.hasNext()) {\n" + - " {\n" + - " final CharSequence o = treeIt.next();\n" + - " if ((o instanceof java.util.List)) {\n" + - " return o;\n" + - " } else {\n" + - " return o;\n" + - " }\n" + - " }\n" + - "}\n" + - "return null;\n"); + compilesTo(""" + { + val treeIt = newArrayList.iterator; + while (treeIt.hasNext) { + val o = treeIt.next; + if (o instanceof java.util.List) { + return o; + } else { + return o; + } + } + return null; + } + """, + """ + final java.util.Iterator treeIt = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList().iterator(); + while (treeIt.hasNext()) { + { + final CharSequence o = treeIt.next(); + if ((o instanceof java.util.List)) { + return o; + } else { + return o; + } + } + } + return null; + """); } @Test public void testAbstractIterator() throws Exception { - compilesTo( - "{\n" + - " var com.google.common.collect.AbstractIterator iter = [| return self.endOfData ]\n" + - " return iter\n" + - "}\n", - "\n" + - "final com.google.common.collect.AbstractIterator _function = new com.google.common.collect.AbstractIterator() {\n" + - " @Override\n" + - " protected String computeNext() {\n" + - " return this.endOfData();\n" + - " }\n" + - "};\n" + - "com.google.common.collect.AbstractIterator iter = _function;\n" + - "return iter;\n"); + compilesTo(""" + { + var com.google.common.collect.AbstractIterator iter = [| return self.endOfData ] + return iter + } + """, + """ + final com.google.common.collect.AbstractIterator _function = new com.google.common.collect.AbstractIterator() { + @Override + protected String computeNext() { + return this.endOfData(); + } + }; + com.google.common.collect.AbstractIterator iter = _function; + return iter; + """); } @Test public void testVariableDeclaration() throws Exception { - compilesTo( - "{\n" + - " val x = 42\n" + - "}\n", "\n" + - "final int x = 42;\n"); + compilesTo(""" + { + val x = 42 + } + """, "final int x = 42;"); } @Test public void testVariableDeclaration1() throws Exception { - compilesTo( - "{\n" + - " val a = 'foo'\n" + - " val x = a\n" + - "}\n", "\n" + - "final String a = \"foo\";\n" + - "final String x = a;\n"); + compilesTo(""" + { + val a = 'foo' + val x = a + } + """, """ + final String a = "foo"; + final String x = a; + """); } @Test public void testVariableDeclaration2() throws Exception { - compilesTo( - "{\n" + - " val x = 'foo'.toUpperCase\n" + - "}\n", "\n" + - "final String x = \"foo\".toUpperCase();\n"); + compilesTo(""" + { + val x = 'foo'.toUpperCase + } + """, """ + final String x = "foo".toUpperCase(); + """); } @Test public void testBug367144() throws Exception { - compilesTo( - "foo::bar::SubOfClassWithStatics::MY_STATIC_FIELD\n", "\n" + - "return foo.bar.SubOfClassWithStatics.MY_STATIC_FIELD;\n"); + compilesTo("foo::bar::SubOfClassWithStatics::MY_STATIC_FIELD", "return foo.bar.SubOfClassWithStatics.MY_STATIC_FIELD;"); } @Test public void testBug367144_1() throws Exception { - compilesTo( - "foo::bar::SubOfClassWithStatics::MY_STATIC_METHOD\n", - "\n" + - "String _MY_STATIC_METHOD = foo.bar.SubOfClassWithStatics.MY_STATIC_METHOD();\n" + - "return _MY_STATIC_METHOD;\n"); + compilesTo("foo::bar::SubOfClassWithStatics::MY_STATIC_METHOD", """ + String _MY_STATIC_METHOD = foo.bar.SubOfClassWithStatics.MY_STATIC_METHOD(); + return _MY_STATIC_METHOD; + """); } @Test public void testBug371321_1() throws Exception { - compilesTo( - "try {\n" + - " 'foo'.length\n" + - "} catch (RuntimeException assert) {\n" + - " assert.printStackTrace\n" + - "}\n", - "\n" + - "int _xtrycatchfinallyexpression = (int) 0;\n" + - "try {\n" + - " _xtrycatchfinallyexpression = \"foo\".length();\n" + - "} catch (final Throwable _t) {\n" + - " if (_t instanceof RuntimeException) {\n" + - " final RuntimeException assert_ = (RuntimeException)_t;\n" + - " assert_.printStackTrace();\n" + - " } else {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" + - " }\n" + - "}\n" + - "return Integer.valueOf(_xtrycatchfinallyexpression);\n"); + compilesTo(""" + try { + 'foo'.length + } catch (RuntimeException assert) { + assert.printStackTrace + } + """, """ + int _xtrycatchfinallyexpression = (int) 0; + try { + _xtrycatchfinallyexpression = "foo".length(); + } catch (final Throwable _t) { + if (_t instanceof RuntimeException) { + final RuntimeException assert_ = (RuntimeException)_t; + assert_.printStackTrace(); + } else { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t); + } + } + return Integer.valueOf(_xtrycatchfinallyexpression); + """); } @Test public void testBug420984_EmptyCatch() throws Exception { - compilesTo( - "try {\n" + - " println(\"\")\n" + - "} catch (RuntimeException e) {\n" + - "}\n", - "\n" + - "String _xtrycatchfinallyexpression = null;\n" + - "try {\n" + - " _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(\"\");\n" + - "} catch (final Throwable _t) {\n" + - " if (_t instanceof RuntimeException) {\n" + - " _xtrycatchfinallyexpression = null;\n" + - " } else {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" + - " }\n" + - "}\n" + - "return _xtrycatchfinallyexpression;\n"); + compilesTo(""" + try { + println("") + } catch (RuntimeException e) { + } + """, """ + String _xtrycatchfinallyexpression = null; + try { + _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(""); + } catch (final Throwable _t) { + if (_t instanceof RuntimeException) { + _xtrycatchfinallyexpression = null; + } else { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t); + } + } + return _xtrycatchfinallyexpression; + """); } @Test public void testBug420984_EmptyCatchWithoutReturnType() throws Exception { - compilesToStatement( - "try {\n" + - " println(\"\")\n" + - "} catch (RuntimeException e) {\n" + - "}\n", - "try {\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.println(\"\");\n" + - "} catch (final Throwable _t) {\n" + - " if (_t instanceof RuntimeException) {\n" + - " } else {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" + - " }\n" + - "}"); + compilesToStatement(""" + try { + println("") + } catch (RuntimeException e) { + } + """, """ + try { + org.eclipse.xtext.xbase.lib.InputOutput.println(""); + } catch (final Throwable _t) { + if (_t instanceof RuntimeException) { + } else { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t); + } + }"""); } @Test public void testBug420984_CatchWithoutReference() throws Exception { - compilesTo( - "try {\n" + - " println(\"\")\n" + - "} catch (RuntimeException e) {\n" + - " println(\"\")\n" + - "}\n", - "\n" + - "String _xtrycatchfinallyexpression = null;\n" + - "try {\n" + - " _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(\"\");\n" + - "} catch (final Throwable _t) {\n" + - " if (_t instanceof RuntimeException) {\n" + - " _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(\"\");\n" + - " } else {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" + - " }\n" + - "}\n" + - "return _xtrycatchfinallyexpression;\n"); + compilesTo(""" + try { + println("") + } catch (RuntimeException e) { + println("") + } + """, """ + String _xtrycatchfinallyexpression = null; + try { + _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(""); + } catch (final Throwable _t) { + if (_t instanceof RuntimeException) { + _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(""); + } else { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t); + } + } + return _xtrycatchfinallyexpression; + """); } @Test public void testBug420984_CatchWithReference() throws Exception { - compilesTo( - "try {\n" + - " println(\"\")\n" + - "} catch (RuntimeException e) {\n" + - " println(e)\n" + - "}\n", - "\n" + - "java.io.Serializable _xtrycatchfinallyexpression = null;\n" + - "try {\n" + - " _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(\"\");\n" + - "} catch (final Throwable _t) {\n" + - " if (_t instanceof RuntimeException) {\n" + - " final RuntimeException e = (RuntimeException)_t;\n" + - " _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(e);\n" + - " } else {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" + - " }\n" + - "}\n" + - "return _xtrycatchfinallyexpression;\n"); + compilesTo(""" + try { + println("") + } catch (RuntimeException e) { + println(e) + } + """, """ + java.io.Serializable _xtrycatchfinallyexpression = null; + try { + _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(""); + } catch (final Throwable _t) { + if (_t instanceof RuntimeException) { + final RuntimeException e = (RuntimeException)_t; + _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.println(e); + } else { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t); + } + } + return _xtrycatchfinallyexpression; + """); } @Test public void testBug371321_2() throws Exception { - compilesTo( - "for (assert : 'foo'.toCharArray) {\n" + - " println(assert)\n" + - "}\n", - "\n" + - "char[] _charArray = \"foo\".toCharArray();\n" + - "for (final char assert_ : _charArray) {\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.println(Character.valueOf(assert_));\n" + - "}\n"); + compilesTo(""" + for (assert : 'foo'.toCharArray) { + println(assert) + } + """, """ + char[] _charArray = "foo".toCharArray(); + for (final char assert_ : _charArray) { + org.eclipse.xtext.xbase.lib.InputOutput.println(Character.valueOf(assert_)); + } + """); } @Test public void testNewThread() throws Exception { - compilesTo( - "new Thread [| ]\n", - "\n" + - "final Runnable _function = new Runnable() {\n" + - " public void run() {\n" + - " }\n" + - "};\n" + - "Thread _thread = new Thread(_function);\n" + - "return _thread;\n"); + compilesTo("new Thread [| ]", """ + final Runnable _function = new Runnable() { + public void run() { + } + }; + Thread _thread = new Thread(_function); + return _thread; + """); } @Test public void testForLoop_01() throws Exception { - compilesTo( - "for(int i: 1..2) { i.toString }\n", - "\n" + - "org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" + - "for (final int i : _upTo) {\n" + - " Integer.valueOf(i).toString();\n" + - "}\n"); + compilesTo(""" + for(int i: 1..2) { i.toString } + """, """ + org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2); + for (final int i : _upTo) { + Integer.valueOf(i).toString(); + } + """); } @Test public void testForLoop_02() throws Exception { - compilesTo( - "for(Integer i: 1..2) { i.toString }\n", - "\n" + - "org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" + - "for (final Integer i : _upTo) {\n" + - " i.toString();\n" + - "}\n"); + compilesTo(""" + for(Integer i: 1..2) { i.toString } + """, """ + org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2); + for (final Integer i : _upTo) { + i.toString(); + } + """); } @Test public void testForLoop_03() throws Exception { - compilesTo( - "for(i: 1..2) { i.toString }\n", - "\n" + - "org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" + - "for (final Integer i : _upTo) {\n" + - " i.toString();\n" + - "}\n"); + compilesTo(""" + for(i: 1..2) { i.toString } + """, """ + org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2); + for (final Integer i : _upTo) { + i.toString(); + } + """); } @Test public void testForLoop_04() throws Exception { - compilesTo( - "for(i: null as int[]) { i.toString }\n", - "\n" + - "for (final int i : ((int[]) null)) {\n" + - " Integer.valueOf(i).toString();\n" + - "}\n"); + compilesTo(""" + for(i: null as int[]) { i.toString } + """, """ + for (final int i : ((int[]) null)) { + Integer.valueOf(i).toString(); + } + """); } @Test public void testForLoop_05() throws Exception { - compilesTo( - "for(i: null as Integer[]) { i.toString }\n", "\n" + - "for (final Integer i : ((Integer[]) null)) {\n" + - " i.toString();\n" + - "}\n"); + compilesTo(""" + for(i: null as Integer[]) { i.toString } + """, """ + for (final Integer i : ((Integer[]) null)) { + i.toString(); + } + """); } @Test public void testForLoop_06() throws Exception { - compilesTo( - "for(Integer i: null as int[]) { i.toString }\n", "\n" + - "for (final Integer i : ((int[]) null)) {\n" + - " i.toString();\n" + - "}\n"); + compilesTo(""" + for(Integer i: null as int[]) { i.toString } + """, """ + for (final Integer i : ((int[]) null)) { + i.toString(); + } + """); } @Test public void testForLoop_07() throws Exception { - compilesTo( - "for(int i: null as Integer[]) { i.toString }\n", - "\n" + - "for (final int i : ((Integer[]) null)) {\n" + - " Integer.valueOf(i).toString();\n" + - "}\n"); + compilesTo(""" + for(int i: null as Integer[]) { i.toString } + """, """ + for (final int i : ((Integer[]) null)) { + Integer.valueOf(i).toString(); + } + """); } @Test public void testForLoop_08() throws Exception { - compilesTo( - "for(Integer i: null as int[]) { i.toString }\n", "\n" + - "for (final Integer i : ((int[]) null)) {\n" + - " i.toString();\n" + - "}\n"); + compilesTo(""" + for(Integer i: null as int[]) { i.toString } + """, """ + for (final Integer i : ((int[]) null)) { + i.toString(); + } + """); } @Test public void testForLoop_09() throws Exception { - compilesTo( - "for(int i: null as Integer[]) { i.toString }\n", - "\n" + - "for (final int i : ((Integer[]) null)) {\n" + - " Integer.valueOf(i).toString();\n" + - "}\n"); + compilesTo(""" + for(int i: null as Integer[]) { i.toString } + """, """ + for (final int i : ((Integer[]) null)) { + Integer.valueOf(i).toString(); + } + """); } @Test public void testForLoop_10() throws Exception { - compilesTo( - "for(int i: newArrayList) { i.toString }\n", - "\n" + - "java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList();\n" + - "for (final int i : _newArrayList) {\n" + - " Integer.valueOf(i).toString();\n" + - "}\n"); + compilesTo(""" + for(int i: newArrayList) { i.toString } + """, """ + java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(); + for (final int i : _newArrayList) { + Integer.valueOf(i).toString(); + } + """); } @Test public void testForLoop_11() throws Exception { - compilesTo( - "for(Integer i: newArrayList) { i.toString }\n", - "\n" + - "java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList();\n" + - "for (final Integer i : _newArrayList) {\n" + - " i.toString();\n" + - "}\n"); + compilesTo(""" + for(Integer i: newArrayList) { i.toString } + """, """ + java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(); + for (final Integer i : _newArrayList) { + i.toString(); + } + """); } @Test public void testForLoop_12() throws Exception { - compilesTo( - "for(double d: newArrayList) { d.toString }\n", - "\n" + - "java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList();\n" + - "for (final double d : _newArrayList) {\n" + - " Double.valueOf(d).toString();\n" + - "}\n"); + compilesTo(""" + for(double d: newArrayList) { d.toString } + """, """ + java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(); + for (final double d : _newArrayList) { + Double.valueOf(d).toString(); + } + """); } @Test public void testForLoop_13() throws Exception { - compilesTo( - "for(double d: null as int[]) { d.toString }\n", - "\n" + - "for (final double d : ((int[]) null)) {\n" + - " Double.valueOf(d).toString();\n" + - "}\n"); + compilesTo(""" + for(double d: null as int[]) { d.toString } + """, """ + for (final double d : ((int[]) null)) { + Double.valueOf(d).toString(); + } + """); } @Test public void testForLoop_14() throws Exception { - compilesTo( - "for(double d: null as Integer[]) { d.toString }\n", - "\n" + - "for (final double d : ((Integer[]) null)) {\n" + - " Double.valueOf(d).toString();\n" + - "}\n"); + compilesTo(""" + for(double d: null as Integer[]) { d.toString } + """, """ + for (final double d : ((Integer[]) null)) { + Double.valueOf(d).toString(); + } + """); } @Test public void testNullSafe_01() throws Exception { - compilesTo( - "('foo'.toUpperCase as CharSequence)?.subSequence(0,2)\n", - "\n" + - "String _upperCase = \"foo\".toUpperCase();\n" + - "CharSequence _subSequence = null;\n" + - "if (((CharSequence) _upperCase)!=null) {\n" + - " _subSequence=((CharSequence) _upperCase).subSequence(0, 2);\n" + - "}\n" + - "return _subSequence;\n"); + compilesTo(""" + ('foo'.toUpperCase as CharSequence)?.subSequence(0,2) + """, """ + String _upperCase = "foo".toUpperCase(); + CharSequence _subSequence = null; + if (((CharSequence) _upperCase)!=null) { + _subSequence=((CharSequence) _upperCase).subSequence(0, 2); + } + return _subSequence; + """); } @Test public void testSwitch() throws Exception { - compilesTo( - "{\n" + - " val it = String\n" + - " switch name {\n" + - " CharSequence : name\n" + - " default : 'noname'\n" + - " }\n" + - "}\n", - "\n" + - "String _xblockexpression = null;\n" + - "{\n" + - " final Class it = String.class;\n" + - " String _switchResult = null;\n" + - " String _name = it.getName();\n" + - " boolean _matched = false;\n" + - " if (_name instanceof CharSequence) {\n" + - " _matched=true;\n" + - " _switchResult = it.getName();\n" + - " }\n" + - " if (!_matched) {\n" + - " _switchResult = \"noname\";\n" + - " }\n" + - " _xblockexpression = _switchResult;\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + val it = String + switch name { + CharSequence : name + default : 'noname' + } + } + """, """ + String _xblockexpression = null; + { + final Class it = String.class; + String _switchResult = null; + String _name = it.getName(); + boolean _matched = false; + if (_name instanceof CharSequence) { + _matched=true; + _switchResult = it.getName(); + } + if (!_matched) { + _switchResult = "noname"; + } + _xblockexpression = _switchResult; + } + return _xblockexpression; + """); } @Test public void testSwitchScopes_Bug470586_01() throws Exception { - compilesTo( - "\n" + - "{\n" + - " val list = #[\"1\", \"2\"]\n" + - " list.forEach[\n" + - " val res = switch it {\n" + - " CharSequence : it\n" + - " }\n" + - " ]\n" + - " list.forEach[\n" + - " println(it)\n" + - " ]\n" + - "}\n", - "\n" + - "final java.util.List list = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(\"1\", \"2\"));\n" + - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final String it) {\n" + - " String _switchResult = null;\n" + - " boolean _matched = false;\n" + - " if (it instanceof CharSequence) {\n" + - " _matched=true;\n" + - " _switchResult = it;\n" + - " }\n" + - " final String res = _switchResult;\n" + - " }\n" + - "};\n" + - "list.forEach(_function);\n" + - "final java.util.function.Consumer _function_1 = new java.util.function.Consumer() {\n" + - " public void accept(final String it) {\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.println(it);\n" + - " }\n" + - "};\n" + - "list.forEach(_function_1);\n"); + compilesTo(""" + { + val list = #["1", "2"] + list.forEach[ + val res = switch it { + CharSequence : it + } + ] + list.forEach[ + println(it) + ] + } + """, + """ + final java.util.List list = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList("1", "2")); + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final String it) { + String _switchResult = null; + boolean _matched = false; + if (it instanceof CharSequence) { + _matched=true; + _switchResult = it; + } + final String res = _switchResult; + } + }; + list.forEach(_function); + final java.util.function.Consumer _function_1 = new java.util.function.Consumer() { + public void accept(final String it) { + org.eclipse.xtext.xbase.lib.InputOutput.println(it); + } + }; + list.forEach(_function_1); + """); } @Test public void testSwitchScopes_Bug470586_02() throws Exception { - compilesTo( - "{\n" + - " val list = #[\"1\", \"2\"]\n" + - " list.forEach[\n" + - " val res = switch it {\n" + - " CharSequence : it\n" + - " default : ''\n" + - " }\n" + - " ]\n" + - " list.forEach[\n" + - " println(it)\n" + - " ]\n" + - "}\n", - "\n" + - "final java.util.List list = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(\"1\", \"2\"));\n" + - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final String it) {\n" + - " String _switchResult = null;\n" + - " boolean _matched = false;\n" + - " if (it instanceof CharSequence) {\n" + - " _matched=true;\n" + - " _switchResult = it;\n" + - " }\n" + - " if (!_matched) {\n" + - " _switchResult = \"\";\n" + - " }\n" + - " final String res = _switchResult;\n" + - " }\n" + - "};\n" + - "list.forEach(_function);\n" + - "final java.util.function.Consumer _function_1 = new java.util.function.Consumer() {\n" + - " public void accept(final String it) {\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.println(it);\n" + - " }\n" + - "};\n" + - "list.forEach(_function_1);\n"); + compilesTo(""" + { + val list = #["1", "2"] + list.forEach[ + val res = switch it { + CharSequence : it + default : '' + } + ] + list.forEach[ + println(it) + ] + } + """, + """ + final java.util.List list = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList("1", "2")); + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final String it) { + String _switchResult = null; + boolean _matched = false; + if (it instanceof CharSequence) { + _matched=true; + _switchResult = it; + } + if (!_matched) { + _switchResult = ""; + } + final String res = _switchResult; + } + }; + list.forEach(_function); + final java.util.function.Consumer _function_1 = new java.util.function.Consumer() { + public void accept(final String it) { + org.eclipse.xtext.xbase.lib.InputOutput.println(it); + } + }; + list.forEach(_function_1); + """); } @Test public void testSwitchScopes_Bug470586_03() throws Exception { - compilesTo( - "{\n" + - " val list = #[\"1\", \"2\"]\n" + - " list.forEach[\n" + - " val res = switch it {\n" + - " String, CharSequence : it\n" + - " default : ''\n" + - " }\n" + - " ]\n" + - " list.forEach[\n" + - " println(it)\n" + - " ]\n" + - "}\n", - "\n" + - "final java.util.List list = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(\"1\", \"2\"));\n" + - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final String it) {\n" + - " String _switchResult = null;\n" + - " boolean _matched = false;\n" + - " if (it instanceof String) {\n" + - " _matched=true;\n" + - " }\n" + - " if (!_matched) {\n" + - " if (it instanceof CharSequence) {\n" + - " _matched=true;\n" + - " }\n" + - " }\n" + - " if (_matched) {\n" + - " _switchResult = it;\n" + - " }\n" + - " if (!_matched) {\n" + - " _switchResult = \"\";\n" + - " }\n" + - " final String res = _switchResult;\n" + - " }\n" + - "};\n" + - "list.forEach(_function);\n" + - "final java.util.function.Consumer _function_1 = new java.util.function.Consumer() {\n" + - " public void accept(final String it) {\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.println(it);\n" + - " }\n" + - "};\n" + - "list.forEach(_function_1);\n"); + compilesTo(""" + { + val list = #["1", "2"] + list.forEach[ + val res = switch it { + String, CharSequence : it + default : '' + } + ] + list.forEach[ + println(it) + ] + } + """, + """ + final java.util.List list = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList("1", "2")); + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final String it) { + String _switchResult = null; + boolean _matched = false; + if (it instanceof String) { + _matched=true; + } + if (!_matched) { + if (it instanceof CharSequence) { + _matched=true; + } + } + if (_matched) { + _switchResult = it; + } + if (!_matched) { + _switchResult = ""; + } + final String res = _switchResult; + } + }; + list.forEach(_function); + final java.util.function.Consumer _function_1 = new java.util.function.Consumer() { + public void accept(final String it) { + org.eclipse.xtext.xbase.lib.InputOutput.println(it); + } + }; + list.forEach(_function_1); + """); } @Test public void testFallThroughSwitch() throws Exception { - compilesTo( - "{\n" + - " switch x : 1 {\n" + - " case 1,\n" + - " case 2:\n" + - " 'lalala'\n" + - " }\n" + - "}\n", - "\n" + - "String _switchResult = null;\n" + - "final int x = 1;\n" + - "switch (x) {\n" + - " case 1:\n" + - " case 2:\n" + - " _switchResult = \"lalala\";\n" + - " break;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch x : 1 { + case 1, + case 2: + 'lalala' + } + } + """, """ + String _switchResult = null; + final int x = 1; + switch (x) { + case 1: + case 2: + _switchResult = "lalala"; + break; + } + return _switchResult; + """); } @Test public void testFallThroughSwitch_2() throws Exception { - compilesTo( - "{\n" + - " switch x : 'lalala' {\n" + - " case 'a',\n" + - " case 'b',\n" + - " case 'c':\n" + - " 'lalala'\n" + - " }\n" + - "}\n", - "\n" + - "String _switchResult = null;\n" + - "final String x = \"lalala\";\n" + - "boolean _matched = false;\n" + - "if (com.google.common.base.Objects.equal(x, \"a\")) {\n" + - " _matched=true;\n" + - "}\n" + - "if (!_matched) {\n" + - " if (com.google.common.base.Objects.equal(x, \"b\")) {\n" + - " _matched=true;\n" + - " }\n" + - "}\n" + - "if (!_matched) {\n" + - " if (com.google.common.base.Objects.equal(x, \"c\")) {\n" + - " _matched=true;\n" + - " }\n" + - "}\n" + - "if (_matched) {\n" + - " _switchResult = \"lalala\";\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch x : 'lalala' { + case 'a', + case 'b', + case 'c': + 'lalala' + } + } + """, """ + String _switchResult = null; + final String x = "lalala"; + boolean _matched = false; + if (com.google.common.base.Objects.equal(x, "a")) { + _matched=true; + } + if (!_matched) { + if (com.google.common.base.Objects.equal(x, "b")) { + _matched=true; + } + } + if (!_matched) { + if (com.google.common.base.Objects.equal(x, "c")) { + _matched=true; + } + } + if (_matched) { + _switchResult = "lalala"; + } + return _switchResult; + """); } @Test public void testFallThroughSwitch_3() throws Exception { - compilesTo( - "{\n" + - " switch x : 1 {\n" + - " case 1,\n" + - " case 2,\n" + - " default:\n" + - " 'lalala'\n" + - " }\n" + - "}\n", - "\n" + - "String _switchResult = null;\n" + - "final int x = 1;\n" + - "switch (x) {\n" + - " case 1:\n" + - " case 2:\n" + - " default:\n" + - " _switchResult = \"lalala\";\n" + - " break;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch x : 1 { + case 1, + case 2, + default: + 'lalala' + } + } + """, """ + String _switchResult = null; + final int x = 1; + switch (x) { + case 1: + case 2: + default: + _switchResult = "lalala"; + break; + } + return _switchResult; + """); } @Test public void testFallThroughSwitch_4() throws Exception { - compilesTo( - "{\n" + - " switch x : 'lalala' {\n" + - " case 'a',\n" + - " case 'b',\n" + - " default:\n" + - " 'lalala'\n" + - " }\n" + - "}\n", - "\n" + - "String _switchResult = null;\n" + - "final String x = \"lalala\";\n" + - "boolean _matched = false;\n" + - "if (com.google.common.base.Objects.equal(x, \"a\")) {\n" + - " _matched=true;\n" + - "}\n" + - "if (!_matched) {\n" + - " if (com.google.common.base.Objects.equal(x, \"b\")) {\n" + - " _matched=true;\n" + - " }\n" + - "}\n" + - "if (_matched) {\n" + - " _switchResult = \"lalala\";\n" + - "}\n" + - "if (!_matched) {\n" + - " _switchResult = \"lalala\";\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch x : 'lalala' { + case 'a', + case 'b', + default: + 'lalala' + } + } + """, """ + String _switchResult = null; + final String x = "lalala"; + boolean _matched = false; + if (com.google.common.base.Objects.equal(x, "a")) { + _matched=true; + } + if (!_matched) { + if (com.google.common.base.Objects.equal(x, "b")) { + _matched=true; + } + } + if (_matched) { + _switchResult = "lalala"; + } + if (!_matched) { + _switchResult = "lalala"; + } + return _switchResult; + """); } @Test public void testFallThroughSwitch_5() throws Exception { - compilesTo( - "{\n" + - " switch x : 1 {\n" + - " case 1:\n" + - " 'blabla'\n" + - " case 2,\n" + - " default:\n" + - " 'lalala'\n" + - " }\n" + - "}\n", - "\n" + - "String _switchResult = null;\n" + - "final int x = 1;\n" + - "switch (x) {\n" + - " case 1:\n" + - " _switchResult = \"blabla\";\n" + - " break;\n" + - " case 2:\n" + - " default:\n" + - " _switchResult = \"lalala\";\n" + - " break;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch x : 1 { + case 1: + 'blabla' + case 2, + default: + 'lalala' + } + } + """, """ + String _switchResult = null; + final int x = 1; + switch (x) { + case 1: + _switchResult = "blabla"; + break; + case 2: + default: + _switchResult = "lalala"; + break; + } + return _switchResult; + """); } @Test public void testFallThroughSwitch_6() throws Exception { - compilesTo( - "{\n" + - " switch x : new Object {\n" + - " String case 'a':\n" + - " 'blabla'\n" + - " Integer case 1,\n" + - " case 2,\n" + - " default:\n" + - " 'lalala'\n" + - " }\n" + - "}\n", - "\n" + - "String _switchResult = null;\n" + - "Object _object = new Object();\n" + - "final Object x = _object;\n" + - "boolean _matched = false;\n" + - "if (x instanceof String) {\n" + - " if (com.google.common.base.Objects.equal(x, \"a\")) {\n" + - " _matched=true;\n" + - " _switchResult = \"blabla\";\n" + - " }\n" + - "}\n" + - "if (!_matched) {\n" + - " if (x instanceof Integer) {\n" + - " if (com.google.common.base.Objects.equal(x, 1)) {\n" + - " _matched=true;\n" + - " }\n" + - " }\n" + - " if (!_matched) {\n" + - " if (com.google.common.base.Objects.equal(x, 2)) {\n" + - " _matched=true;\n" + - " }\n" + - " }\n" + - " if (_matched) {\n" + - " _switchResult = \"lalala\";\n" + - " }\n" + - "}\n" + - "if (!_matched) {\n" + - " _switchResult = \"lalala\";\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch x : new Object { + String case 'a': + 'blabla' + Integer case 1, + case 2, + default: + 'lalala' + } + } + """, """ + String _switchResult = null; + Object _object = new Object(); + final Object x = _object; + boolean _matched = false; + if (x instanceof String) { + if (com.google.common.base.Objects.equal(x, "a")) { + _matched=true; + _switchResult = "blabla"; + } + } + if (!_matched) { + if (x instanceof Integer) { + if (com.google.common.base.Objects.equal(x, 1)) { + _matched=true; + } + } + if (!_matched) { + if (com.google.common.base.Objects.equal(x, 2)) { + _matched=true; + } + } + if (_matched) { + _switchResult = "lalala"; + } + } + if (!_matched) { + _switchResult = "lalala"; + } + return _switchResult; + """); } @Test public void testFallThroughSwitch_7() throws Exception { - compilesTo( - "{\n" + - " switch x : 'lalala' as Object { \n" + - " String, Integer case 1: 0\n" + - " Integer, default: 1\n" + - " }\n" + - "}\n", - "\n" + - "int _switchResult = (int) 0;\n" + - "final Object x = ((Object) \"lalala\");\n" + - "boolean _matched = false;\n" + - "if (x instanceof String) {\n" + - " _matched=true;\n" + - "}\n" + - "if (!_matched) {\n" + - " if (x instanceof Integer) {\n" + - " if (com.google.common.base.Objects.equal(x, 1)) {\n" + - " _matched=true;\n" + - " }\n" + - " }\n" + - "}\n" + - "if (_matched) {\n" + - " _switchResult = 0;\n" + - "}\n" + - "if (!_matched) {\n" + - " if (x instanceof Integer) {\n" + - " _matched=true;\n" + - " _switchResult = 1;\n" + - " }\n" + - "}\n" + - "if (!_matched) {\n" + - " _switchResult = 1;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch x : 'lalala' as Object { + String, Integer case 1: 0 + Integer, default: 1 + } + } + """, """ + int _switchResult = (int) 0; + final Object x = ((Object) "lalala"); + boolean _matched = false; + if (x instanceof String) { + _matched=true; + } + if (!_matched) { + if (x instanceof Integer) { + if (com.google.common.base.Objects.equal(x, 1)) { + _matched=true; + } + } + } + if (_matched) { + _switchResult = 0; + } + if (!_matched) { + if (x instanceof Integer) { + _matched=true; + _switchResult = 1; + } + } + if (!_matched) { + _switchResult = 1; + } + return _switchResult; + """); } @Test public void testFallThroughSwitch_8() throws Exception { - compilesTo( - "{\n" + - " switch x : 1 as Object {\n" + - " Integer,\n" + - " Double: x\n" + - " }\n" + - "}\n", - "\n" + - "Number _switchResult = null;\n" + - "final Object x = ((Object) Integer.valueOf(1));\n" + - "boolean _matched = false;\n" + - "if (x instanceof Integer) {\n" + - " _matched=true;\n" + - "}\n" + - "if (!_matched) {\n" + - " if (x instanceof Double) {\n" + - " _matched=true;\n" + - " }\n" + - "}\n" + - "if (_matched) {\n" + - " _switchResult = ((Number)x);\n" + - "}\n" + - "return ((Number)_switchResult);\n"); + compilesTo(""" + { + switch x : 1 as Object { + Integer, + Double: x + } + } + """, """ + Number _switchResult = null; + final Object x = ((Object) Integer.valueOf(1)); + boolean _matched = false; + if (x instanceof Integer) { + _matched=true; + } + if (!_matched) { + if (x instanceof Double) { + _matched=true; + } + } + if (_matched) { + _switchResult = ((Number)x); + } + return ((Number)_switchResult); + """); } @Test public void testEmptySwitch() throws Exception { - compilesTo( - "{\n" + - " switch 1 {\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final int _switchValue = 1;\n" + - "switch (_switchValue) {\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch 1 { + } + } + """, """ + Object _switchResult = null; + final int _switchValue = 1; + switch (_switchValue) { + } + return _switchResult; + """); } @Test public void testEmptySwitch_2() throws Exception { - compilesTo( - "{\n" + - " switch 1 {\n" + - " default: {\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final int _switchValue = 1;\n" + - "switch (_switchValue) {\n" + - " default:\n" + - " _switchResult = null;\n" + - " break;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch 1 { + default: { + } + } + } + """, """ + Object _switchResult = null; + final int _switchValue = 1; + switch (_switchValue) { + default: + _switchResult = null; + break; + } + return _switchResult; + """); } @Test public void testEmptySwitch_3() throws Exception { - compilesTo( - "{\n" + - " switch Object {\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final Class Object = Object.class;\n" + - "boolean _matched = false;\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch Object { + } + } + """, """ + Object _switchResult = null; + final Class Object = Object.class; + boolean _matched = false; + return _switchResult; + """); } @Test public void testEmptySwitch_4() throws Exception { - compilesTo( - "{\n" + - " switch Object {\n" + - " default: {\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final Class Object = Object.class;\n" + - "boolean _matched = false;\n" + - "if (!_matched) {\n" + - " _switchResult = null;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch Object { + default: { + } + } + } + """, """ + Object _switchResult = null; + final Class Object = Object.class; + boolean _matched = false; + if (!_matched) { + _switchResult = null; + } + return _switchResult; + """); } @Test public void testEmptySwitch_5() throws Exception { - compilesTo( - "{\n" + - " switch Thread.State.NEW {\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final Thread.State _switchValue = Thread.State.NEW;\n" + - "if (_switchValue != null) {\n" + - " switch (_switchValue) {\n" + - " default:\n" + - " break;\n" + - " }\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch Thread.State.NEW { + } + } + """, """ + Object _switchResult = null; + final Thread.State _switchValue = Thread.State.NEW; + if (_switchValue != null) { + switch (_switchValue) { + default: + break; + } + } + return _switchResult; + """); } @Test public void testEmptySwitch_6() throws Exception { - compilesTo( - "{\n" + - " switch Thread.State.NEW {\n" + - " default: {\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final Thread.State _switchValue = Thread.State.NEW;\n" + - "if (_switchValue != null) {\n" + - " switch (_switchValue) {\n" + - " default:\n" + - " _switchResult = null;\n" + - " break;\n" + - " }\n" + - "} else {\n" + - " _switchResult = null;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch Thread.State.NEW { + default: { + } + } + } + """, """ + Object _switchResult = null; + final Thread.State _switchValue = Thread.State.NEW; + if (_switchValue != null) { + switch (_switchValue) { + default: + _switchResult = null; + break; + } + } else { + _switchResult = null; + } + return _switchResult; + """); } @Test public void testEmptySwitch_7() throws Exception { - compilesTo( - "{\n" + - " switch Thread.State x : Thread.State.NEW {\n" + - " default: {\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final Thread.State x = Thread.State.NEW;\n" + - "if (x != null) {\n" + - " switch (x) {\n" + - " default:\n" + - " _switchResult = null;\n" + - " break;\n" + - " }\n" + - "} else {\n" + - " _switchResult = null;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch Thread.State x : Thread.State.NEW { + default: { + } + } + } + """, """ + Object _switchResult = null; + final Thread.State x = Thread.State.NEW; + if (x != null) { + switch (x) { + default: + _switchResult = null; + break; + } + } else { + _switchResult = null; + } + return _switchResult; + """); } @Test public void testEmptySwitch_8() throws Exception { - compilesTo( - "{\n" + - " switch (Thread.State x : Thread.State.NEW) {\n" + - " default: {\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final Thread.State x = Thread.State.NEW;\n" + - "if (x != null) {\n" + - " switch (x) {\n" + - " default:\n" + - " _switchResult = null;\n" + - " break;\n" + - " }\n" + - "} else {\n" + - " _switchResult = null;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch (Thread.State x : Thread.State.NEW) { + default: { + } + } + } + """, """ + Object _switchResult = null; + final Thread.State x = Thread.State.NEW; + if (x != null) { + switch (x) { + default: + _switchResult = null; + break; + } + } else { + _switchResult = null; + } + return _switchResult; + """); } @Test public void testEmptySwitch_9() throws Exception { - compilesTo( - "{\n" + - " switch Object x : Thread.State.NEW {\n" + - " default: {\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final Object x = Thread.State.NEW;\n" + - "boolean _matched = false;\n" + - "if (!_matched) {\n" + - " _switchResult = null;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch Object x : Thread.State.NEW { + default: { + } + } + } + """, """ + Object _switchResult = null; + final Object x = Thread.State.NEW; + boolean _matched = false; + if (!_matched) { + _switchResult = null; + } + return _switchResult; + """); } @Test public void testEmptySwitch_10() throws Exception { - compilesTo( - "{\n" + - " switch (Object x : Thread.State.NEW) {\n" + - " default: {\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "Object _switchResult = null;\n" + - "final Object x = Thread.State.NEW;\n" + - "boolean _matched = false;\n" + - "if (!_matched) {\n" + - " _switchResult = null;\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch (Object x : Thread.State.NEW) { + default: { + } + } + } + """, """ + Object _switchResult = null; + final Object x = Thread.State.NEW; + boolean _matched = false; + if (!_matched) { + _switchResult = null; + } + return _switchResult; + """); } @Test @@ -1012,1182 +1064,1254 @@ public void testEmptySwitch_11() throws Exception { it.setJavaSourceVersion(JavaVersion.JAVA8); }; GeneratorConfig _doubleArrow = ObjectExtensions. operator_doubleArrow(_generatorConfig, _function); - compilesTo( - "{\n" + - " val String x = null\n" + - " switch (x) {\n" + - " }\n" + - "}\n", - "\n" + - "Object _xblockexpression = null;\n" + - "{\n" + - " final String x = null;\n" + - " Object _switchResult = null;\n" + - " if (x != null) {\n" + - " switch (x) {\n" + - " }\n" + - " }\n" + - " _xblockexpression = _switchResult;\n" + - "}\n" + - "return _xblockexpression;\n", - _doubleArrow); + compilesTo(""" + { + val String x = null + switch (x) { + } + } + """, """ + Object _xblockexpression = null; + { + final String x = null; + Object _switchResult = null; + if (x != null) { + switch (x) { + } + } + _xblockexpression = _switchResult; + } + return _xblockexpression; + """, _doubleArrow); } @Test public void testBasicForLoop_0() throws Exception { - compilesTo( - "{\n" + - " for (var i = 0; i < 10; i = i + 1) {\n" + - " }\n" + - "}\n", "\n" + - "for (int i = 0; (i < 10); i = (i + 1)) {\n" + - "}\n"); + compilesTo(""" + { + for (var i = 0; i < 10; i = i + 1) { + } + } + """, """ + for (int i = 0; (i < 10); i = (i + 1)) { + } + """); } @Test public void testBasicForLoop_toJavaWhile_0() throws Exception { - compilesTo( - "{\n" + - " for (var i = if (true) { 1 }; i < 10; i = i + 1) {\n" + - " }\n" + - "}\n", - "\n" + - "int _xifexpression = (int) 0;\n" + - "if (true) {\n" + - " _xifexpression = 1;\n" + - "}\n" + - "int i = _xifexpression;\n" + - "boolean _while = (i < 10);\n" + - "while (_while) {\n" + - " i = (i + 1);\n" + - " _while = (i < 10);\n" + - "}\n"); + compilesTo(""" + { + for (var i = if (true) { 1 }; i < 10; i = i + 1) { + } + } + """, """ + int _xifexpression = (int) 0; + if (true) { + _xifexpression = 1; + } + int i = _xifexpression; + boolean _while = (i < 10); + while (_while) { + i = (i + 1); + _while = (i < 10); + } + """); } @Test public void testBasicForLoop_1() throws Exception { - compilesTo( - "{\n" + - " for (;;) {\n" + - " }\n" + - "}\n", "\n" + - "for (;;) {\n" + - "}\n"); + compilesTo(""" + { + for (;;) { + } + } + """, """ + for (;;) { + } + """); } @Test public void testBasicForLoop_toJavaWhile_1() throws Exception { - compilesTo( - "{\n" + - " for (for (;;) {};;) {\n" + - " }\n" + - "}\n", - "\n" + - "for (;;) {\n" + - "}\n" + - "boolean _while = true;\n" + - "while (_while) {\n" + - " _while = true;\n" + - "}\n"); + compilesTo(""" + { + for (for (;;) {};;) { + } + } + """, """ + for (;;) { + } + boolean _while = true; + while (_while) { + _while = true; + } + """); } @Test public void testBasicForLoop_2() throws Exception { - compilesTo( - "{\n" + - " for (val i = 0; i < 10;) {\n" + - " if (i == 2) {\n" + - " return true;\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "for (final int i = 0; (i < 10);) {\n" + - " if ((i == 2)) {\n" + - " return Boolean.valueOf(true);\n" + - " }\n" + - "}\n"); + compilesTo(""" + { + for (val i = 0; i < 10;) { + if (i == 2) { + return true; + } + } + } + """, """ + for (final int i = 0; (i < 10);) { + if ((i == 2)) { + return Boolean.valueOf(true); + } + } + """); } @Test public void testBasicForLoop_toJavaWhile_2() throws Exception { - compilesTo( - "{\n" + - " for (val i = #[if(true) {1} else {2}]; i.head < 10;) {\n" + - " if (i.head == 2) {\n" + - " return true;\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "int _xifexpression = (int) 0;\n" + - "if (true) {\n" + - " _xifexpression = 1;\n" + - "} else {\n" + - " _xifexpression = 2;\n" + - "}\n" + - "final java.util.List i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(Integer.valueOf(_xifexpression)));\n" + - "Integer _head = org.eclipse.xtext.xbase.lib.IterableExtensions.head(i);\n" + - "boolean _lessThan = ((_head).intValue() < 10);\n" + - "boolean _while = _lessThan;\n" + - "while (_while) {\n" + - " Integer _head_1 = org.eclipse.xtext.xbase.lib.IterableExtensions.head(i);\n" + - " boolean _equals = ((_head_1).intValue() == 2);\n" + - " if (_equals) {\n" + - " return Boolean.valueOf(true);\n" + - " }\n" + - " Integer _head_2 = org.eclipse.xtext.xbase.lib.IterableExtensions.head(i);\n" + - " boolean _lessThan_1 = ((_head_2).intValue() < 10);\n" + - " _while = _lessThan_1;\n" + - "}\n" + - "return null;\n"); + compilesTo(""" + { + for (val i = #[if(true) {1} else {2}]; i.head < 10;) { + if (i.head == 2) { + return true; + } + } + } + """, + """ + int _xifexpression = (int) 0; + if (true) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + final java.util.List i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(Integer.valueOf(_xifexpression))); + Integer _head = org.eclipse.xtext.xbase.lib.IterableExtensions.head(i); + boolean _lessThan = ((_head).intValue() < 10); + boolean _while = _lessThan; + while (_while) { + Integer _head_1 = org.eclipse.xtext.xbase.lib.IterableExtensions.head(i); + boolean _equals = ((_head_1).intValue() == 2); + if (_equals) { + return Boolean.valueOf(true); + } + Integer _head_2 = org.eclipse.xtext.xbase.lib.IterableExtensions.head(i); + boolean _lessThan_1 = ((_head_2).intValue() < 10); + _while = _lessThan_1; + } + return null; + """); } @Test public void testBasicForLoop_3() throws Exception { - compilesTo( - "{\n" + - " for (val i = 0; i < 10;) {\n" + - " return true\n" + - " }\n" + - "}\n", - "\n" + - "for (final int i = 0; (i < 10);) {\n" + - " return Boolean.valueOf(true);\n" + - "}\n"); + compilesTo(""" + { + for (val i = 0; i < 10;) { + return true + } + } + """, """ + for (final int i = 0; (i < 10);) { + return Boolean.valueOf(true); + } + """); } @Test public void testBasicForLoop_4() throws Exception { - compilesTo( - "{\n" + - " for (var i = 1; i < 10;) {}\n" + - " for (val i = 1;;) {}\n" + - "}\n", - "\n" + - "for (int i = 1; (i < 10);) {\n" + - "}\n" + - "for (final int i = 1;;) {\n" + - "}\n"); + compilesTo(""" + { + for (var i = 1; i < 10;) {} + for (val i = 1;;) {} + } + """, """ + for (int i = 1; (i < 10);) { + } + for (final int i = 1;;) { + } + """); } @Test public void testBasicForLoop_5() throws Exception { - compilesTo( - "{\n" + - " {\n" + - " for (var i = 1;i < 10;) {}\n" + - " }\n" + - " {\n" + - " for (val i = 1;;) {}\n" + - " }\n" + - "}\n", - "\n" + - "for (int i = 1; (i < 10);) {\n" + - "}\n" + - "for (final int i = 1;;) {\n" + - "}\n"); + compilesTo(""" + { + { + for (var i = 1;i < 10;) {} + } + { + for (val i = 1;;) {} + } + } + """, """ + for (int i = 1; (i < 10);) { + } + for (final int i = 1;;) { + } + """); } @Test public void testBasicForLoop_6() throws Exception { - compilesTo( - "{\n" + - " if (true) {\n" + - " for (;;) {}\n" + - " }\n" + - "}\n", "\n" + - "if (true) {\n" + - " for (;;) {\n" + - " }\n" + - "}\n"); + compilesTo(""" + { + if (true) { + for (;;) {} + } + } + """, """ + if (true) { + for (;;) { + } + } + """); } @Test public void testBasicForLoop_7() throws Exception { - compilesTo( - "{\n" + - " try {\n" + - " for (;;) {}\n" + - " } finally {\n" + - " }\n" + - "}\n", "\n" + - "try {\n" + - " for (;;) {\n" + - " }\n" + - "} finally {\n" + - "}\n"); + compilesTo(""" + { + try { + for (;;) {} + } finally { + } + } + """, """ + try { + for (;;) { + } + } finally { + } + """); } @Test public void testBasicForLoop_8() throws Exception { - compilesTo( - "{\n" + - " [| for (;;) {} ]\n" + - "}\n", - "\n" + - "final org.eclipse.xtext.xbase.lib.Procedures.Procedure0 _function = new org.eclipse.xtext.xbase.lib.Procedures.Procedure0() {\n" + - " public void apply() {\n" + - " for (;;) {\n" + - " }\n" + - " }\n" + - "};\n" + - "return _function;\n"); + compilesTo(""" + { + [| for (;;) {} ] + } + """, + """ + final org.eclipse.xtext.xbase.lib.Procedures.Procedure0 _function = new org.eclipse.xtext.xbase.lib.Procedures.Procedure0() { + public void apply() { + for (;;) { + } + } + }; + return _function; + """); } @Test public void testBasicForLoop_9() throws Exception { - compilesTo( - "{\n" + - " {\n" + - " {\n" + - " {\n" + - " for (;;) {}\n" + - " }\n" + - " }\n" + - " }\n" + - "}\n", "\n" + - "for (;;) {\n" + - "}\n"); + compilesTo(""" + { + { + { + { + for (;;) {} + } + } + } + } + """, """ + for (;;) { + } + """); } @Test public void testBasicForLoop_10() throws Exception { - compilesTo( - "{\n" + - " val i = 0\n" + - " for ([| i ].apply, [| i ].apply; i < 10;) {\n" + - " \n" + - " }\n" + - "}\n", - "\n" + - "final int i = 0;\n" + - "for (new org.eclipse.xtext.xbase.lib.Functions.Function0() {\n" + - " public Integer apply() {\n" + - " return Integer.valueOf(i);\n" + - " }\n" + - "}.apply(), new org.eclipse.xtext.xbase.lib.Functions.Function0() {\n" + - " public Integer apply() {\n" + - " return Integer.valueOf(i);\n" + - " }\n" + - "}.apply(); (i < 10);) {\n" + - "}\n"); + compilesTo(""" + { + val i = 0 + for ([| i ].apply, [| i ].apply; i < 10;) { + } + } + """, """ + final int i = 0; + for (new org.eclipse.xtext.xbase.lib.Functions.Function0() { + public Integer apply() { + return Integer.valueOf(i); + } + }.apply(), new org.eclipse.xtext.xbase.lib.Functions.Function0() { + public Integer apply() { + return Integer.valueOf(i); + } + }.apply(); (i < 10);) { + } + """); } @Test public void testBasicForLoop_11() throws Exception { - compilesTo( - "{\n" + - " for (val i = 0, val j = 0; i < 10;) {\n" + - " \n" + - " }\n" + - "}\n", - "\n" + - "final int i = 0;\n" + - "final int j = 0;\n" + - "boolean _while = (i < 10);\n" + - "while (_while) {\n" + - " _while = (i < 10);\n" + - "}\n"); + compilesTo(""" + { + for (val i = 0, val j = 0; i < 10;) { + } + } + """, """ + final int i = 0; + final int j = 0; + boolean _while = (i < 10); + while (_while) { + _while = (i < 10); + } + """); } @Test public void testBasicForLoop_12() throws Exception { - compilesTo( - "{\n" + - " for (var i = 0; i != 10; i += 1) {\n" + - " \n" + - " }\n" + - "}\n", - "\n" + - "int i = 0;\n" + - "boolean _while = (i != 10);\n" + - "while (_while) {\n" + - " int _i = i;\n" + - " i = (_i + 1);\n" + - " _while = (i != 10);\n" + - "}\n"); + compilesTo(""" + { + for (var i = 0; i != 10; i += 1) { + } + } + """, """ + int i = 0; + boolean _while = (i != 10); + while (_while) { + int _i = i; + i = (_i + 1); + _while = (i != 10); + } + """); } @Test public void testWhileLoopWithConstantCondition_01() throws Exception { - compilesTo( - "while (newArrayList('').empty || 2 == (1 << 1)) {\n" + - " return ''\n" + - "}\n", - "\n" + - "while ((org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(\"\").isEmpty() || (2 == (1 << 1)))) {\n" + - " return \"\";\n" + - "}\n" + - "return null;\n"); + compilesTo(""" + while (newArrayList('').empty || 2 == (1 << 1)) { + return '' + } + """, """ + while ((org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList("").isEmpty() || (2 == (1 << 1)))) { + return ""; + } + return null; + """); } @Test public void testWhileLoopWithConstantCondition_02() throws Exception { - compilesTo( - "while (2 == (1 << 1)) {\n" + - " return ''\n" + - "}\n", "\n" + - "while ((2 == (1 << 1))) {\n" + - " return \"\";\n" + - "}\n"); + compilesTo(""" + while (2 == (1 << 1)) { + return '' + } + """, """ + while ((2 == (1 << 1))) { + return ""; + } + """); } @Test public void testSynchronizedBlock_1() throws Exception { - compilesTo( - "{\n" + - " val x = new Integer(1)\n" + - " synchronized(x) {}\n" + - "}\n", - "\n" + - "Object _xblockexpression = null;\n" + - "{\n" + - " final Integer x = new Integer(1);\n" + - " Object _xsynchronizedexpression = null;\n" + - " synchronized (x) {\n" + - " _xsynchronizedexpression = null;\n" + - " }\n" + - " _xblockexpression = _xsynchronizedexpression;\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + val x = new Integer(1) + synchronized(x) {} + } + """, """ + Object _xblockexpression = null; + { + final Integer x = new Integer(1); + Object _xsynchronizedexpression = null; + synchronized (x) { + _xsynchronizedexpression = null; + } + _xblockexpression = _xsynchronizedexpression; + } + return _xblockexpression; + """); } @Test public void testSynchronizedBlock_2() throws Exception { - compilesTo( - "{\n" + - " val x = new Integer(1)\n" + - " val y = synchronized(x) 1\n" + - "}\n", - "\n" + - "final Integer x = new Integer(1);\n" + - "int _xsynchronizedexpression = (int) 0;\n" + - "synchronized (x) {\n" + - " _xsynchronizedexpression = 1;\n" + - "}\n" + - "final int y = _xsynchronizedexpression;\n"); + compilesTo(""" + { + val x = new Integer(1) + val y = synchronized(x) 1 + } + """, """ + final Integer x = new Integer(1); + int _xsynchronizedexpression = (int) 0; + synchronized (x) { + _xsynchronizedexpression = 1; + } + final int y = _xsynchronizedexpression; + """); } @Test public void testSynchronizedBlock_3() throws Exception { - compilesTo( - "{\n" + - " val x = new Integer(1)\n" + - " val y = synchronized(x) {\n" + - " {\n" + - " {\n" + - " val i = 1\n" + - " val j = 2\n" + - " i + j\n" + - " }\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "final Integer x = new Integer(1);\n" + - "int _xsynchronizedexpression = (int) 0;\n" + - "synchronized (x) {\n" + - " int _xblockexpression = (int) 0;\n" + - " {\n" + - " final int i = 1;\n" + - " final int j = 2;\n" + - " _xblockexpression = (i + j);\n" + - " }\n" + - " _xsynchronizedexpression = _xblockexpression;\n" + - "}\n" + - "final int y = _xsynchronizedexpression;\n"); + compilesTo(""" + { + val x = new Integer(1) + val y = synchronized(x) { + { + { + val i = 1 + val j = 2 + i + j + } + } + } + } + """, """ + final Integer x = new Integer(1); + int _xsynchronizedexpression = (int) 0; + synchronized (x) { + int _xblockexpression = (int) 0; + { + final int i = 1; + final int j = 2; + _xblockexpression = (i + j); + } + _xsynchronizedexpression = _xblockexpression; + } + final int y = _xsynchronizedexpression; + """); } @Test public void testBug410797_01() throws Exception { - compilesTo( - "{ val boolean bug = #[true, false, true].reduce[a,b|a && b] }\n", - "\n" + - "final org.eclipse.xtext.xbase.lib.Functions.Function2 _function = new org.eclipse.xtext.xbase.lib.Functions.Function2() {\n" + - " public Boolean apply(final Boolean a, final Boolean b) {\n" + - " return Boolean.valueOf(((a).booleanValue() && (b).booleanValue()));\n" + - " }\n" + - "};\n" + - "final boolean bug = (boolean) org.eclipse.xtext.xbase.lib.IterableExtensions.reduce(java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(Boolean.valueOf(true), Boolean.valueOf(false), Boolean.valueOf(true))), _function);\n"); + compilesTo(""" + { val boolean bug = #[true, false, true].reduce[a,b|a && b] } + """, + """ + final org.eclipse.xtext.xbase.lib.Functions.Function2 _function = new org.eclipse.xtext.xbase.lib.Functions.Function2() { + public Boolean apply(final Boolean a, final Boolean b) { + return Boolean.valueOf(((a).booleanValue() && (b).booleanValue())); + } + }; + final boolean bug = (boolean) org.eclipse.xtext.xbase.lib.IterableExtensions.reduce(java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(Boolean.valueOf(true), Boolean.valueOf(false), Boolean.valueOf(true))), _function); + """); } @Test public void testBug434224_01() throws Exception { - compilesTo( - "{\n" + - " val bar = new Object\n" + - " if (bar instanceof Byte) {\n" + - " bar as char\n" + - " }\n" + - "}\n", - "\n" + - "char _xblockexpression = (char) 0;\n" + - "{\n" + - " final Object bar = new Object();\n" + - " char _xifexpression = (char) 0;\n" + - " if ((bar instanceof Byte)) {\n" + - " _xifexpression = ((char) ((Byte) bar).byteValue());\n" + - " }\n" + - " _xblockexpression = _xifexpression;\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + val bar = new Object + if (bar instanceof Byte) { + bar as char + } + } + """, """ + char _xblockexpression = (char) 0; + { + final Object bar = new Object(); + char _xifexpression = (char) 0; + if ((bar instanceof Byte)) { + _xifexpression = ((char) ((Byte) bar).byteValue()); + } + _xblockexpression = _xifexpression; + } + return _xblockexpression; + """); } @Test public void testBug434224_02() throws Exception { - compilesTo( - "{\n" + - " switch bar : new Object {\n" + - " Byte: bar as char\n" + - " }\n" + - "}\n", - "\n" + - "char _switchResult = (char) 0;\n" + - "Object _object = new Object();\n" + - "final Object bar = _object;\n" + - "boolean _matched = false;\n" + - "if (bar instanceof Byte) {\n" + - " _matched=true;\n" + - " _switchResult = ((char) ((Byte) bar).byteValue());\n" + - "}\n" + - "return _switchResult;\n"); + compilesTo(""" + { + switch bar : new Object { + Byte: bar as char + } + } + """, """ + char _switchResult = (char) 0; + Object _object = new Object(); + final Object bar = _object; + boolean _matched = false; + if (bar instanceof Byte) { + _matched=true; + _switchResult = ((char) ((Byte) bar).byteValue()); + } + return _switchResult; + """); } @Test public void testBug433573_01() throws Exception { - compilesTo( - "{\n" + - " val Number element = null\n" + - " if(element instanceof Double) {\n" + - " val Iterable i = #[element]\n" + - " }\n" + - "}\n", - "\n" + - "final Number element = null;\n" + - "if ((element instanceof Double)) {\n" + - " final Iterable i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(element));\n" + - "}\n"); + compilesTo(""" + { + val Number element = null + if(element instanceof Double) { + val Iterable i = #[element] + } + } + """, + """ + final Number element = null; + if ((element instanceof Double)) { + final Iterable i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(element)); + } + """); } @Test public void testBug433573_02() throws Exception { - compilesTo( - "{\n" + - " val Number element = null\n" + - " switch element {\n" + - " Double: {\n" + - " val Iterable i = #[element]\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "final Number element = null;\n" + - "boolean _matched = false;\n" + - "if (element instanceof Double) {\n" + - " _matched=true;\n" + - " final Iterable i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(element));\n" + - "}\n"); + compilesTo(""" + { + val Number element = null + switch element { + Double: { + val Iterable i = #[element] + } + } + } + """, + """ + final Number element = null; + boolean _matched = false; + if (element instanceof Double) { + _matched=true; + final Iterable i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(element)); + } + """); } @Test public void testBug433573_03() throws Exception { - compilesTo( - "{\n" + - " val Number element = null\n" + - " if(element instanceof Double) {\n" + - " val Iterable i = #{element}\n" + - " }\n" + - "}\n", - "\n" + - "final Number element = null;\n" + - "if ((element instanceof Double)) {\n" + - " final Iterable i = java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(element));\n" + - "}\n"); + compilesTo(""" + { + val Number element = null + if(element instanceof Double) { + val Iterable i = #{element} + } + } + """, + """ + final Number element = null; + if ((element instanceof Double)) { + final Iterable i = java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(element)); + } + """); } @Test public void testBug433573_04() throws Exception { - compilesTo( - "{\n" + - " val Number element = null\n" + - " switch element {\n" + - " Double: {\n" + - " val Iterable i = #{element}\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "final Number element = null;\n" + - "boolean _matched = false;\n" + - "if (element instanceof Double) {\n" + - " _matched=true;\n" + - " final Iterable i = java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(element));\n" + - "}\n"); + compilesTo(""" + { + val Number element = null + switch element { + Double: { + val Iterable i = #{element} + } + } + } + """, + """ + final Number element = null; + boolean _matched = false; + if (element instanceof Double) { + _matched=true; + final Iterable i = java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(element)); + } + """); } @Test public void testBug433573_05() throws Exception { - compilesTo( - "{\n" + - " val Object element = null\n" + - " if(element instanceof Double) {\n" + - " val Iterable i = #[element]\n" + - " }\n" + - "}\n", - "\n" + - "final Object element = null;\n" + - "if ((element instanceof Double)) {\n" + - " final Iterable i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(((Number)element)));\n" + - "}\n"); + compilesTo(""" + { + val Object element = null + if(element instanceof Double) { + val Iterable i = #[element] + } + } + """, + """ + final Object element = null; + if ((element instanceof Double)) { + final Iterable i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(((Number)element))); + } + """); } @Test public void testBug433573_06() throws Exception { - compilesTo( - "{\n" + - " val Object element = null\n" + - " switch element {\n" + - " Double: {\n" + - " val Iterable i = #[element]\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "final Object element = null;\n" + - "boolean _matched = false;\n" + - "if (element instanceof Double) {\n" + - " _matched=true;\n" + - " final Iterable i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(((Number)element)));\n" + - "}\n"); + compilesTo(""" + { + val Object element = null + switch element { + Double: { + val Iterable i = #[element] + } + } + } + """, + """ + final Object element = null; + boolean _matched = false; + if (element instanceof Double) { + _matched=true; + final Iterable i = java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(((Number)element))); + } + """); } @Test public void testBug433573_07() throws Exception { - compilesTo( - "{\n" + - " val Object element = null\n" + - " if(element instanceof Double) {\n" + - " val Iterable i = #{element}\n" + - " }\n" + - "}\n", - "\n" + - "final Object element = null;\n" + - "if ((element instanceof Double)) {\n" + - " final Iterable i = java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(((Number)element)));\n" + - "}\n"); + compilesTo(""" + { + val Object element = null + if(element instanceof Double) { + val Iterable i = #{element} + } + } + """, + """ + final Object element = null; + if ((element instanceof Double)) { + final Iterable i = java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(((Number)element))); + } + """); } @Test public void testBug433573_08() throws Exception { - compilesTo( - "{\n" + - " val Object element = null\n" + - " switch element {\n" + - " Double: {\n" + - " val Iterable i = #{element}\n" + - " }\n" + - " }\n" + - "}\n", - "\n" + - "final Object element = null;\n" + - "boolean _matched = false;\n" + - "if (element instanceof Double) {\n" + - " _matched=true;\n" + - " final Iterable i = java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(((Number)element)));\n" + - "}\n"); + compilesTo(""" + { + val Object element = null + switch element { + Double: { + val Iterable i = #{element} + } + } + } + """, + """ + final Object element = null; + boolean _matched = false; + if (element instanceof Double) { + _matched=true; + final Iterable i = java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(((Number)element))); + } + """); } @Test public void testBug466974_01() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " Math.max( i = i + 1, if (i == 1) { 1 } else { 2 })\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int _i = i = (i + 1);\n" + - " int _xifexpression = (int) 0;\n" + - " if ((i == 1)) {\n" + - " _xifexpression = 1;\n" + - " } else {\n" + - " _xifexpression = 2;\n" + - " }\n" + - " _xblockexpression = Math.max(_i, _xifexpression);\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + Math.max( i = i + 1, if (i == 1) { 1 } else { 2 }) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int _i = i = (i + 1); + int _xifexpression = (int) 0; + if ((i == 1)) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + _xblockexpression = Math.max(_i, _xifexpression); + } + return _xblockexpression; + """); } @Test public void testBug466974_02() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " Math.max(if (i == 1) { 1 } else { 2 }, i = i + 1)\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int _xifexpression = (int) 0;\n" + - " if ((i == 1)) {\n" + - " _xifexpression = 1;\n" + - " } else {\n" + - " _xifexpression = 2;\n" + - " }\n" + - " _xblockexpression = Math.max(_xifexpression, i = (i + 1));\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + Math.max(if (i == 1) { 1 } else { 2 }, i = i + 1) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int _xifexpression = (int) 0; + if ((i == 1)) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + _xblockexpression = Math.max(_xifexpression, i = (i + 1)); + } + return _xblockexpression; + """); } @Test public void testBug466974_03() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " Math.max({ i = i + 1 }, if (i == 1) { 1 } else { 2 })\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int _i = i = (i + 1);\n" + - " int _xifexpression = (int) 0;\n" + - " if ((i == 1)) {\n" + - " _xifexpression = 1;\n" + - " } else {\n" + - " _xifexpression = 2;\n" + - " }\n" + - " _xblockexpression = Math.max(_i, _xifexpression);\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + Math.max({ i = i + 1 }, if (i == 1) { 1 } else { 2 }) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int _i = i = (i + 1); + int _xifexpression = (int) 0; + if ((i == 1)) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + _xblockexpression = Math.max(_i, _xifexpression); + } + return _xblockexpression; + """); } @Test public void testBug466974_04() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " Math.max(if (i == 1) { 1 } else { 2 }, { i = i + 1 })\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int _xifexpression = (int) 0;\n" + - " if ((i == 1)) {\n" + - " _xifexpression = 1;\n" + - " } else {\n" + - " _xifexpression = 2;\n" + - " }\n" + - " _xblockexpression = Math.max(_xifexpression, i = (i + 1));\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + Math.max(if (i == 1) { 1 } else { 2 }, { i = i + 1 }) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int _xifexpression = (int) 0; + if ((i == 1)) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + _xblockexpression = Math.max(_xifexpression, i = (i + 1)); + } + return _xblockexpression; + """); } @Test public void testBug466974_05() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " {i = i + 1}.compareTo(if (i == 1) { 1 } else { 2 })\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int _i = i = (i + 1);\n" + - " int _xifexpression = (int) 0;\n" + - " if ((i == 1)) {\n" + - " _xifexpression = 1;\n" + - " } else {\n" + - " _xifexpression = 2;\n" + - " }\n" + - " _xblockexpression = Integer.valueOf(_i).compareTo(Integer.valueOf(_xifexpression));\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + {i = i + 1}.compareTo(if (i == 1) { 1 } else { 2 }) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int _i = i = (i + 1); + int _xifexpression = (int) 0; + if ((i == 1)) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + _xblockexpression = Integer.valueOf(_i).compareTo(Integer.valueOf(_xifexpression)); + } + return _xblockexpression; + """); } @Test public void testBug466974_06() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " {if (i == 1) { 1 } else { 2 }}.compareTo(i = i + 1)\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int _xifexpression = (int) 0;\n" + - " if ((i == 1)) {\n" + - " _xifexpression = 1;\n" + - " } else {\n" + - " _xifexpression = 2;\n" + - " }\n" + - " _xblockexpression = Integer.valueOf(_xifexpression).compareTo(Integer.valueOf(i = (i + 1)));\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + {if (i == 1) { 1 } else { 2 }}.compareTo(i = i + 1) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int _xifexpression = (int) 0; + if ((i == 1)) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + _xblockexpression = Integer.valueOf(_xifexpression).compareTo(Integer.valueOf(i = (i + 1))); + } + return _xblockexpression; + """); } @Test public void testBug466974_07() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " {{i = i + 1}}.compareTo(if (i == 1) { 1 } else { 2 })\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int _i = i = (i + 1);\n" + - " int _xifexpression = (int) 0;\n" + - " if ((i == 1)) {\n" + - " _xifexpression = 1;\n" + - " } else {\n" + - " _xifexpression = 2;\n" + - " }\n" + - " _xblockexpression = Integer.valueOf(_i).compareTo(Integer.valueOf(_xifexpression));\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + {{i = i + 1}}.compareTo(if (i == 1) { 1 } else { 2 }) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int _i = i = (i + 1); + int _xifexpression = (int) 0; + if ((i == 1)) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + _xblockexpression = Integer.valueOf(_i).compareTo(Integer.valueOf(_xifexpression)); + } + return _xblockexpression; + """); } @Test public void testBug466974_08() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " {if (i == 1) { 1 } else { 2 }}.compareTo({i = i + 1})\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int _xifexpression = (int) 0;\n" + - " if ((i == 1)) {\n" + - " _xifexpression = 1;\n" + - " } else {\n" + - " _xifexpression = 2;\n" + - " }\n" + - " _xblockexpression = Integer.valueOf(_xifexpression).compareTo(Integer.valueOf(i = (i + 1)));\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + {if (i == 1) { 1 } else { 2 }}.compareTo({i = i + 1}) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int _xifexpression = (int) 0; + if ((i == 1)) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + _xblockexpression = Integer.valueOf(_xifexpression).compareTo(Integer.valueOf(i = (i + 1))); + } + return _xblockexpression; + """); } @Test public void testBug466974_09() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " Math.max(i = i + 1, 2)\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " _xblockexpression = Math.max(i = (i + 1), 2);\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + Math.max(i = i + 1, 2) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + _xblockexpression = Math.max(i = (i + 1), 2); + } + return _xblockexpression; + """); } @Test public void testBug466974_10() throws Exception { - compilesTo( - "{\n" + - " var i = 0\n" + - " {i = i + 1}.compareTo(2)\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " _xblockexpression = Integer.valueOf(i = (i + 1)).compareTo(Integer.valueOf(2));\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + {i = i + 1}.compareTo(2) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + _xblockexpression = Integer.valueOf(i = (i + 1)).compareTo(Integer.valueOf(2)); + } + return _xblockexpression; + """); } @Test public void testBug489037_NestedAssignment_1() throws Exception { - compilesTo( - "\n" + - "{\n" + - " var i = 0\n" + - " var j = 0\n" + - " j=i=0;\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int j = 0;\n" + - " _xblockexpression = j = (i = 0);\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + var j = 0 + j=i=0; + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int j = 0; + _xblockexpression = j = (i = 0); + } + return _xblockexpression; + """); } @Test public void testBug489037_NestedAssignment_2() throws Exception { - compilesTo( - "\n" + - "{\n" + - " var i = 0\n" + - " var n = 1\n" + - " n=i=(if (i==0) 1 else 2)\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int n = 1;\n" + - " int _xifexpression = (int) 0;\n" + - " if ((i == 0)) {\n" + - " _xifexpression = 1;\n" + - " } else {\n" + - " _xifexpression = 2;\n" + - " }\n" + - " int _i = (i = _xifexpression);\n" + - " _xblockexpression = n = _i;\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + var n = 1 + n=i=(if (i==0) 1 else 2) + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int n = 1; + int _xifexpression = (int) 0; + if ((i == 0)) { + _xifexpression = 1; + } else { + _xifexpression = 2; + } + int _i = (i = _xifexpression); + _xblockexpression = n = _i; + } + return _xblockexpression; + """); } @Test public void testBug489037_NestedAssignment_3() throws Exception { - compilesTo( - "\n" + - "{\n" + - " var i = 0\n" + - " var n = 1\n" + - " n=i=switch(i) {\n" + - " default: 2\n" + - " }\n" + - "}\n", - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int i = 0;\n" + - " int n = 1;\n" + - " int _switchResult = (int) 0;\n" + - " switch (i) {\n" + - " default:\n" + - " _switchResult = 2;\n" + - " break;\n" + - " }\n" + - " int _i = (i = _switchResult);\n" + - " _xblockexpression = n = _i;\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + var i = 0 + var n = 1 + n=i=switch(i) { + default: 2 + } + } + """, """ + int _xblockexpression = (int) 0; + { + int i = 0; + int n = 1; + int _switchResult = (int) 0; + switch (i) { + default: + _switchResult = 2; + break; + } + int _i = (i = _switchResult); + _xblockexpression = n = _i; + } + return _xblockexpression; + """); } @Test public void test406762_NestedReturn() throws Exception { - compilesTo( - "{\n" + - " return return 0\n" + - "}\n", "\n" + - "return 0;\n" + - "return /* error - couldn't compile nested return */;\n", false); + compilesTo(""" + { + return return 0 + } + """, """ + return 0; + return /* error - couldn't compile nested return */; + """, false); } @Test public void test406762_NestedReturn2() throws Exception { - compilesTo( - "{\n" + - " return { \n" + - " return 0\n" + - " }\n" + - "}\n", "\n" + - "return 0;\n" + - "return /* error - couldn't compile nested return */;\n", false); + compilesTo(""" + { + return { + return 0 + } + } + """, """ + return 0; + return /* error - couldn't compile nested return */; + """, false); } @Test public void test406762_NestedReturn3() throws Exception { - compilesTo( - "{\n" + - " return { \n" + - " if (true) return 0 else return 1\n" + - " }\n" + - "}\n", - "\n" + - "int _xifexpression = (int) 0;\n" + - "if (true) {\n" + - " return 0;\n" + - "} else {\n" + - " return 1;\n" + - "}\n" + - "return _xifexpression;\n", false); + compilesTo(""" + { + return { + if (true) return 0 else return 1 + } + } + """, """ + int _xifexpression = (int) 0; + if (true) { + return 0; + } else { + return 1; + } + return _xifexpression; + """, false); } @Test public void test406762_NestedReturn4() throws Exception { - compilesTo( - "{\n" + - " return \n" + - " if (true) return 0 else return 1\n" + - "}\n", - "\n" + - "int _xifexpression = (int) 0;\n" + - "if (true) {\n" + - " return 0;\n" + - "} else {\n" + - " return 1;\n" + - "}\n" + - "return _xifexpression;\n", false); + compilesTo(""" + { + return + if (true) return 0 else return 1 + } + """, """ + int _xifexpression = (int) 0; + if (true) { + return 0; + } else { + return 1; + } + return _xifexpression; + """, false); } @Test public void test406762_ReturnInThrow() throws Exception { - compilesTo( - "{\n" + - " throw return\n" + - "}\n", - "\n" + - "try {\n" + - " return;\n" + - " throw /* error - couldn't compile nested return */;\n" + - "} catch (Throwable _e) {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e);\n" + - "}\n", - false); + compilesTo(""" + { + throw return + } + """, """ + try { + return; + throw /* error - couldn't compile nested return */; + } catch (Throwable _e) { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e); + } + """, false); } @Test public void test406762_ReturnInThrow2() throws Exception { - compilesTo( - "{\n" + - " throw {\n" + - " return\n" + - " }\n" + - "}\n", - "\n" + - "try {\n" + - " return;\n" + - " throw /* error - couldn't compile nested return */;\n" + - "} catch (Throwable _e) {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e);\n" + - "}\n", - false); + compilesTo(""" + { + throw { + return + } + } + """, """ + try { + return; + throw /* error - couldn't compile nested return */; + } catch (Throwable _e) { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e); + } + """, false); } @Test public void test406762_ReturnThrow() throws Exception { - compilesTo( - "{\n" + - " return throw new Exception()\n" + - "}\n", - "\n" + - "try {\n" + - " throw new Exception();\n" + - " return /* error - couldn't compile invalid throw */;\n" + - "} catch (Throwable _e) {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e);\n" + - "}\n", - false); + compilesTo(""" + { + return throw new Exception() + } + """, """ + try { + throw new Exception(); + return /* error - couldn't compile invalid throw */; + } catch (Throwable _e) { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e); + } + """, false); } @Test public void test406762_ValidThrowInSingleIfBranch() throws Exception { - compilesTo( - "{\n" + - " val b = true\n" + - " return if (b) throw new RuntimeException() else 42\n" + - "}\n", - "\n" + - "final boolean b = true;\n" + - "int _xifexpression = (int) 0;\n" + - "if (b) {\n" + - " throw new RuntimeException();\n" + - "} else {\n" + - " _xifexpression = 42;\n" + - "}\n" + - "return _xifexpression;\n"); + compilesTo(""" + { + val b = true + return if (b) throw new RuntimeException() else 42 + } + """, """ + final boolean b = true; + int _xifexpression = (int) 0; + if (b) { + throw new RuntimeException(); + } else { + _xifexpression = 42; + } + return _xifexpression; + """); } @Test public void test406762_ValidThrowInSingleIfBranch_1() throws Exception { - compilesTo( - "{\n" + - " val b = true\n" + - " return if (b) 42 else throw new RuntimeException()\n" + - "}\n", - "\n" + - "final boolean b = true;\n" + - "int _xifexpression = (int) 0;\n" + - "if (b) {\n" + - " _xifexpression = 42;\n" + - "} else {\n" + - " throw new RuntimeException();\n" + - "}\n" + - "return _xifexpression;\n"); + compilesTo(""" + { + val b = true + return if (b) 42 else throw new RuntimeException() + } + """, """ + final boolean b = true; + int _xifexpression = (int) 0; + if (b) { + _xifexpression = 42; + } else { + throw new RuntimeException(); + } + return _xifexpression; + """); } @Test public void test406762_InvalidThrowInBothIfBranches() throws Exception { - compilesTo( - "{\n" + - " val b = true\n" + - " return if (b) throw new RuntimeException() else throw new RuntimeException()\n" + - "}\n", - "\n" + - "final boolean b = true;\n" + - "void _xifexpression = null;\n" + - "if (b) {\n" + - " throw new RuntimeException();\n" + - "} else {\n" + - " throw new RuntimeException();\n" + - "}\n" + - "return _xifexpression;\n", - false); + compilesTo(""" + { + val b = true + return if (b) throw new RuntimeException() else throw new RuntimeException() + } + """, """ + final boolean b = true; + void _xifexpression = null; + if (b) { + throw new RuntimeException(); + } else { + throw new RuntimeException(); + } + return _xifexpression; + """, false); } @Test public void test406762_ValidReturnInLambdaContainedInThrow() throws Exception { - compilesTo( - "throw {\n" + - " val foo = [|return \"foo\"]\n" + - " new Exception(foo.apply)\n" + - "}\n", - "\n" + - "try {\n" + - " Exception _xblockexpression = null;\n" + - " {\n" + - " final org.eclipse.xtext.xbase.lib.Functions.Function0 _function = new org.eclipse.xtext.xbase.lib.Functions.Function0() {\n" + - " public String apply() {\n" + - " return \"foo\";\n" + - " }\n" + - " };\n" + - " final org.eclipse.xtext.xbase.lib.Functions.Function0 foo = _function;\n" + - " String _apply = foo.apply();\n" + - " _xblockexpression = new Exception(_apply);\n" + - " }\n" + - " throw _xblockexpression;\n" + - "} catch (Throwable _e) {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e);\n" + - "}\n"); + compilesTo(""" + throw { + val foo = [|return "foo"] + new Exception(foo.apply) + } + """, + """ + try { + Exception _xblockexpression = null; + { + final org.eclipse.xtext.xbase.lib.Functions.Function0 _function = new org.eclipse.xtext.xbase.lib.Functions.Function0() { + public String apply() { + return "foo"; + } + }; + final org.eclipse.xtext.xbase.lib.Functions.Function0 foo = _function; + String _apply = foo.apply(); + _xblockexpression = new Exception(_apply); + } + throw _xblockexpression; + } catch (Throwable _e) { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e); + } + """); } @Test public void test406762_ValidReturnInLambdaContainedInThrow_1() throws Exception { - compilesTo( - "throw {\n" + - " val ()=>Exception foo = [|return new Exception]\n" + - " foo.apply\n" + - "}\n", - "\n" + - "try {\n" + - " Exception _xblockexpression = null;\n" + - " {\n" + - " final org.eclipse.xtext.xbase.lib.Functions.Function0 _function = new org.eclipse.xtext.xbase.lib.Functions.Function0() {\n" + - " public Exception apply() {\n" + - " return new Exception();\n" + - " }\n" + - " };\n" + - " final org.eclipse.xtext.xbase.lib.Functions.Function0 foo = _function;\n" + - " _xblockexpression = foo.apply();\n" + - " }\n" + - " throw _xblockexpression;\n" + - "} catch (Throwable _e) {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e);\n" + - "}\n"); + compilesTo(""" + throw { + val ()=>Exception foo = [|return new Exception] + foo.apply + } + """, + """ + try { + Exception _xblockexpression = null; + { + final org.eclipse.xtext.xbase.lib.Functions.Function0 _function = new org.eclipse.xtext.xbase.lib.Functions.Function0() { + public Exception apply() { + return new Exception(); + } + }; + final org.eclipse.xtext.xbase.lib.Functions.Function0 foo = _function; + _xblockexpression = foo.apply(); + } + throw _xblockexpression; + } catch (Throwable _e) { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e); + } + """); } @Test public void testObjectEqualNull() throws Exception { - compilesTo( - "\"Foo\" == null\n", "\n" + - "boolean _equals = com.google.common.base.Objects.equal(\"Foo\", null);\n" + - "return _equals;\n"); + compilesTo(""" + "Foo" == null + """, """ + boolean _equals = com.google.common.base.Objects.equal("Foo", null); + return _equals; + """); } @Test public void testObjectNotEqualNull() throws Exception { - compilesTo( - "\"Foo\" != null\n", - "\n" + - "boolean _notEquals = (!com.google.common.base.Objects.equal(\"Foo\", null));\n" + - "return _notEquals;\n"); + compilesTo(""" + "Foo" != null + """, """ + boolean _notEquals = (!com.google.common.base.Objects.equal("Foo", null)); + return _notEquals; + """); } @Test public void testObjectIdenticalNull() throws Exception { - compilesTo( - "\"Foo\" === null\n", "\n" + - "return (\"Foo\" == null);\n"); + compilesTo(""" + "Foo" === null + """, """ + return ("Foo" == null); + """); } @Test public void testObjectNotIdenticalNull() throws Exception { - compilesTo( - "\"Foo\" !== null\n", "\n" + - "return (\"Foo\" != null);\n"); + compilesTo(""" + "Foo" !== null + """, """ + return ("Foo" != null); + """); } @Test public void testBigIntegerLiteral01() throws Exception { - compilesTo( - "1bi\n", "\n" + - "return java.math.BigInteger.ONE;\n"); + compilesTo("1bi", "return java.math.BigInteger.ONE;"); } @Test public void testBigIntegerLiteral02() throws Exception { - compilesTo( - "1.0bi\n", "\n" + - "return java.math.BigInteger.ONE;\n"); + compilesTo("1.0bi", "return java.math.BigInteger.ONE;"); } @Test public void testBigIntegerLiteral03() throws Exception { - compilesTo( - "1e23bi\n", "\n" + - "return new java.math.BigInteger(\"1\").multiply(java.math.BigInteger.TEN.pow(23));\n"); + compilesTo("1e23bi", """ + return new java.math.BigInteger("1").multiply(java.math.BigInteger.TEN.pow(23)); + """); } @Test public void testBigIntegerLiteral04() throws Exception { - compilesTo( - "1.23e45bi\n", - "\n" + - "return new java.math.BigDecimal(\"1.23\").multiply(java.math.BigDecimal.TEN.pow(45)).toBigInteger();\n"); + compilesTo("1.23e45bi", """ + return new java.math.BigDecimal("1.23").multiply(java.math.BigDecimal.TEN.pow(45)).toBigInteger(); + """); } @Test public void testExtrasIssue394() throws Exception { - compilesTo( - "newArrayOfSize(1).get(0)\n", "\n" + - "Object _get = (new Object[1])[0];\n" + - "return _get;\n"); + compilesTo("newArrayOfSize(1).get(0)", """ + Object _get = (new Object[1])[0]; + return _get; + """); } @Test public void testExtrasIssue394_2() throws Exception { - compilesTo( - "newArrayOfSize(1)\n", "\n" + - "Object[] _newArrayOfSize = new Object[1];\n" + - "return _newArrayOfSize;\n"); + compilesTo("newArrayOfSize(1)", """ + Object[] _newArrayOfSize = new Object[1]; + return _newArrayOfSize; + """); } @Test public void testLibIssue60() throws Exception { - compilesTo( - "{\n" + - " val a = newArrayOfSize(1)\n" + - " a.set(0, \"\").length\n" + - " a.set(0,\"hello\")\n" + - " 1\n" + - "}\n", - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " final String[] a = new String[1];\n" + - " (a[0] = \"\").length();\n" + - " a[0] = \"hello\";\n" + - " _xblockexpression = 1;\n" + - "}\n" + - "return _xblockexpression;\n"); + compilesTo(""" + { + val a = newArrayOfSize(1) + a.set(0, "").length + a.set(0,"hello") + 1 + } + """, """ + int _xblockexpression = (int) 0; + { + final String[] a = new String[1]; + (a[0] = "").length(); + a[0] = "hello"; + _xblockexpression = 1; + } + return _xblockexpression; + """); } } diff --git a/org.eclipse.xtext.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/CompilerTest.java b/org.eclipse.xtext.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/CompilerTest.java index 35dc388465b..3e09067c441 100644 --- a/org.eclipse.xtext.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/CompilerTest.java +++ b/org.eclipse.xtext.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/CompilerTest.java @@ -31,918 +31,930 @@ protected FakeTreeAppendable createAppendable() { private IGeneratorConfigProvider generatorConfigProvider; @Test public void testSimple() throws Exception { - assertCompilesTo("\nint _length = \"foo\".length();\n" + - "return _length;", "'foo'.length"); + assertCompilesTo(""" + int _length = "foo".length(); + return _length; + """, "'foo'.length"); } @Test public void testExtensionOnTypeLiteral() throws Exception { - assertCompilesTo("\nboolean _identityEquals = (org.eclipse.xtext.xbase.lib.StringExtensions.class == null);\n" + - "return _identityEquals;", "org.eclipse.xtext.xbase.lib.StringExtensions.identityEquals(null)"); + assertCompilesTo(""" + boolean _identityEquals = (org.eclipse.xtext.xbase.lib.StringExtensions.class == null); + return _identityEquals; + """, "org.eclipse.xtext.xbase.lib.StringExtensions.identityEquals(null)"); } @Test public void testBug377855() throws Exception { - assertCompilesToStatement( - "\n" + - "try {\n" + - " Class clazz = Class.forName(\"java.lang.String\");\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.>println(clazz.getSuperclass());\n" + - "} catch (Throwable _e) {\n" + - " throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e);\n" + - "}", - "{" + - " var clazz = Class::forName('java.lang.String')\n" + - " println(clazz.superclass)\n" + - "}"); + assertCompilesToStatement(""" + + try { + Class clazz = Class.forName("java.lang.String"); + org.eclipse.xtext.xbase.lib.InputOutput.>println(clazz.getSuperclass()); + } catch (Throwable _e) { + throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_e); + }""", """ + { + var clazz = Class::forName('java.lang.String') + println(clazz.superclass) + } + """); } @Test public void testBug383551_01() throws Exception { - assertCompilesToStatement( - "\n" + - "final java.util.List list = null;\n" + - "Object _switchResult = null;\n" + - "boolean _matched = false;\n" + - "if (list instanceof java.util.ArrayList) {\n" + - " _matched=true;\n" + - " _switchResult = ((java.util.ArrayList)list).get(1);\n" + - "}\n" + - "final Object it = _switchResult;\n" + - "it.toString();", - "{" + - " val java.util.List list = null\n" + - " val Object it = switch list {\n" + - " java.util.ArrayList: list.get(1)\n" + - " }\n" + - " toString" + - "}"); - } + assertCompilesToStatement(""" + final java.util.List list = null; + Object _switchResult = null; + boolean _matched = false; + if (list instanceof java.util.ArrayList) { + _matched=true; + _switchResult = ((java.util.ArrayList)list).get(1); + } + final Object it = _switchResult; + it.toString();""", """ + { + val java.util.List list = null + val Object it = switch list { + java.util.ArrayList: list.get(1) + } + toString + } + """); + } + @Ignore("TODO automatically insert type arguments after instanceof expressions") @Test public void testBug383551_02() throws Exception { - assertCompilesToStatement( - "\n" + - "final java.util.List list = null;\n" + - "Object _switchResult = null;\n" + - "boolean _matched = false;\n" + - "if (!_matched) {\n" + - " if (list instanceof java.util.ArrayList) {\n" + - " _matched=true;\n" + - " Object _get = ((java.util.ArrayList)list).get(1);\n" + - " _switchResult = _get;\n" + - " }\n" + - "}\n" + - "final String it = _switchResult;\n" + - "it.substring(1);", - "{" + - " val java.util.List list = null\n" + - " val it = switch list {\n" + - " java.util.ArrayList: list.get(1)\n" + - " }\n" + - " substring(1)" + - "}"); + assertCompilesToStatement(""" + final java.util.List list = null; + Object _switchResult = null; + boolean _matched = false; + if (!_matched) { + if (list instanceof java.util.ArrayList) { + _matched=true; + Object _get = ((java.util.ArrayList)list).get(1); + _switchResult = _get; + } + } + final String it = _switchResult; + it.substring(1); + """, """ + { + val java.util.List list = null + val it = switch list { + java.util.ArrayList: list.get(1) + } + substring(1) + } + """); } @Test public void testBug383551_03() throws Exception { - assertCompilesToStatement( - "\n" + - "final java.util.List list = null;\n" + - "String _switchResult = null;\n" + - "boolean _matched = false;\n" + - "if (list instanceof java.util.ArrayList) {\n" + - " _matched=true;\n" + - " _switchResult = ((java.util.ArrayList)list).get(1);\n" + - "}\n" + - "final String it = _switchResult;\n" + - "it.substring(1);", - "{" + - " val java.util.List list = null\n" + - " val it = switch list {\n" + - " java.util.ArrayList: list.get(1)\n" + - " }\n" + - " substring(1)" + - "}"); + assertCompilesToStatement(""" + + final java.util.List list = null; + String _switchResult = null; + boolean _matched = false; + if (list instanceof java.util.ArrayList) { + _matched=true; + _switchResult = ((java.util.ArrayList)list).get(1); + } + final String it = _switchResult; + it.substring(1);""", """ + { + val java.util.List list = null + val it = switch list { + java.util.ArrayList: list.get(1) + } + substring(1) + } + """); } @Test public void testBug383551_04() throws Exception { - assertCompilesToStatement( - "\n" + - "final Object o = null;\n" + - "if ((o instanceof String)) {\n" + - " ((String) o).length();\n" + - "}", - "{" + - " val Object o = null\n" + - " if (o instanceof String) (o as String).length()" + - "}"); + assertCompilesToStatement(""" + + final Object o = null; + if ((o instanceof String)) { + ((String) o).length(); + }""", """ + { + val Object o = null + if (o instanceof String) (o as String).length() + } + """); } @Test public void testImplicitReferenceToMultitype() throws Exception { - assertCompilesTo( - // TODO AbstractStringBuilder is package private and should not be part of the resolved type - "Iterable _plus = com.google.common.collect.Iterables.concat(((Iterable) null), ((Iterable) null));\n" + - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final Object it) {\n" + - " ((CharSequence)it).length();\n" + - " }\n" + - "};\n" + - "com.google.common.collect.Iterables.concat(_plus, ((Iterable) null)).forEach(_function);", + assertCompilesTo(""" + Iterable _plus = com.google.common.collect.Iterables.\ + concat(((Iterable) null), ((Iterable) null)); + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final Object it) { + ((CharSequence)it).length(); + } + }; + com.google.common.collect.Iterables.concat(_plus, ((Iterable) null)).forEach(_function); + """, "((null as Iterable) + (null as Iterable) + (null as Iterable)).forEach[ length ]"); } @Test public void testReferenceToSynonym_01() throws Exception { - assertCompilesTo( - "int _size = ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(((String[]) null))).size();\n" + - "return _size;", - "(null as String[]).size"); + assertCompilesTo(""" + int _size = ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(((String[]) null))).size(); + return _size; + """, "(null as String[]).size"); } @Test public void testImplicitReferenceToSynonym_01() throws Exception { - assertCompilesTo( - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final String[] it) {\n" + - " ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).subList(1, 1);\n" + - " }\n" + - "};\n" + - "((Iterable) null).forEach(_function);", - "(null as Iterable).forEach[ subList(1,1) ]"); + assertCompilesTo(""" + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final String[] it) { + ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).subList(1, 1); + } + }; + ((Iterable) null).forEach(_function); + """, "(null as Iterable).forEach[ subList(1,1) ]"); } @Test public void testImplicitReferenceToSynonym_02() throws Exception { - assertCompilesTo( - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final String[] it) {\n" + - " ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).size();\n" + - " }\n" + - "};\n" + - "((Iterable) null).forEach(_function);", - "(null as Iterable).forEach[ size() ]"); + assertCompilesTo(""" + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final String[] it) { + ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).size(); + } + }; + ((Iterable) null).forEach(_function); + """, "(null as Iterable).forEach[ size() ]"); } @Test public void testImplicitReferenceToSynonymWithPrimitives() throws Exception { - assertCompilesTo( - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final int[] it) {\n" + - " ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).subList(1, 1);\n" + - " }\n" + - "};\n" + - "((Iterable) null).forEach(_function);", - "(null as Iterable).forEach[ subList(1,1) ]"); + assertCompilesTo(""" + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final int[] it) { + ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).subList(1, 1); + } + }; + ((Iterable) null).forEach(_function); + """, "(null as Iterable).forEach[ subList(1,1) ]"); } @Test public void testImplicitReferenceToArray() throws Exception { - assertCompilesTo( - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final String[] it) {\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.println(Integer.valueOf(it.length));\n" + - " }\n" + - "};\n" + - "((Iterable) null).forEach(_function);", - "(null as Iterable).forEach[ println(length) ]"); + assertCompilesTo(""" + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final String[] it) { + org.eclipse.xtext.xbase.lib.InputOutput.println(Integer.valueOf(it.length)); + } + }; + ((Iterable) null).forEach(_function); + """, "(null as Iterable).forEach[ println(length) ]"); } @Test public void testExplicitReferenceToSynonym_01() throws Exception { - assertCompilesTo( - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final String[] it) {\n" + - " ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).subList(1, 1);\n" + - " }\n" + - "};\n" + - "((Iterable) null).forEach(_function);", - "(null as Iterable).forEach[ it.subList(1,1) ]"); + assertCompilesTo(""" + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final String[] it) { + ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).subList(1, 1); + } + }; + ((Iterable) null).forEach(_function); + """, "(null as Iterable).forEach[ it.subList(1,1) ]"); } @Test public void testExplicitReferenceToSynonym_02() throws Exception { - assertCompilesTo( - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final String[] it) {\n" + - " ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).size();\n" + - " }\n" + - "};\n" + - "((Iterable) null).forEach(_function);", - "(null as Iterable).forEach[ it.size ]"); + assertCompilesTo(""" + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final String[] it) { + ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).size(); + } + }; + ((Iterable) null).forEach(_function); + """, "(null as Iterable).forEach[ it.size ]"); } @Test public void testExplicitReferenceToSynonymWithPrimitives() throws Exception { - assertCompilesTo( - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final int[] it) {\n" + - " ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).subList(1, 1);\n" + - " }\n" + - "};\n" + - "((Iterable) null).forEach(_function);", - "(null as Iterable).forEach[ it.subList(1,1) ]"); + assertCompilesTo(""" + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final int[] it) { + ((java.util.List)org.eclipse.xtext.xbase.lib.Conversions.doWrapArray(it)).subList(1, 1); + } + }; + ((Iterable) null).forEach(_function); + """, "(null as Iterable).forEach[ it.subList(1,1) ]"); } @Test public void testExplicitReferenceToArray() throws Exception { - assertCompilesTo( - "final java.util.function.Consumer _function = new java.util.function.Consumer() {\n" + - " public void accept(final String[] it) {\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.println(Integer.valueOf(it.length));\n" + - " }\n" + - "};\n" + - "((Iterable) null).forEach(_function);", - "(null as Iterable).forEach[ println(it.length) ]"); + assertCompilesTo(""" + final java.util.function.Consumer _function = new java.util.function.Consumer() { + public void accept(final String[] it) { + org.eclipse.xtext.xbase.lib.InputOutput.println(Integer.valueOf(it.length)); + } + }; + ((Iterable) null).forEach(_function); + """, "(null as Iterable).forEach[ println(it.length) ]"); } @Test public void testFieldAccessDontGetAVariableDeclaration() throws Exception { - assertCompilesTo( - "return new testdata.Properties1().prop1;", - "new testdata.Properties1().prop1"); + assertCompilesTo("return new testdata.Properties1().prop1;", "new testdata.Properties1().prop1"); } - + @Test public void testNull() throws Exception { - assertCompilesToStatement( - "\n/* null */", - "null"); + assertCompilesToStatement(""" + + /* null */""", "null"); } @Test public void testStringLiteralInComment() throws Exception { - assertCompilesToStatement( - "\n/* \"* /\" */", - "'*/'"); + assertCompilesToStatement(""" + + /* "* /" */""", "'*/'"); } @Test public void testBlockHasNoSuperfluousBraces_01() throws Exception { - assertCompilesToStatement( - "\n" + - "try {\n" + - " new Object();\n" + - " new Object();\n" + - "} finally {\n" + - " new Object();\n" + - " new Object();\n" + - "}", - "try { new Object() new Object() } finally { new Object() new Object() }"); + assertCompilesToStatement(""" + + try { + new Object(); + new Object(); + } finally { + new Object(); + new Object(); + }""", "try { new Object() new Object() } finally { new Object() new Object() }"); } @Test public void testBlockHasNoSuperfluousBraces_02() throws Exception { - assertCompilesToStatement( - "\n" + - "if (true) {\n" + - " new Object();\n" + - " new Object();\n" + - "} else {\n" + - " new Object();\n" + - " new Object();\n" + - "}", - "if (true) { new Object() new Object() } else { new Object() new Object() }"); + assertCompilesToStatement(""" + + if (true) { + new Object(); + new Object(); + } else { + new Object(); + new Object(); + }""", "if (true) { new Object() new Object() } else { new Object() new Object() }"); } @Test public void testBlockHasNoSuperfluousBraces_03() throws Exception { - assertCompilesTo( - "\n" + - "final org.eclipse.xtext.xbase.lib.Procedures.Procedure1 _function = new org.eclipse.xtext.xbase.lib.Procedures.Procedure1() {\n" + - " public void apply(final Integer i) {\n" + - " new Object();\n" + - " new Object();\n" + - " }\n" + - "};\n" + - "org.eclipse.xtext.xbase.lib.Procedures.Procedure1 fun = _function;", - "{ var (int)=>void fun = [ int i | new Object() new Object() ] }"); + assertCompilesTo(""" + final org.eclipse.xtext.xbase.lib.Procedures.Procedure1 _function \ + = new org.eclipse.xtext.xbase.lib.Procedures.Procedure1() { + public void apply(final Integer i) { + new Object(); + new Object(); + } + }; + org.eclipse.xtext.xbase.lib.Procedures.Procedure1 fun = _function; + """, "{ var (int)=>void fun = [ int i | new Object() new Object() ] }"); } @Test public void testBlockHasNoSuperfluousBraces_04() throws Exception { - assertCompilesTo( - "\n" + - "Object it = new Object();\n" + - "it.notify();", - "{ var it = new Object() notify }"); + assertCompilesTo(""" + Object it = new Object(); + it.notify(); + """, "{ var it = new Object() notify }"); } @Test public void testBlockHasNoSuperfluousBraces_05() throws Exception { - assertCompilesTo( - "\n" + - "{\n" + - " Object it = new Object();\n" + - " it.notify();\n" + - "}\n" + - "{\n" + - " Object it = new Object();\n" + - " it.notify();\n" + - "}", - "{ { var it = new Object() notify } { var it = new Object() notify } }"); + assertCompilesTo(""" + { + Object it = new Object(); + it.notify(); + } + { + Object it = new Object(); + it.notify(); + } + """, "{ { var it = new Object() notify } { var it = new Object() notify } }"); } @Test public void testBlockHasNoSuperfluousBraces_06() throws Exception { - assertCompilesTo( - "\n" + - "Object it = new Object();\n" + - "{\n" + - " Object it_1 = new Object();\n" + - " it_1.notify();\n" + - "}\n" + - "it.notify();", - "{ var it = new Object() { var it = new Object() it.notify() } notify }"); + assertCompilesTo(""" + Object it = new Object(); + { + Object it_1 = new Object(); + it_1.notify(); + } + it.notify(); + """, "{ var it = new Object() { var it = new Object() it.notify() } notify }"); } @Test public void testBlockHasNoSuperfluousBraces_07() throws Exception { - assertCompilesTo( - "\n" + - "Object it = new Object();\n" + - "new Object().notify();\n" + - "it.notify();", - "{ var it = new Object() { new Object().notify() } notify }"); - } - - @Test - public void testNoUnneccessaryConversionStatement() throws Exception { - assertCompilesTo( - "String _xblockexpression = null;\n" + - "{\n" + - " final testdata.Properties1 x = new testdata.Properties1();\n" + - " _xblockexpression = x.toString();\n" + - "}\n" + - "return _xblockexpression;", - "{ val x = new testdata.Properties1() x.toString()}"); + assertCompilesTo(""" + Object it = new Object(); + new Object().notify(); + it.notify(); + """, "{ var it = new Object() { new Object().notify() } notify }"); + } + + @Test public void testNoUnneccessaryConversionStatement() throws Exception { + assertCompilesTo(""" + String _xblockexpression = null; + { + final testdata.Properties1 x = new testdata.Properties1(); + _xblockexpression = x.toString(); + } + return _xblockexpression; + """, "{ val x = new testdata.Properties1() x.toString()}"); } @Test public void testBlock() throws Exception { - assertCompilesTo( - "\nint _xblockexpression = (int) 0;\n" + - "{\n" + - " final java.util.ArrayList it = new java.util.ArrayList();\n" + - " _xblockexpression = it.size();\n" + - "}\n" + - "return _xblockexpression;" - , "{ val it = new java.util.ArrayList(); size;}"); + assertCompilesTo(""" + int _xblockexpression = (int) 0; + { + final java.util.ArrayList it = new java.util.ArrayList(); + _xblockexpression = it.size(); + } + return _xblockexpression; + """, "{ val it = new java.util.ArrayList(); size;}"); } @Test public void testIf() throws Exception { - assertCompilesTo( - "\nint _xifexpression = (int) 0;\n" + - "if (true) {\n" + - " _xifexpression = 42;\n" + - "} else {\n" + - " _xifexpression = 21;\n}\n" + - "return _xifexpression;" - , "if (true) 42 else 21"); + assertCompilesTo(""" + int _xifexpression = (int) 0; + if (true) { + _xifexpression = 42; + } else { + _xifexpression = 21; + } + return _xifexpression; + """, "if (true) 42 else 21"); } - + @Test public void testForEach_01() throws Exception { - assertCompilesTo( - "\njava.util.ArrayList _arrayList = new java.util.ArrayList();\n" + - "for (final String s : _arrayList) {\n" + - " s.length();\n" + - "}" - , "for (String s : new java.util.ArrayList())" + - "s.length"); + assertCompilesTo(""" + java.util.ArrayList _arrayList = new java.util.ArrayList(); + for (final String s : _arrayList) { + s.length(); + } + """, "for (String s : new java.util.ArrayList())" + "s.length"); } @Test public void testForEach_02() throws Exception { - assertCompilesTo( - "\njava.util.ArrayList _arrayList = new java.util.ArrayList();\n" + - "for (final String s : _arrayList) {\n" + - " s.length();\n" + - "}" - , "for (s : new java.util.ArrayList())" + - "s.length"); - } - - @Test - public void testForEach_03() throws Exception { - assertCompilesTo( - "\njava.util.ArrayList _arrayList = new java.util.ArrayList();\n" + - "for (final String s : _arrayList) {\n" + - " s.length();\n" + - "}" - , "for (String s : new java.util.ArrayList())" + - "s.length"); + assertCompilesTo(""" + java.util.ArrayList _arrayList = new java.util.ArrayList(); + for (final String s : _arrayList) { + s.length(); + } + """, "for (s : new java.util.ArrayList())" + "s.length"); + } + + @Test public void testForEach_03() throws Exception { + assertCompilesTo(""" + java.util.ArrayList _arrayList = new java.util.ArrayList(); + for (final String s : _arrayList) { + s.length(); + } + """, "for (String s : new java.util.ArrayList())" + "s.length"); } @Test public void testFeatureCall() throws Exception { - assertCompilesTo( - "\n" + - "java.util.ArrayList _xblockexpression = null;\n" + - "{\n" + - " final java.util.ArrayList x = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(\"foo\");\n" + - " _xblockexpression = x;\n" + - "}\n" + - "return _xblockexpression;", - "{val x = newArrayList('foo')\n" + - "x}"); + assertCompilesTo(""" + java.util.ArrayList _xblockexpression = null; + { + final java.util.ArrayList x = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList("foo"); + _xblockexpression = x; + } + return _xblockexpression; + """, """ + {val x = newArrayList('foo') + x} + """); } @Test public void testNullSafeFeatureCall_01() throws Exception { - assertCompilesTo( - "\n" + - "Object _object = new Object();\n" + - "if (_object!=null) {\n" + - " _object.notify();\n" + - "}", - "new Object()?.notify"); + assertCompilesTo(""" + Object _object = new Object(); + if (_object!=null) { + _object.notify(); + } + """, "new Object()?.notify"); } @Test public void testNullSafeFeatureCall_02() throws Exception { - assertCompilesTo( - "\n" + - "String _string = new Object().toString();\n" + - "if (_string!=null) {\n" + - " _string.notify();\n" + - "}", - "new Object().toString?.notify"); + assertCompilesTo(""" + String _string = new Object().toString(); + if (_string!=null) { + _string.notify(); + } + """, "new Object().toString?.notify"); } @Test public void testNullSafeFeatureCall_03() throws Exception { - assertCompilesTo( - "\n" + - "Object _object = new Object();\n" + - "String _string = null;\n" + - "if (_object!=null) {\n" + - " _string=_object.toString();\n" + - "}\n" + - "if (_string!=null) {\n" + - " _string.notify();\n" + - "}", - "new Object()?.toString?.notify"); + assertCompilesTo(""" + Object _object = new Object(); + String _string = null; + if (_object!=null) { + _string=_object.toString(); + } + if (_string!=null) { + _string.notify(); + } + """, "new Object()?.toString?.notify"); } @Test public void testNullSafeFeatureCall_04() throws Exception { - assertCompilesTo( - "\n" + - "String _string = new String();\n" + - "String _substring = null;\n" + - "if (_string!=null) {\n" + - " _substring=_string.substring(1);\n" + - "}\n" + - "int _length = 0;\n" + - "if (_substring!=null) {\n" + - " _length=_substring.length();\n" + - "}\n" + - "return _length;", - "new String()?.substring(1)?.length"); + assertCompilesTo(""" + String _string = new String(); + String _substring = null; + if (_string!=null) { + _substring=_string.substring(1); + } + int _length = 0; + if (_substring!=null) { + _length=_substring.length(); + } + return _length; + """, "new String()?.substring(1)?.length"); } @Test public void testNullSafeFeatureCall_05() throws Exception { - assertCompilesTo( - "\n" + - "int _xblockexpression = (int) 0;\n" + - "{\n" + - " int x = 0;\n" + - " String _string = new String();\n" + - " if (_string!=null) {\n" + - " _string.substring(x = 2);\n" + - " }\n" + - " _xblockexpression = x;\n" + - "}\n" + - "return _xblockexpression;", - "{var x = 0; new String()?.substring(x=2); x}"); + assertCompilesTo(""" + int _xblockexpression = (int) 0; + { + int x = 0; + String _string = new String(); + if (_string!=null) { + _string.substring(x = 2); + } + _xblockexpression = x; + } + return _xblockexpression; + """, "{var x = 0; new String()?.substring(x=2); x}"); } @Test public void testNullSafeFeatureCall_06() throws Exception { - assertCompilesTo( - "\n" + - "String s = new String();\n" + - "String _string = null;\n" + - "if (s!=null) {\n" + - " _string=s.toString();\n" + - "}\n" + - "return _string;", - "{ var s = new String; return s?.toString() }"); + assertCompilesTo(""" + String s = new String(); + String _string = null; + if (s!=null) { + _string=s.toString(); + } + return _string; + """, "{ var s = new String; return s?.toString() }"); } @Test public void testInline_01() throws Exception { - assertCompilesTo( - "\n" + - "String _string = (\"a\" + \"b\").toString();\n" + - "return _string;", - "('a'+'b').toString"); + assertCompilesTo(""" + String _string = ("a" + "b").toString(); + return _string; + """, "('a'+'b').toString"); } @Test public void testInline_02() throws Exception { - assertCompilesTo( - "\n" + - "boolean _xifexpression = false;\n" + - "if ((!true)) {\n" + - " _xifexpression = (true == false);\n" + - "} else {\n" + - " _xifexpression = (true != false);\n" + - "}\n" + - "return _xifexpression;", - "if(!true) true==false else true!=false"); + assertCompilesTo(""" + boolean _xifexpression = false; + if ((!true)) { + _xifexpression = (true == false); + } else { + _xifexpression = (true != false); + } + return _xifexpression; + """, "if(!true) true==false else true!=false"); } @Test public void testSwitch_01() throws Exception { - assertCompilesTo("String _xblockexpression = null;\n" + - "{\n" + - " final Object o = \"foo\";\n" + - " boolean _matched = false;\n" + - " if (o instanceof String) {\n" + - " _matched=true;\n" + - " \"\".toString();\n" + - " }\n" + - " String _switchResult_1 = null;\n" + - " boolean _matched_1 = false;\n" + - " if (o instanceof String) {\n" + - " _matched_1=true;\n" + - " _switchResult_1 = \"\";\n" + - " }\n" + - " _xblockexpression = _switchResult_1;\n" + - "}\n" + - "return _xblockexpression;", - "{" + - " val Object o = 'foo'" + - " switch(o) {\n" + - " String: \"\".toString\n" + - " }\n" + - " switch(o) {\n" + - " String: \"\"\n" + - " }" + - "}"); - } - - @Test - public void testSwitchTypeGuards_01() throws Exception { - assertCompilesTo( - "String _switchResult = null;\n" + - "final CharSequence x = ((CharSequence) \"foo\");\n" + - "boolean _matched = false;\n" + - "if (x instanceof String) {\n" + - " _matched=true;\n" + - " String _substring = ((String)x).substring(3);\n" + - " _switchResult = (_substring + ((String)x));\n" + - "}\n" + - "if (!_matched) {\n" + - " if (x instanceof Comparable) {\n" + - " _matched=true;\n" + - " int _compareTo = ((Comparable)x).compareTo(\"jho\");\n" + - " String _plus = (\"\" + Integer.valueOf(_compareTo));\n" + - " String _string = x.toString();\n" + - " _switchResult = (_plus + _string);\n" + - " }\n" + - "}\n" + - "return _switchResult;" - , - "switch x : 'foo' as CharSequence {" + - " String : x.substring(3) + x " + - " Comparable : '' + x.compareTo('jho') + x.toString" + - "}"); + assertCompilesTo(""" + String _xblockexpression = null; + { + final Object o = "foo"; + boolean _matched = false; + if (o instanceof String) { + _matched=true; + "".toString(); + } + String _switchResult_1 = null; + boolean _matched_1 = false; + if (o instanceof String) { + _matched_1=true; + _switchResult_1 = ""; + } + _xblockexpression = _switchResult_1; + } + return _xblockexpression; + """, """ + { + val Object o = 'foo' + switch(o) { + String: "".toString + } + switch(o) { + String: "" + } + } + """); + } + + @Test public void testSwitchTypeGuards_01() throws Exception { + assertCompilesTo(""" + String _switchResult = null; + final CharSequence x = ((CharSequence) "foo"); + boolean _matched = false; + if (x instanceof String) { + _matched=true; + String _substring = ((String)x).substring(3); + _switchResult = (_substring + ((String)x)); + } + if (!_matched) { + if (x instanceof Comparable) { + _matched=true; + int _compareTo = ((Comparable)x).compareTo("jho"); + String _plus = ("" + Integer.valueOf(_compareTo)); + String _string = x.toString(); + _switchResult = (_plus + _string); + } + } + return _switchResult; + """, """ + switch x : 'foo' as CharSequence { + String : x.substring(3) + x + Comparable : '' + x.compareTo('jho') + x.toString + } + """); } @Test public void testSwitchTypeGuards_02() throws Exception { - assertCompilesTo( - "String _switchResult = null;\n" + - "final CharSequence x = ((CharSequence) \"foo\");\n" + - "boolean _matched = false;\n" + - "if (x instanceof String) {\n" + - " _matched=true;\n" + - " String _substring = ((String)x).substring(3);\n" + - " _switchResult = (_substring + ((String)x));\n" + - "}\n" + - "if (!_matched) {\n" + - " if (x instanceof Comparable) {\n" + - " _matched=true;\n" + - " int _compareTo = ((Comparable)x).compareTo(\"jho\");\n" + - " String _plus = (\"\" + Integer.valueOf(_compareTo));\n" + - " int _length = x.length();\n" + - " _switchResult = (_plus + Integer.valueOf(_length));\n" + - " }\n" + - "}\n" + - "return _switchResult;" - , - "switch x : 'foo' as CharSequence {" + - " String : x.substring(3) + x " + - " Comparable : '' + x.compareTo('jho') + x.length" + - "}"); + assertCompilesTo(""" + String _switchResult = null; + final CharSequence x = ((CharSequence) "foo"); + boolean _matched = false; + if (x instanceof String) { + _matched=true; + String _substring = ((String)x).substring(3); + _switchResult = (_substring + ((String)x)); + } + if (!_matched) { + if (x instanceof Comparable) { + _matched=true; + int _compareTo = ((Comparable)x).compareTo("jho"); + String _plus = ("" + Integer.valueOf(_compareTo)); + int _length = x.length(); + _switchResult = (_plus + Integer.valueOf(_length)); + } + } + return _switchResult; + """, """ + switch x : 'foo' as CharSequence { + String : x.substring(3) + x + Comparable : '' + x.compareTo('jho') + x.length + } + """); } @Test public void testNoExceptionWithUnresolveableTypes() throws Exception { - assertCompilesTo( - // Occurrences of "void" here are inconsistent with the serialized type of the _switchResult. - // In practice, the ErrorTreeAppendable will take care of some of these. Most importantly - // we don't throw an Exception. - "java.util.List _switchResult = null;\n" - + "Object _instance = new Object(void.class, _Unresolvable);\n" - + "final Object x = _instance;\n" - + "boolean _matched = false;\n" - + "if (x instanceof void) {\n" - + " _matched=true;\n" - + " _switchResult = java.util.Arrays.asList(x);\n" - + "}\n" - + "return _switchResult;" - , - "switch x : new Unresolveable(typeof(Unresolvable), Unresolvable) {" + - " Unresolvable : java.util.Arrays.asList(x) " + - "}", - false); + assertCompilesTo(""" + java.util.List _switchResult = null; + Object _instance = new Object(void.class, _Unresolvable); + final Object x = _instance; + boolean _matched = false; + if (x instanceof void) { + _matched=true; + _switchResult = java.util.Arrays.asList(x); + } + return _switchResult; + """, """ + switch x : new Unresolveable(typeof(Unresolvable), Unresolvable) { + Unresolvable : java.util.Arrays.asList(x) + } + """, false); } - + /* * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=371306 */ @Test public void testSwitchEarlyExits() throws Exception { - assertCompilesTo( - "final boolean _switchValue = true;\n" + - "boolean _matched = false;\n" + - "if (true) {\n" + - " _matched=true;\n" + - " return 1;\n" + - "}\n" + - "if (!_matched) {\n" + - " if (false) {\n" + - " _matched=true;\n" + - " return (-1);\n" + - " }\n" + - "}\n" + - "return 0;" - , - "switch true {" + - " case true: return 1" + - " case false: return -1" + - " default: return 0" + - "}"); + assertCompilesTo(""" + final boolean _switchValue = true; + boolean _matched = false; + if (true) { + _matched=true; + return 1; + } + if (!_matched) { + if (false) { + _matched=true; + return (-1); + } + } + return 0; + """, """ + switch true { + case true: return 1 + case false: return -1 + default: return 0 + } + """); } - + /* * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=371306 */ @Test public void testSwitchEarlyExits_1() throws Exception { - assertCompilesTo( - "int _switchResult = (int) 0;\n" + - "final boolean _switchValue = true;\n" + - "boolean _matched = false;\n" + - "if (true) {\n" + - " _matched=true;\n" + - " _switchResult = 1;\n" + - "}\n" + - "if (!_matched) {\n" + - " if (false) {\n" + - " _matched=true;\n" + - " return (-1);\n" + - " }\n" + - "}\n" + - "if (!_matched) {\n" + - " return 0;\n" + - "}\n" + - "return _switchResult;" - , - "switch true {" + - " case true: 1" + - " case false: return -1" + - " default: return 0" + - "}"); + assertCompilesTo(""" + int _switchResult = (int) 0; + final boolean _switchValue = true; + boolean _matched = false; + if (true) { + _matched=true; + _switchResult = 1; + } + if (!_matched) { + if (false) { + _matched=true; + return (-1); + } + } + if (!_matched) { + return 0; + } + return _switchResult; + """, """ + switch true { + case true: 1 + case false: return -1 + default: return 0 + } + """); } @Test public void testNewLines_withinline() throws Exception { - assertCompilesTo( - "java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(\"foo\");\n" + - "boolean _add = _newArrayList.add(\n" + - " \"bar\");\n" + - "return _add;", - "newArrayList('foo') += \n'bar'"); + assertCompilesTo(""" + java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList("foo"); + boolean _add = _newArrayList.add( + "bar"); + return _add; + """, """ + newArrayList('foo') += + 'bar' + """); } @Test public void testNewLines_withinline1() throws Exception { - assertCompilesTo( - "java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(\n" + - " (\"foo\" + \n" + - " \"bar\"));\n" + - "return _newArrayList;", - "newArrayList(\n'foo' + \n'bar')"); + assertCompilesTo(""" + java.util.ArrayList _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList( + ("foo" +\s + "bar")); + return _newArrayList; + """, """ + newArrayList( + 'foo' + + 'bar') + """); } @Test public void testNewLines_forExtensionMethod() throws Exception { - assertCompilesTo( - "String _firstUpper = org.eclipse.xtext.xbase.lib.StringExtensions.toFirstUpper(\"foo\");\n" + - "return _firstUpper;", - "\n'foo'.toFirstUpper"); + assertCompilesTo(""" + String _firstUpper = org.eclipse.xtext.xbase.lib.StringExtensions.toFirstUpper("foo"); + return _firstUpper; + """, "'foo'.toFirstUpper"); } @Test public void testSetLiteral() throws Exception { - assertCompilesTo( - "return java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(\"foo\"));\n", - "#{'foo'}"); + assertCompilesTo(""" + return java.util.Collections.unmodifiableSet(\ + org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet("foo")); + """, "#{'foo'}"); } @Test public void testMapLiteral() throws Exception { - assertCompilesTo( - "org.eclipse.xtext.xbase.lib.Pair _mappedTo = org.eclipse.xtext.xbase.lib.Pair.of(\"foo\", Integer.valueOf(42));\n"+ - "return java.util.Collections.unmodifiableMap(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashMap(_mappedTo));\n", - "#{'foo'->42}"); + assertCompilesTo(""" + org.eclipse.xtext.xbase.lib.Pair _mappedTo = \ + org.eclipse.xtext.xbase.lib.Pair.of("foo", Integer.valueOf(42)); + return java.util.Collections.unmodifiableMap(\ + org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashMap(_mappedTo)); + """, "#{'foo'->42}"); } @Test public void testListLiteralAsList() throws Exception { - assertCompilesTo( - "return java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(\"foo\"));\n", - "#['foo']"); + assertCompilesTo(""" + return java.util.Collections.unmodifiableList(\ + org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList("foo")); + """, "#['foo']"); } - + @Test public void testListLiteralAsArray() throws Exception { - assertCompilesTo( - "final String[] x = { \"foo\" };", - "{val String[] x = #['foo']}"); + assertCompilesTo(""" + final String[] x = { "foo" }; + """, "{val String[] x = #['foo']}"); } - + @Test public void testExceptionOnClosure() throws Exception { - assertCompilesTo( - "final java.beans.VetoableChangeListener _function = new java.beans.VetoableChangeListener() {\n" + - " public void vetoableChange(final java.beans.PropertyChangeEvent it) throws java.beans.PropertyVetoException {\n" + - " }\n" + - "};\n" + - "final java.beans.VetoableChangeListener x = _function;", - "{val java.beans.VetoableChangeListener x = []}"); + assertCompilesTo(""" + final java.beans.VetoableChangeListener _function = new java.beans.VetoableChangeListener() { + public void vetoableChange(final java.beans.PropertyChangeEvent it) throws java.beans.PropertyVetoException { + } + }; + final java.beans.VetoableChangeListener x = _function; + """, "{val java.beans.VetoableChangeListener x = []}"); } - + @Test public void testArrayLiteralInForLoop() throws Exception { - assertCompilesTo( - "for (final String i : new String[] { \"a\", \"b\", \"c\" }) {\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.println(i);\n}", - "for (String i : #['a','b','c']) { println(i) }"); + assertCompilesTo(""" + for (final String i : new String[] { "a", "b", "c" }) { + org.eclipse.xtext.xbase.lib.InputOutput.println(i); + } + """, "for (String i : #['a','b','c']) { println(i) }"); } - + @Test public void testArrayLiteralInForLoop2() throws Exception { - assertCompilesTo( - "for (final String i : java.util.Collections.unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(\"a\", \"b\", \"c\"))) {\n" + - " org.eclipse.xtext.xbase.lib.InputOutput.println(i);\n}", - "for (i : #['a','b','c']) { println(i) }"); + assertCompilesTo(""" + for (final String i : java.util.Collections.unmodifiableList(\ + org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList("a", "b", "c"))) { + org.eclipse.xtext.xbase.lib.InputOutput.println(i); + } + """, "for (i : #['a','b','c']) { println(i) }"); } - - @Test - public void testArrayLiteralInForLoop3() throws Exception { - assertCompilesTo( - "org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" - + "for (final org.eclipse.xtext.xbase.lib.IntegerRange i : " - + "java.util.Collections.unmodifiableList(" - + "org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(_upTo))) {\n}", - "for (i : #[1..2]) {}"); + + @Test public void testArrayLiteralInForLoop3() throws Exception { + assertCompilesTo(""" + org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2); + for (final org.eclipse.xtext.xbase.lib.IntegerRange i : \ + java.util.Collections.unmodifiableList(\ + org.eclipse.xtext.xbase.lib.CollectionLiterals.newArrayList(_upTo))) { + } + """, "for (i : #[1..2]) {}"); } - + @Test public void testArrayLiteralInForLoop4() throws Exception { - assertCompilesTo("org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" - + "for (final org.eclipse.xtext.xbase.lib.IntegerRange i : new org.eclipse.xtext.xbase.lib.IntegerRange[] { _upTo }) {\n" - + "}", "for (IntegerRange i : #[1..2]) {}"); - } - - @Test - public void testArrayLiteralInForLoop5() throws Exception { - assertCompilesTo("org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" - + "for (final Iterable i : new Iterable[] { _upTo }) {\n" + "}", "for (Iterable i : #[1..2]) {}"); - } - - @Test - public void testSetLiteralInForLoop1() throws Exception { - assertCompilesTo( - "org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" - + "for (final org.eclipse.xtext.xbase.lib.IntegerRange i : java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(_upTo))) {\n}", - "for (i : #{1..2}) {}"); - } - - @Test - public void testSetLiteralInForLoop2() throws Exception { - assertCompilesTo("org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" - + "for (final org.eclipse.xtext.xbase.lib.IntegerRange i : java.util.Collections.unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(_upTo))) {\n" - + "}", "for (IntegerRange i : #{1..2}) {}"); - } - - @Test - public void testSetLiteralInForLoop3() throws Exception { - assertCompilesTo("org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" - + "for (final Iterable i : java.util.Collections.>unmodifiableSet(org.eclipse.xtext.xbase.lib.CollectionLiterals.>newHashSet(_upTo))) {\n" - + "}", "for (Iterable i : #{1..2}) {}"); - } - - @Test - public void testBug472265_01() throws Exception { - assertCompilesTo("final closures.IAcceptors.IAcceptor _function = new closures.IAcceptors.IAcceptor() {\n" - + " public void doSth(final String x) {\n" - + " }\n" - + "};\n" - + "closures.IAcceptors.IAcceptor a = _function;", - "{var closures.IAcceptors.IAcceptor a = [x|]}"); - } - - @Test - public void testBug472265_02() throws Exception { - assertCompilesTo("final closures.IAcceptors.IAcceptor2 _function = new closures.IAcceptors.IAcceptor2() {\n" - + " public void doSth(final String[] x) {\n" - + " }\n" - + "};\n" - + "closures.IAcceptors.IAcceptor2 a = _function;", - "{var closures.IAcceptors.IAcceptor2 a = [x|]}"); - } - - @Test - public void testBug472265_03() throws Exception { - assertCompilesTo("final closures.IAcceptors.IAcceptor3 _function = new closures.IAcceptors.IAcceptor3() {\n" - + " public void doSth(final String... x) {\n" - + " }\n" - + "};\n" - + "closures.IAcceptors.IAcceptor3 a = _function;", - "{var closures.IAcceptors.IAcceptor3 a = [x|]}"); - } - - @Test - public void testBug472265_04() throws Exception { - assertCompilesTo("final closures.IAcceptors.IAcceptor4 _function = new closures.IAcceptors.IAcceptor4() {\n" - + " public void doSth(final String x, final String[] y) {\n" - + " }\n" - + "};\n" - + "closures.IAcceptors.IAcceptor4 a = _function;", - "{var closures.IAcceptors.IAcceptor4 a = [x,y|]}"); - } - - @Test - public void testBug472265_05() throws Exception { - assertCompilesTo("final closures.IAcceptors.IAcceptor5 _function = new closures.IAcceptors.IAcceptor5() {\n" - + " public void doSth(final String x, final String... y) {\n" - + " }\n" - + "};\n" - + "closures.IAcceptors.IAcceptor5 a = _function;", - "{var closures.IAcceptors.IAcceptor5 a = [x,y|]}"); - } - - @Test - public void testBug472265_06() throws Exception { - assertCompilesTo("final closures.IAcceptors.IAcceptor6 _function = new closures.IAcceptors.IAcceptor6() {\n" - + " public void doSth(final String[] x, final String[] y) {\n" - + " }\n" - + "};\n" - + "closures.IAcceptors.IAcceptor6 a = _function;", - "{var closures.IAcceptors.IAcceptor6 a = [x,y|]}"); - } - - @Test - public void testBug472265_07() throws Exception { - assertCompilesTo("final closures.IAcceptors.IAcceptor7 _function = new closures.IAcceptors.IAcceptor7() {\n" - + " public void doSth(final String[] x, final String... y) {\n" - + " }\n" - + "};\n" - + "closures.IAcceptors.IAcceptor7 a = _function;", - "{var closures.IAcceptors.IAcceptor7 a = [x,y|]}"); - } - - @Test - public void testBug472265_01_lamda() throws Exception { + assertCompilesTo(""" + org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2); + for (final org.eclipse.xtext.xbase.lib.IntegerRange i : new org.eclipse.xtext.xbase.lib.IntegerRange[] { _upTo }) { + } + """, "for (IntegerRange i : #[1..2]) {}"); + } + + @Test public void testArrayLiteralInForLoop5() throws Exception { + assertCompilesTo(""" + org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2); + for (final Iterable i : new Iterable[] { _upTo }) { + } + """, "for (Iterable i : #[1..2]) {}"); + } + + @Test public void testSetLiteralInForLoop1() throws Exception { + assertCompilesTo(""" + org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2); + for (final org.eclipse.xtext.xbase.lib.IntegerRange i : \ + java.util.Collections.unmodifiableSet(\ + org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(_upTo))) { + } + """, "for (i : #{1..2}) {}"); + } + + @Test public void testSetLiteralInForLoop2() throws Exception { + assertCompilesTo(""" + org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2); + for (final org.eclipse.xtext.xbase.lib.IntegerRange i : \ + java.util.Collections.unmodifiableSet(\ + org.eclipse.xtext.xbase.lib.CollectionLiterals.newHashSet(_upTo))) { + } + """, "for (IntegerRange i : #{1..2}) {}"); + } + + @Test public void testSetLiteralInForLoop3() throws Exception { + assertCompilesTo(""" + org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2); + for (final Iterable i : java.util.Collections.>unmodifiableSet(\ + org.eclipse.xtext.xbase.lib.CollectionLiterals.>newHashSet(_upTo))) { + } + """, "for (Iterable i : #{1..2}) {}"); + } + + @Test public void testBug472265_01() throws Exception { + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor _function = new closures.IAcceptors.IAcceptor() { + public void doSth(final String x) { + } + }; + closures.IAcceptors.IAcceptor a = _function; + """, "{var closures.IAcceptors.IAcceptor a = [x|]}"); + } + + @Test public void testBug472265_02() throws Exception { + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor2 _function = new closures.IAcceptors.IAcceptor2() { + public void doSth(final String[] x) { + } + }; + closures.IAcceptors.IAcceptor2 a = _function; + """, "{var closures.IAcceptors.IAcceptor2 a = [x|]}"); + } + + @Test public void testBug472265_03() throws Exception { + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor3 _function = new closures.IAcceptors.IAcceptor3() { + public void doSth(final String... x) { + } + }; + closures.IAcceptors.IAcceptor3 a = _function; + """, "{var closures.IAcceptors.IAcceptor3 a = [x|]}"); + } + + @Test public void testBug472265_04() throws Exception { + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor4 _function = new closures.IAcceptors.IAcceptor4() { + public void doSth(final String x, final String[] y) { + } + }; + closures.IAcceptors.IAcceptor4 a = _function; + """, "{var closures.IAcceptors.IAcceptor4 a = [x,y|]}"); + } + + @Test public void testBug472265_05() throws Exception { + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor5 _function = new closures.IAcceptors.IAcceptor5() { + public void doSth(final String x, final String... y) { + } + }; + closures.IAcceptors.IAcceptor5 a = _function; + """, "{var closures.IAcceptors.IAcceptor5 a = [x,y|]}"); + } + + @Test public void testBug472265_06() throws Exception { + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor6 _function = new closures.IAcceptors.IAcceptor6() { + public void doSth(final String[] x, final String[] y) { + } + }; + closures.IAcceptors.IAcceptor6 a = _function; + """, "{var closures.IAcceptors.IAcceptor6 a = [x,y|]}"); + } + + @Test public void testBug472265_07() throws Exception { + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor7 _function = new closures.IAcceptors.IAcceptor7() { + public void doSth(final String[] x, final String... y) { + } + }; + closures.IAcceptors.IAcceptor7 a = _function; + """, "{var closures.IAcceptors.IAcceptor7 a = [x,y|]}"); + } + + @Test public void testBug472265_01_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); - assertCompilesTo("final closures.IAcceptors.IAcceptor _function = (String x) -> {\n" - + "};\n" - + "closures.IAcceptors.IAcceptor a = _function;", - "{var closures.IAcceptors.IAcceptor a = [x|]}", - generatorConfig); + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor _function = (String x) -> { + }; + closures.IAcceptors.IAcceptor a = _function; + """, "{var closures.IAcceptors.IAcceptor a = [x|]}", generatorConfig); } - @Test - public void testBug472265_02_lamda() throws Exception { + @Test public void testBug472265_02_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); - assertCompilesTo("final closures.IAcceptors.IAcceptor2 _function = (String[] x) -> {\n" - + "};\n" - + "closures.IAcceptors.IAcceptor2 a = _function;", - "{var closures.IAcceptors.IAcceptor2 a = [x|]}", - generatorConfig); + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor2 _function = (String[] x) -> { + }; + closures.IAcceptors.IAcceptor2 a = _function; + """, "{var closures.IAcceptors.IAcceptor2 a = [x|]}", generatorConfig); } - @Test - public void testBug472265_03_lamda() throws Exception { + @Test public void testBug472265_03_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); - assertCompilesTo("final closures.IAcceptors.IAcceptor3 _function = (String... x) -> {\n" - + "};\n" - + "closures.IAcceptors.IAcceptor3 a = _function;", - "{var closures.IAcceptors.IAcceptor3 a = [x|]}", - generatorConfig); + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor3 _function = (String... x) -> { + }; + closures.IAcceptors.IAcceptor3 a = _function; + """, "{var closures.IAcceptors.IAcceptor3 a = [x|]}", generatorConfig); } - @Test - public void testBug472265_04_lamda() throws Exception { + @Test public void testBug472265_04_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); - assertCompilesTo("final closures.IAcceptors.IAcceptor4 _function = (String x, String[] y) -> {\n" - + "};\n" - + "closures.IAcceptors.IAcceptor4 a = _function;", - "{var closures.IAcceptors.IAcceptor4 a = [x,y|]}", - generatorConfig); + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor4 _function = (String x, String[] y) -> { + }; + closures.IAcceptors.IAcceptor4 a = _function; + """, "{var closures.IAcceptors.IAcceptor4 a = [x,y|]}", generatorConfig); } - @Test - public void testBug472265_05_lamda() throws Exception { + @Test public void testBug472265_05_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); - assertCompilesTo("final closures.IAcceptors.IAcceptor5 _function = (String x, String... y) -> {\n" - + "};\n" - + "closures.IAcceptors.IAcceptor5 a = _function;", - "{var closures.IAcceptors.IAcceptor5 a = [x,y|]}", - generatorConfig); + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor5 _function = (String x, String... y) -> { + }; + closures.IAcceptors.IAcceptor5 a = _function; + """, "{var closures.IAcceptors.IAcceptor5 a = [x,y|]}", generatorConfig); } - @Test - public void testBug472265_06_lamda() throws Exception { + @Test public void testBug472265_06_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); - assertCompilesTo("final closures.IAcceptors.IAcceptor6 _function = (String[] x, String[] y) -> {\n" - + "};\n" - + "closures.IAcceptors.IAcceptor6 a = _function;", - "{var closures.IAcceptors.IAcceptor6 a = [x,y|]}", - generatorConfig); + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor6 _function = (String[] x, String[] y) -> { + }; + closures.IAcceptors.IAcceptor6 a = _function; + """, "{var closures.IAcceptors.IAcceptor6 a = [x,y|]}", generatorConfig); } - @Test - public void testBug472265_07_lamda() throws Exception { + @Test public void testBug472265_07_lamda() throws Exception { GeneratorConfig generatorConfig = generatorConfigProvider.get(null); generatorConfig.setJavaSourceVersion(JavaVersion.JAVA8); - assertCompilesTo("final closures.IAcceptors.IAcceptor7 _function = (String[] x, String... y) -> {\n" - + "};\n" - + "closures.IAcceptors.IAcceptor7 a = _function;", - "{var closures.IAcceptors.IAcceptor7 a = [x,y|]}", - generatorConfig); + assertCompilesTo(""" + final closures.IAcceptors.IAcceptor7 _function = (String[] x, String... y) -> { + }; + closures.IAcceptors.IAcceptor7 a = _function; + """, "{var closures.IAcceptors.IAcceptor7 a = [x,y|]}", generatorConfig); } }