Skip to content

Commit

Permalink
ISSUES-26 return MockBuilder2 from mockBuilder method
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Dec 10, 2023
1 parent 06be77a commit 4f288ed
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.jbock.simple.processor.binding.DependencyRequest;
import io.jbock.simple.processor.binding.FactoryElement;
import io.jbock.simple.processor.binding.Key;
import io.jbock.simple.processor.binding.ParameterBinding;

import javax.annotation.processing.Generated;
import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -81,16 +82,21 @@ TypeSpec generate() {
component.factoryElement().ifPresent(factory -> {
spec.addMethod(generateFactoryMethod(factory));
spec.addType(factoryImpl.generate(factory, mockBuilder));
if (component.mockBuilder()) {
spec.addMethod(generateMockBuilderMethodFactory());
}
});
component.builderElement().ifPresent(builder -> {
spec.addMethod(generateBuilderMethod(builder));
spec.addType(builderImpl.generate(builder, mockBuilder, mockBuilder2));
});
if (component.factoryElement().isEmpty() && component.builderElement().isEmpty()) {
spec.addMethod(generateCreateMethod());
if (component.mockBuilder()) {
spec.addMethod(generateMockBuilderMethod());
}
}
if (component.mockBuilder()) {
spec.addMethod(generateMockBuilderMethod());
spec.addType(mockBuilder.generate());
spec.addType(mockBuilder2.generate());
}
Expand Down Expand Up @@ -160,12 +166,37 @@ private MethodSpec generateCreateMethod() {
MethodSpec generateMockBuilderMethod() {
MethodSpec.Builder method = MethodSpec.methodBuilder(MOCK_BUILDER_METHOD);
method.addJavadoc("Visible for testing. Do not call this method from production code.");
method.addStatement("return new $T()", mockBuilder.getClassName());
method.returns(mockBuilder.getClassName());
method.addStatement("return new $T()", mockBuilder2.getClassName());
method.returns(mockBuilder2.getClassName());
method.addModifiers(STATIC);
if (component.publicMockBuilder()) {
method.addModifiers(modifiers);
}
return method.build();
}

MethodSpec generateMockBuilderMethodFactory() {
MethodSpec.Builder method = MethodSpec.methodBuilder(MOCK_BUILDER_METHOD);
method.addModifiers(STATIC);
method.addJavadoc("Visible for testing. Do not call this method from production code.");
method.returns(mockBuilder2.getClassName());
List<CodeBlock> constructorParameters = new ArrayList<>();
for (NamedBinding namedBinding : sorted.values()) {
Binding b = namedBinding.binding();
if (!(b instanceof ParameterBinding)) {
continue;
}
Key key = b.key();
ParameterSpec param = names.apply(key);
method.addParameter(param);
constructorParameters.add(CodeBlock.of("$N", param));
}
if (component.publicMockBuilder()) {
method.addModifiers(modifiers);
}
method.addStatement("return new $T($L)",
mockBuilder2.getClassName(),
constructorParameters.stream().collect(CodeBlock.joining(", ")));
return method.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ void factoryParameter() {
" return new Factory_Impl(null);",
" }",
"",
" static MockBuilder2 mockBuilder(String s) {",
" return new MockBuilder2(s);",
" }",
"",
" private static final class Factory_Impl implements TestClass.AComponent.Factory {",
" final MockBuilder mockBuilder;",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ void staticMethodBindings() {
" return new TestClass_AComponent_Impl(a);",
" }",
"",
" public static MockBuilder mockBuilder() {",
" return new MockBuilder();",
" public static MockBuilder2 mockBuilder() {",
" return new MockBuilder2();",
" }",
"",
" public static final class MockBuilder {",
" public static final class MockBuilder2 {",
" private TestClass.C c;",
" private TestClass.B b;",
" private TestClass.A a;",
Expand All @@ -81,15 +81,15 @@ void staticMethodBindings() {
" TestClass.A a = this.a != null ? this.a : new TestClass.A(b);",
" return new TestClass_AComponent_Impl(a);",
" }",
" public MockBuilder c(TestClass.C c) {",
" public MockBuilder2 c(TestClass.C c) {",
" this.c = c;",
" return this;",
" }",
" public MockBuilder b(TestClass.B b) {",
" public MockBuilder2 b(TestClass.B b) {",
" this.b = b;",
" return this;",
" }",
" public MockBuilder a(TestClass.A a) {",
" public MockBuilder2 a(TestClass.A a) {",
" this.a = a;",
" return this;",
" }",
Expand Down Expand Up @@ -154,11 +154,11 @@ void dependencyDiamond() {
" return new TestClass_AComponent_Impl(a);",
" }",
"",
" static MockBuilder mockBuilder() {",
" return new MockBuilder();",
" static MockBuilder2 mockBuilder() {",
" return new MockBuilder2();",
" }",
"",
" static final class MockBuilder {",
" static final class MockBuilder2 {",
" private TestClass.E e;",
"",
" private TestClass.C c;",
Expand All @@ -175,22 +175,22 @@ void dependencyDiamond() {
" return new TestClass_AComponent_Impl(a);",
" }",
"",
" MockBuilder e(TestClass.E e) {",
" MockBuilder2 e(TestClass.E e) {",
" this.e = e;",
" return this;",
" }",
"",
" MockBuilder c(TestClass.C c) {",
" MockBuilder2 c(TestClass.C c) {",
" this.c = c;",
" return this;",
" }",
"",
" MockBuilder b(TestClass.B b) {",
" MockBuilder2 b(TestClass.B b) {",
" this.b = b;",
" return this;",
" }",
"",
" MockBuilder a(TestClass.A a) {",
" MockBuilder2 a(TestClass.A a) {",
" this.a = a;",
" return this;",
" }",
Expand Down

0 comments on commit 4f288ed

Please sign in to comment.