Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeType;
import software.amazon.smithy.model.shapes.ShapeVisitor;
import software.amazon.smithy.model.shapes.ShortShape;
import software.amazon.smithy.model.shapes.StringShape;
Expand Down Expand Up @@ -400,13 +401,20 @@ private void writePropertyEqualityChecks(JavaWriter writer) {
var iter = shape.members().iterator();
while (iter.hasNext()) {
var member = iter.next();
var target = model.expectShape(member.getTarget());
var memberSymbol = symbolProvider.toSymbol(member);
writer.pushState();
writer.putContext("memberName", symbolProvider.toMemberName(member));
// Use `==` instead of `equals` for unboxed primitives
// Avoid `equals` for unboxed primitives
if (memberSymbol.expectProperty(SymbolProperties.IS_PRIMITIVE)
&& !CodegenUtils.isNullableMember(model, member)) {
writer.writeInline("this.${memberName:L} == that.${memberName:L}");
if (target.getType() == ShapeType.FLOAT) {
writer.writeInline("Float.compare(this.${memberName:L}, that.${memberName:L}) == 0");
} else if (target.getType() == ShapeType.DOUBLE) {
writer.writeInline("Double.compare(this.${memberName:L}, that.${memberName:L}) == 0");
} else {
writer.writeInline("this.${memberName:L} == that.${memberName:L}");
}
} else {
Class<?> comparator = CodegenUtils.isJavaArray(memberSymbol) ? Arrays.class : Objects.class;
writer.writeInline("$T.equals(this.${memberName:L}, that.${memberName:L})", comparator);
Expand Down
4 changes: 4 additions & 0 deletions codegen/plugins/types-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ description = "This module provides the codegen plugin for Smithy java type code
extra["displayName"] = "Smithy :: Java :: Codegen :: Types"
extra["moduleName"] = "software.amazon.smithy.java.codegen.types"

dependencies {
testImplementation(project(":codegen:test-utils"))
}

addGenerateSrcsTask("software.amazon.smithy.java.codegen.types.TestJavaTypeCodegenRunner")
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.java.codegen.types;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static software.amazon.smithy.java.codegen.test.PluginTestRunner.addTestCasesFromUrl;
import static software.amazon.smithy.java.codegen.test.PluginTestRunner.assertContentEquals;
import static software.amazon.smithy.java.codegen.test.PluginTestRunner.findGotContent;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.smithy.java.codegen.test.PluginTestRunner.TestCase;

public class TypesCodegenPluginTest {

@ParameterizedTest(name = "[{index}] => {0}")
@MethodSource("testCases")
public void runTestCase(TestCase test) {
test.builder().build();
var got = test.manifests().stream().flatMap(x -> x.getFiles().stream()).collect(Collectors.toSet());
for (var expected : test.expectedToContents().keySet()) {
var found = findExpected(expected, got);
assertNotNull(found);
var contents = findGotContent(found, test);
assertTrue(contents.isPresent());
assertContentEquals(test.expectedToContents().get(expected), contents.get().trim());
}
}

//@ParameterizedTest(name = "[{index}] => {0}")
//@MethodSource("testCases")
// This test can be used to render the java files when we there are changes to the codegen logic.
public void renderExpected(TestCase test) throws IOException {
test.builder().build();
var got = test.manifests().stream().flatMap(x -> x.getFiles().stream()).collect(Collectors.toSet());
for (var expected : test.expectedToContents().keySet()) {
var found = findExpected(expected, got);
assertNotNull(found);
var contents = findGotContent(found, test);
var expectedFile = new File("/tmp/rendered/" + test + "/expected" + expected);
expectedFile.getParentFile().mkdirs();
Files.write(expectedFile.toPath(), contents.get().getBytes(StandardCharsets.UTF_8));
}
}

private Path findExpected(String expected, Set<Path> manifestFiles) {
return manifestFiles.stream().filter(path -> path.toString().contains(expected)).findFirst().orElse(null);
}

public static Collection<TestCase> testCases() {
return addTestCasesFromUrl(TypesCodegenPluginTest.class.getResource("test-cases"));
}
}
Loading
Loading