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

Fix for test wrappers generation #2025

Merged
merged 3 commits into from
Apr 4, 2024
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

### Bug Fixes

*
* Fix for test wrappers generation [#2025](https://github.com/web3j/web3j/pull/2025)

### Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ private Set<String> getDuplicateFunctionNames(List<AbiDefinition> functionDefini
private static MethodSpec buildGetDeploymentBinaryMethod() {
MethodSpec.Builder toReturn =
MethodSpec.methodBuilder("getDeploymentBinary")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
.returns(ClassName.get(String.class));

CodeBlock codeBlock =
Expand Down
16 changes: 11 additions & 5 deletions codegen/src/main/java/org/web3j/codegen/unit/gen/MethodFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.web3j.codegen.unit.gen;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -61,9 +62,11 @@ public static List<MethodSpec> generateMethodSpecsForEachTest(Class theContract)
.forEach(
method -> {
String uniqueName = getUniqueName(method, methodNameCountMap);
listOfMethodSpecs.add(
new MethodParser(method, theContract, uniqueName)
.getMethodSpec());
if (!Modifier.isPrivate(method.getModifiers())) {
listOfMethodSpecs.add(
new MethodParser(method, theContract, uniqueName)
.getMethodSpec());
}
});

return listOfMethodSpecs;
Expand All @@ -76,8 +79,11 @@ public static List<FunSpec> generateFunctionSpecsForEachTest(Class theContract)
.forEach(
method -> {
String uniqueName = getUniqueName(method, functionNameCountMap);
listOfFunSpecs.add(
new FunParser(method, theContract, uniqueName).getFunSpec());
if (!Modifier.isPrivate(method.getModifiers())) {
listOfFunSpecs.add(
new FunParser(method, theContract, uniqueName)
.getFunSpec());
}
});

return listOfFunSpecs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,22 @@ public void testGeneratedDuplicateGreetingMethods() {
greetMethodSpecs.stream().anyMatch(methodSpec -> methodSpec.name.equals("greet1")));
assertEquals(2, greetMethodSpecs.size());
}

@Test
public void testGetDeploymentBinaryMethodNotGenerated() {
List<MethodSpec> allMethodSpecs =
MethodFilter.generateMethodSpecsForEachTest(greeterContractClass);

// Filter all MethodSpecs for those related to "getDeploymentBinary" method
List<MethodSpec> getDeploymentBinaryMethodSpecs =
allMethodSpecs.stream()
.filter(methodSpec -> methodSpec.name.startsWith("getDeploymentBinary"))
.collect(Collectors.toList());

// Ensure no MethodSpecs were generated for getDeploymentBinary method
assertEquals(
0,
getDeploymentBinaryMethodSpecs.size(),
"MethodSpec list should not contain getDeploymentBinary method");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,22 @@ public void testGeneratedDuplicateGreetingMethods() {
.anyMatch(methodSpec -> methodSpec.getName().equals("greet1")));
assertEquals(2, greetFunSpecs.size());
}

@Test
public void testGetDeploymentBinaryMethodNotGenerated() {
List<FunSpec> allMethodSpecs =
MethodFilter.generateFunctionSpecsForEachTest(greeterContractClass);

// Filter all FunSpecs for those related to "getDeploymentBinary" method
List<FunSpec> getDeploymentBinaryFunSpecs =
allMethodSpecs.stream()
.filter(funSpec -> funSpec.getName().startsWith("getDeploymentBinary"))
.collect(Collectors.toList());

// Ensure no MethodSpecs were generated for getDeploymentBinary method
assertEquals(
0,
getDeploymentBinaryFunSpecs.size(),
"MethodSpec list should not contain getDeploymentBinary function");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static void linkLibraries(List<Contract.LinkReference> references) {
librariesLinkedBinary = linkBinaryWithReferences(BINARY, references);
}

public static String getDeploymentBinary() {
private static String getDeploymentBinary() {
if (librariesLinkedBinary != null) {
return librariesLinkedBinary;
} else {
Expand Down
Loading