|
| 1 | +/* |
| 2 | + * Copyright 2009-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.eclipse.jdt.groovy.core.tests.xform; |
| 17 | + |
| 18 | +import org.eclipse.jdt.groovy.core.tests.basic.GroovyCompilerTestSuite; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +/** |
| 22 | + * Test cases for {@link groovy.transform.AutoImplement}. |
| 23 | + */ |
| 24 | +public final class AutoImplementTests extends GroovyCompilerTestSuite { |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testAutoImplement1() { |
| 28 | + //@formatter:off |
| 29 | + String[] sources = { |
| 30 | + "Main.groovy", |
| 31 | + "@groovy.transform.AutoImplement\n" + |
| 32 | + "class Foo implements Iterator<String> { }\n" + |
| 33 | + "print new Foo().hasNext()\n", |
| 34 | + }; |
| 35 | + //@formatter:on |
| 36 | + |
| 37 | + runConformTest(sources, "false"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testAutoImplement2() { |
| 42 | + //@formatter:off |
| 43 | + String[] sources = { |
| 44 | + "Main.groovy", |
| 45 | + "@groovy.transform.AutoImplement(exception=UnsupportedOperationException)\n" + |
| 46 | + "class Foo implements Iterator<String> { }\n" + |
| 47 | + "try { new Foo().hasNext() } catch (UnsupportedOperationException e) { print e }\n", |
| 48 | + }; |
| 49 | + //@formatter:on |
| 50 | + |
| 51 | + runConformTest(sources, "java.lang.UnsupportedOperationException"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testAutoImplement3() { |
| 56 | + //@formatter:off |
| 57 | + String[] sources = { |
| 58 | + "Main.groovy", |
| 59 | + "@groovy.transform.AutoImplement(exception=UnsupportedOperationException, message='not supported by Foo')\n" + |
| 60 | + "class Foo implements Iterator<String> { }\n" + |
| 61 | + "try { new Foo().hasNext() } catch (UnsupportedOperationException e) { print e }\n", |
| 62 | + }; |
| 63 | + //@formatter:on |
| 64 | + |
| 65 | + runConformTest(sources, "java.lang.UnsupportedOperationException: not supported by Foo"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testAutoImplement4() { |
| 70 | + //@formatter:off |
| 71 | + String[] sources = { |
| 72 | + "Main.groovy", |
| 73 | + "@groovy.transform.AutoImplement(code={ throw new IllegalStateException() })\n" + |
| 74 | + "class Foo implements Iterator<String> { }\n" + |
| 75 | + "try { new Foo().hasNext() } catch (IllegalStateException e) { print e }\n", |
| 76 | + }; |
| 77 | + //@formatter:on |
| 78 | + |
| 79 | + runConformTest(sources, "java.lang.IllegalStateException"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testAutoImplement5() { |
| 84 | + //@formatter:off |
| 85 | + String[] sources = { |
| 86 | + "Main.java", |
| 87 | + "public class Main {\n" + |
| 88 | + " public static void main(String[] args) {\n" + |
| 89 | + " Pogo pogo = new Pogo();\n" + |
| 90 | + " System.out.print(pogo.hasNext());\n" + |
| 91 | + " }\n" + |
| 92 | + "}\n", |
| 93 | + |
| 94 | + "Pogo.groovy", |
| 95 | + "@groovy.transform.AutoImplement\n" + |
| 96 | + "class Pogo implements Iterator<String> { }\n", |
| 97 | + }; |
| 98 | + //@formatter:on |
| 99 | + |
| 100 | + runConformTest(sources, "false"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test // GROOVY-8270 |
| 104 | + public void testAutoImplement6() { |
| 105 | + //@formatter:off |
| 106 | + String[] sources = { |
| 107 | + "Main.groovy", |
| 108 | + "@groovy.transform.AutoImplement\n" + |
| 109 | + "class Foo implements Comparator<String> { }\n" + |
| 110 | + "print new Foo().compare('bar', 'baz')\n", |
| 111 | + }; |
| 112 | + //@formatter:on |
| 113 | + |
| 114 | + runConformTest(sources, "0"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test // GROOVY-9816 |
| 118 | + public void testAutoImplement7() { |
| 119 | + //@formatter:off |
| 120 | + String[] sources = { |
| 121 | + "Main.groovy", |
| 122 | + "interface Bar {\n" + |
| 123 | + " def getBaz(); void setBaz(baz)\n" + |
| 124 | + "}\n" + |
| 125 | + "@groovy.transform.AutoImplement\n" + |
| 126 | + "class Foo implements Bar {\n" + |
| 127 | + " def baz\n" + |
| 128 | + "}\n" + |
| 129 | + "def foo = new Foo(baz: 123)\n" + |
| 130 | + "print foo.baz\n", |
| 131 | + }; |
| 132 | + //@formatter:on |
| 133 | + |
| 134 | + runConformTest(sources, "123"); |
| 135 | + } |
| 136 | + |
| 137 | + @Test // GROOVY-9816 |
| 138 | + public void testAutoImplement8() { |
| 139 | + //@formatter:off |
| 140 | + String[] sources = { |
| 141 | + "Main.groovy", |
| 142 | + "interface Bar {\n" + |
| 143 | + " boolean getBaz(); boolean isBaz()\n" + |
| 144 | + "}\n" + |
| 145 | + "@groovy.transform.AutoImplement\n" + |
| 146 | + "class Foo implements Bar {\n" + |
| 147 | + " boolean baz\n" + |
| 148 | + "}\n" + |
| 149 | + "def foo = new Foo(baz: true)\n" + |
| 150 | + "print foo.getBaz()\n" + |
| 151 | + "print foo.isBaz()\n" + |
| 152 | + "print foo.baz\n", |
| 153 | + }; |
| 154 | + //@formatter:on |
| 155 | + |
| 156 | + runConformTest(sources, "truetruetrue"); |
| 157 | + } |
| 158 | +} |
0 commit comments