Skip to content

Commit

Permalink
Add equals and hashCode to ErrorExample (smithy-lang#2052)
Browse files Browse the repository at this point in the history
* Add `equals` and `hashCode` implementations to `Example` and `ExampleOrder`

* remove line

* add negative test

* Use `Objects.equals`

Co-authored-by: Jaykumar Gosar <5666661+gosar@users.noreply.github.com>

---------

Co-authored-by: Miguel Villa <miguel.villa@siriusxm.com>
Co-authored-by: Jaykumar Gosar <5666661+gosar@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent c34f984 commit 7b7a3f3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,23 @@ public Builder content(ObjectNode content) {
return this;
}
}

@Override
public int hashCode() {
return Objects.hash(shapeId, content);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (other == null || other.getClass() != this.getClass()) {
return false;
}
ErrorExample otherExample = (ErrorExample) other;
return Objects.equals(shapeId, otherExample.shapeId) && Objects.equals(content, otherExample.content);
}
}

public static final class Provider implements TraitService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -27,6 +28,8 @@
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.ExamplesTrait.ErrorExample;
import software.amazon.smithy.model.traits.ExamplesTrait.Example;

public class ExamplesTraitTest {
@Test
Expand Down Expand Up @@ -81,4 +84,24 @@ public void omitsAllowConstraintErrorsFromSerializedNodeWhenNotTrue() {
assertFalse(serialized.get(1).get().asObjectNode().get().getMember("allowConstraintErrors").isPresent());
assertTrue(serialized.get(2).get().asObjectNode().get().getMember("allowConstraintErrors").isPresent());
}

@Test
public void exampleEqualsWorks() {
ObjectNode input = Node.objectNode().withMember("a", Node.from("b"));
ObjectNode output = Node.objectNode().withMember("c", Node.from("d"));
ErrorExample errorExample1 = ErrorExample.builder().shapeId(ShapeId.from("smithy.example#FooError")).content(Node.objectNode()
.withMember("e", Node.from("f"))).build();
ErrorExample errorExample2 = ErrorExample.builder().shapeId(ShapeId.from("smithy.example#FooError")).content(Node.objectNode()
.withMember("e", Node.from("f"))).build();
ErrorExample errorExample3 = ErrorExample.builder().shapeId(ShapeId.from("smithy.example#FooError")).content(Node.objectNode()
.withMember("g", Node.from("h"))).build();
Example example1 = Example.builder().title("foo").documentation("docs").input(input).output(output).error(errorExample1).build();
Example example2 = Example.builder().title("foo").documentation("docs").input(input).output(output).error(errorExample2).build();
Example example3 = Example.builder().title("foo").documentation("docs").input(input).output(output).error(errorExample3).build();
assertThat(errorExample1, equalTo(errorExample2));
assertThat(errorExample1, not(equalTo(errorExample3)));
assertThat(example1, equalTo(example2));
assertThat(example1, not(equalTo(example3)));
}

}

0 comments on commit 7b7a3f3

Please sign in to comment.