Skip to content

Commit 642908f

Browse files
author
joeretro
committed
Added writer for a quick summary of all matchers.
1 parent 5c08b0e commit 642908f

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.hamcrest.generator;
2+
3+
import org.hamcrest.generator.FactoryWriter;
4+
import org.hamcrest.generator.FactoryMethod;
5+
6+
import java.io.PrintStream;
7+
import java.io.IOException;
8+
9+
/**
10+
* Dumps a quick list of factory methods. Designed to be read by users, as a cheatsheet.
11+
*
12+
* @author Joe Walnes
13+
*/
14+
public class QuickReferenceWriter implements FactoryWriter {
15+
16+
private final PrintStream out;
17+
private int columnPosition = 14;
18+
19+
public QuickReferenceWriter(PrintStream out) {
20+
this.out = out;
21+
}
22+
23+
public QuickReferenceWriter() {
24+
this(System.out);
25+
}
26+
27+
public void setColumnPosition(int columnPosition) {
28+
this.columnPosition = columnPosition;
29+
}
30+
31+
public void writeHeader() throws IOException {
32+
}
33+
34+
public void writeMethod(String generatedMethodName, FactoryMethod factoryMethod) throws IOException {
35+
String actsOn = removePackageNames(factoryMethod.getGenerifiedType());
36+
for (int i = actsOn.length(); i < columnPosition; i++) {
37+
out.append(' ');
38+
}
39+
out.append('[').append(actsOn).append("] ");
40+
out.append(generatedMethodName);
41+
out.append('(');
42+
boolean seenFirst = false;
43+
for (FactoryMethod.Parameter parameter : factoryMethod.getParameters()) {
44+
if (seenFirst) {
45+
out.append(", ");
46+
} else {
47+
seenFirst = true;
48+
}
49+
out.append(removePackageNames(parameter.getType()));
50+
}
51+
out.append(')');
52+
out.println();
53+
}
54+
55+
private String removePackageNames(String in) {
56+
// Simplify fully qualified names (allowing for trailing '...').
57+
return in == null ? "" : in.replaceAll("[^<>]*\\.([^\\.])", "$1");
58+
}
59+
60+
public void writeFooter() throws IOException {
61+
}
62+
63+
public void close() throws IOException {
64+
}
65+
66+
public void flush() throws IOException {
67+
}
68+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.hamcrest.generator;
2+
3+
import junit.framework.TestCase;
4+
5+
import java.io.PrintStream;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.IOException;
8+
9+
public class QuickReferenceWriterTest extends TestCase {
10+
11+
private ByteArrayOutputStream actualBuffer;
12+
private ByteArrayOutputStream expectedBuffer;
13+
private PrintStream expected;
14+
private QuickReferenceWriter writer;
15+
16+
protected void setUp() throws Exception {
17+
super.setUp();
18+
actualBuffer = new ByteArrayOutputStream();
19+
writer = new QuickReferenceWriter(new PrintStream(actualBuffer));
20+
21+
expectedBuffer = new ByteArrayOutputStream();
22+
expected = new PrintStream(expectedBuffer);
23+
}
24+
25+
public void testWritesSimplifiedSummaryOfMatchers() throws IOException {
26+
FactoryMethod namedMethod = new FactoryMethod("SomeClass", "someMethod");
27+
namedMethod.addParameter("Cheese", "a");
28+
namedMethod.addParameter("int", "b");
29+
namedMethod.setGenerifiedType("String");
30+
writer.writeMethod("namedMethod", namedMethod);
31+
32+
FactoryMethod anotherMethod = new FactoryMethod("SomeClass", "anotherMethod");
33+
anotherMethod.setGenerifiedType("int");
34+
writer.writeMethod("anotherMethod", anotherMethod);
35+
36+
expected.println(" [String] namedMethod(Cheese, int)");
37+
expected.println(" [int] anotherMethod()");
38+
verify();
39+
}
40+
41+
public void testRemovesPackageNames() throws IOException {
42+
FactoryMethod namedMethod = new FactoryMethod("SomeClass", "someMethod");
43+
namedMethod.addParameter("com.blah.Foo", "a");
44+
namedMethod.addParameter("com.foo.Cheese<x.y.Zoo>", "b");
45+
namedMethod.setGenerifiedType("java.lang.Cheese");
46+
writer.writeMethod("namedMethod", namedMethod);
47+
48+
expected.println(" [Cheese] namedMethod(Foo, Cheese<Zoo>)");
49+
verify();
50+
}
51+
52+
private void verify() {
53+
assertEquals(new String(expectedBuffer.toByteArray()), new String(actualBuffer.toByteArray()));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)