|
| 1 | +package org.hamcrest.generator; |
| 2 | + |
| 3 | +import junit.framework.TestCase; |
| 4 | + |
| 5 | +import java.io.StringWriter; |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +public class EasyMock2FactoryWriterTest extends TestCase { |
| 9 | + |
| 10 | + private FactoryWriter factoryWriter; |
| 11 | + private StringWriter output = new StringWriter(); |
| 12 | + |
| 13 | + protected void setUp() throws Exception { |
| 14 | + super.setUp(); |
| 15 | + factoryWriter = new EasyMock2FactoryWriter(output, "com.blah", "EasyMatchers"); |
| 16 | + } |
| 17 | + |
| 18 | + public void testWritesMethodDelegationMethodWrappedInAdapter() throws IOException { |
| 19 | + FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyObject"); |
| 20 | + |
| 21 | + factoryWriter.writeMethod(method.getName(), method); |
| 22 | + assertEquals("" + |
| 23 | + " public static java.lang.Object anyObject() {\n" + |
| 24 | + " org.hamcrest.integration.EasyMockAdapter.adapt(\n" + |
| 25 | + " com.example.MyMatcher.anyObject());\n" + |
| 26 | + " return null;\n" + |
| 27 | + " }\n" + |
| 28 | + "\n", |
| 29 | + output.toString()); |
| 30 | + } |
| 31 | + |
| 32 | + public void testWritesReturnType() throws IOException { |
| 33 | + FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "anyString"); |
| 34 | + method.setGenerifiedType("String"); |
| 35 | + |
| 36 | + factoryWriter.writeMethod(method.getName(), method); |
| 37 | + assertEquals("" + |
| 38 | + " public static String anyString() {\n" + |
| 39 | + " org.hamcrest.integration.EasyMockAdapter.adapt(\n" + |
| 40 | + " com.example.MyMatcher.anyString());\n" + |
| 41 | + " return null;\n" + |
| 42 | + " }\n" + |
| 43 | + "\n", |
| 44 | + output.toString()); |
| 45 | + } |
| 46 | + |
| 47 | + public void testWritesAdvancedGenerifiedMatcherType() throws IOException { |
| 48 | + FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "weirdThing"); |
| 49 | + method.setGenerifiedType("java.util.Map<com.Foo<Cheese>,?>"); |
| 50 | + |
| 51 | + factoryWriter.writeMethod(method.getName(), method); |
| 52 | + assertEquals("" + |
| 53 | + " public static java.util.Map<com.Foo<Cheese>,?> weirdThing() {\n" + |
| 54 | + " org.hamcrest.integration.EasyMockAdapter.adapt(\n" + |
| 55 | + " com.example.MyMatcher.weirdThing());\n" + |
| 56 | + " return null;\n" + |
| 57 | + " }\n" + |
| 58 | + "\n", |
| 59 | + output.toString()); |
| 60 | + } |
| 61 | + |
| 62 | + public void testWritesParameters() throws IOException { |
| 63 | + FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "between"); |
| 64 | + method.addParameter("int[]", "lower"); |
| 65 | + method.addParameter("com.blah.Cheesable<Long>...", "upper"); |
| 66 | + |
| 67 | + factoryWriter.writeMethod(method.getName(), method); |
| 68 | + assertEquals("" + |
| 69 | + " public static java.lang.Object between(int[] lower, com.blah.Cheesable<Long>... upper) {\n" + |
| 70 | + " org.hamcrest.integration.EasyMockAdapter.adapt(\n" + |
| 71 | + " com.example.MyMatcher.between(lower, upper));\n" + |
| 72 | + " return null;\n" + |
| 73 | + " }\n" + |
| 74 | + "\n", |
| 75 | + output.toString()); |
| 76 | + } |
| 77 | + |
| 78 | + public void testWritesExceptions() throws IOException { |
| 79 | + FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky"); |
| 80 | + method.addException("java.io.IOException"); |
| 81 | + method.addException("com.foo.CheeselessException"); |
| 82 | + |
| 83 | + factoryWriter.writeMethod(method.getName(), method); |
| 84 | + assertEquals("" + |
| 85 | + " public static java.lang.Object tricky() throws java.io.IOException, com.foo.CheeselessException {\n" + |
| 86 | + " org.hamcrest.integration.EasyMockAdapter.adapt(\n" + |
| 87 | + " com.example.MyMatcher.tricky());\n" + |
| 88 | + " return null;\n" + |
| 89 | + " }\n" + |
| 90 | + "\n", |
| 91 | + output.toString()); |
| 92 | + } |
| 93 | +// |
| 94 | +// public void testWritesGenericTypeParameters() throws IOException { |
| 95 | +// FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "tricky"); |
| 96 | +// method.addGenericTypeParameter("T"); |
| 97 | +// method.addGenericTypeParameter("V extends String & Cheese"); |
| 98 | +// method.addParameter("T", "t"); |
| 99 | +// method.addParameter("List<V>", "v"); |
| 100 | +// |
| 101 | +// factoryWriter.writeMethod(method.getName(), method); |
| 102 | +// assertEquals("" + |
| 103 | +// " public static <T, V extends String & Cheese> org.hamcrest.Matcher tricky(T t, List<V> v) {\n" + |
| 104 | +// " return com.example.MyMatcher.tricky(t, v);\n" + |
| 105 | +// " }\n" + |
| 106 | +// "\n", |
| 107 | +// output.toString()); |
| 108 | +// } |
| 109 | +// |
| 110 | +// public void testWritesJavaDoc() throws IOException { |
| 111 | +// FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "needsDoc"); |
| 112 | +// method.setJavaDoc("This is a complicated method.\nIt needs docs.\n\n@see MoreStuff"); |
| 113 | +// |
| 114 | +// factoryWriter.writeMethod(method.getName(), method); |
| 115 | +// assertEquals("" + |
| 116 | +// " /**\n" + |
| 117 | +// " * This is a complicated method.\n" + |
| 118 | +// " * It needs docs.\n" + |
| 119 | +// " * \n" + |
| 120 | +// " * @see MoreStuff\n" + |
| 121 | +// " */\n" + |
| 122 | +// " public static org.hamcrest.Matcher needsDoc() {\n" + |
| 123 | +// " return com.example.MyMatcher.needsDoc();\n" + |
| 124 | +// " }\n" + |
| 125 | +// "\n", |
| 126 | +// output.toString()); |
| 127 | +// } |
| 128 | +// |
| 129 | +// public void testWritesMethodWithNameOverriden() throws IOException { |
| 130 | +// FactoryMethod method = new FactoryMethod("com.example.MyMatcher", "eq"); |
| 131 | +// |
| 132 | +// factoryWriter.writeMethod("anotherName", method); |
| 133 | +// assertEquals("" + |
| 134 | +// " public static org.hamcrest.Matcher anotherName() {\n" + |
| 135 | +// " return com.example.MyMatcher.eq();\n" + |
| 136 | +// " }\n" + |
| 137 | +// "\n", |
| 138 | +// output.toString()); |
| 139 | +// } |
| 140 | + |
| 141 | + // primitives |
| 142 | + // arrays? |
| 143 | + // and/or/not/etc |
| 144 | + |
| 145 | + |
| 146 | +} |
0 commit comments