Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review refactor: Add a new way to build a query in QueryFactory #1740

Merged
merged 3 commits into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -915,4 +915,9 @@ public interface Factory {
* @see CoreFactory#create(Class)
*/
CtElement createElement(Class<? extends CtElement> klass);

/**
* @see QueryFactory#createQuery(Object...)
*/
CtQuery createQuery(Object... input);
}
5 changes: 5 additions & 0 deletions src/main/java/spoon/reflect/factory/FactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1167,4 +1167,9 @@ public CtPackage createPackage(CtPackage parent, String simpleName) {
public CtElement createElement(Class<? extends CtElement> klass) {
return Core().create(klass);
}

@Override
public CtQuery createQuery(Object... input) {
return Query().createQuery(input);
}
}
8 changes: 8 additions & 0 deletions src/main/java/spoon/reflect/factory/QueryFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ public CtQuery createQuery() {
public CtQuery createQuery(Object input) {
return new CtQueryImpl(input);
}

/**
* Creates a bound query with an optional number
* of inputs elements to the query (see {@link CtQuery#setInput(Object...)})
*/
public CtQuery createQuery(Object... input) {
return new CtQueryImpl(input);
}
}
22 changes: 22 additions & 0 deletions src/test/java/spoon/test/filters/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ public void testReuseOfQuery() throws Exception {
assertSame(cls2, ((CtQueryImpl)q).getInputs().get(0));

}

@Test
public void testReuseOfBaseQuery() throws Exception {
// contract: an empty query can be used on several inputs
Expand All @@ -751,7 +752,28 @@ public void testReuseOfBaseQuery() throws Exception {
assertEquals("Tostada", q.setInput(cls2).list().get(0));
}

@Test
public void testQueryWithOptionalNumberOfInputs() throws Exception {
// contract: QueryFactory allows to create query with an optional number of inputs
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] {"--output-type", "nooutput","--level","info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();

CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
CtClass<?> cls2 = launcher.getFactory().Class().get(Tostada.class);
CtClass<?> cls3 = launcher.getFactory().Class().get(Antojito.class);

// here is the query
CtQuery q1 = launcher.getFactory().Query().createQuery(cls, cls2).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[]{"Tacos", "Tostada"}, q1.list().toArray());

CtQuery q2 = launcher.getFactory().Query().createQuery(cls, cls3).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[]{"Tacos", "Antojito"}, q2.list().toArray());

CtQuery q3 = launcher.getFactory().Query().createQuery(cls, cls2, cls3).map((CtClass c) -> c.getSimpleName());
assertArrayEquals(new String[]{"Tacos", "Tostada", "Antojito"}, q3.list().toArray());
}

// now testing map(CtConsumableFunction)

Expand Down