Skip to content

Commit

Permalink
Added equals and hashcode to models. Updated/Added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wkennedy committed Jul 18, 2024
1 parent 25c385a commit c5a7d2b
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 13 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/github/wkennedy/abi/models/DecodedFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@SuppressWarnings("unused")
public class DecodedFunctions {
Expand Down Expand Up @@ -63,4 +64,21 @@ public String toString() {
", params=" + params +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

DecodedFunctions that = (DecodedFunctions) o;
return Objects.equals(name, that.name) && Objects.equals(params, that.params) && Objects.equals(nestedDecodedFunctions, that.nestedDecodedFunctions);
}

@Override
public int hashCode() {
int result = Objects.hashCode(name);
result = 31 * result + Objects.hashCode(params);
result = 31 * result + Objects.hashCode(nestedDecodedFunctions);
return result;
}
}
22 changes: 18 additions & 4 deletions src/main/java/com/github/wkennedy/abi/models/DecodedLog.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.wkennedy.abi.models;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;

public class DecodedLog {
private String name;
Expand Down Expand Up @@ -68,4 +65,21 @@ public String toString() {
", events=" + events +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

DecodedLog that = (DecodedLog) o;
return Objects.equals(name, that.name) && Objects.equals(address, that.address) && Objects.equals(events, that.events);
}

@Override
public int hashCode() {
int result = Objects.hashCode(name);
result = 31 * result + Objects.hashCode(address);
result = 31 * result + Objects.hashCode(events);
return result;
}
}
20 changes: 19 additions & 1 deletion src/main/java/com/github/wkennedy/abi/models/Log.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.wkennedy.abi.models;

import java.util.ArrayList;
import java.util.List;

public class Log {
private String data;
private List<String> topics;
private List<String> topics = new ArrayList<>();
private String address;

public Log() {
Expand Down Expand Up @@ -48,4 +49,21 @@ public String toString() {
", address='" + address + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Log log = (Log) o;
return data.equals(log.data) && topics.equals(log.topics) && address.equals(log.address);
}

@Override
public int hashCode() {
int result = data.hashCode();
result = 31 * result + topics.hashCode();
result = 31 * result + address.hashCode();
return result;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/github/wkennedy/abi/models/Param.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.wkennedy.abi.models;

import java.util.Arrays;
import java.util.Objects;

import com.github.wkennedy.util.ByteUtil;

public class Param {
Expand Down Expand Up @@ -64,4 +66,22 @@ public String toDisplayString() {
String valueString = value == null ? "null" : (value.getClass().isArray() ? Arrays.toString((Object[]) value) : value.toString());
return this.getClass().getName() + "(name=" + this.name + ", type=" + this.getType() + ", value=" + valueString + ")";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Param param = (Param) o;
return Objects.equals(name, param.name) && Objects.equals(type, param.type) && Objects.equals(value, param.value) && Objects.equals(rawValue, param.rawValue);
}

@Override
public int hashCode() {
int result = Objects.hashCode(name);
result = 31 * result + Objects.hashCode(type);
result = 31 * result + Objects.hashCode(value);
result = 31 * result + Objects.hashCode(rawValue);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.github.wkennedy.abi.models;

import com.github.wkennedy.abi.models.DecodedFunctions;
import com.github.wkennedy.abi.models.Param;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;

public class DecodedFunctionsTest {

@Test
void testEquals_sameObject_ReturnsTrue() {
DecodedFunctions decodedFunctions1 = new DecodedFunctions("function1", Collections.emptyList());
assertTrue(decodedFunctions1.equals(decodedFunctions1));
}

@Test
void testEquals_sameValues_ReturnsTrue() {
DecodedFunctions decodedFunctions1 = new DecodedFunctions("function1", Collections.emptyList());
DecodedFunctions decodedFunctions2 = new DecodedFunctions("function1", Collections.emptyList());
assertTrue(decodedFunctions1.equals(decodedFunctions2));
}

@Test
void testEquals_differentNames_ReturnsFalse() {
DecodedFunctions decodedFunctions1 = new DecodedFunctions("function1", Collections.emptyList());
DecodedFunctions decodedFunctions2 = new DecodedFunctions("function2", Collections.emptyList());
assertFalse(decodedFunctions1.equals(decodedFunctions2));
}

@Test
void testEquals_differentParams_ReturnsFalse() {
Param param1 = new Param("param1", "type1", "value");
Param param2 = new Param("param2", "type1", "value");

DecodedFunctions decodedFunctions1 = new DecodedFunctions("function1", Arrays.asList(param1));
DecodedFunctions decodedFunctions2 = new DecodedFunctions("function1", Arrays.asList(param2));

assertFalse(decodedFunctions1.equals(decodedFunctions2));
}

@Test
void testEquals_nullObject_ReturnsFalse() {
DecodedFunctions decodedFunctions1 = new DecodedFunctions("function1", Collections.emptyList());
assertFalse(decodedFunctions1.equals(null));
}

@Test
void testEquals_nonDecodedFunctionsObject_ReturnsFalse() {
DecodedFunctions decodedFunctions1 = new DecodedFunctions("function1", Collections.emptyList());
assertFalse(decodedFunctions1.equals("Not a DecodedFunctions object"));
}

@Test
void testEquals_differentNestedDecodedFunctions_ReturnsFalse() {
DecodedFunctions decodedFunctions1 = new DecodedFunctions("function1", Collections.emptyList());
DecodedFunctions decodedFunctions2 = new DecodedFunctions("function1", Collections.emptyList());

DecodedFunctions nestedDecodedFunctions = new DecodedFunctions("nestedFunction", Collections.emptyList());
decodedFunctions1.addNestedDecodedFunction(nestedDecodedFunctions);

assertFalse(decodedFunctions1.equals(decodedFunctions2));
}

@Test
void testHashCode_sameValues_ReturnsSameHashCode() {
DecodedFunctions decodedFunctions1 = new DecodedFunctions("function1", Collections.emptyList());
DecodedFunctions decodedFunctions2 = new DecodedFunctions("function1", Collections.emptyList());
assertEquals(decodedFunctions1.hashCode(), decodedFunctions2.hashCode());
}
}
69 changes: 61 additions & 8 deletions src/test/java/com/github/wkennedy/abi/models/DecodedLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,32 @@ void getEventTestEventPresent() {
Param param1 = new Param("event1", "type1", "value1");
Param param2 = new Param("event2", "type2", "value2");
List<Param> events = Arrays.asList(param1, param2);

DecodedLog decodedLog = new DecodedLog("name", "address", events);

Param foundEvent = decodedLog.getEvent("event2");

assertEquals(param2, foundEvent);
}

@Test
void getEventTestEventNotPresent() {
Param param1 = new Param("event1", "type1", "value1");
List<Param> events = List.of(param1);

DecodedLog decodedLog = new DecodedLog("name", "address", events);

Param foundEvent = decodedLog.getEvent("event2");

assertNull(foundEvent);
}

@Test
void getEventTestNoEvents() {
DecodedLog decodedLog = new DecodedLog("name", "address", null);

Param foundEvent = decodedLog.getEvent("event1");

assertNull(foundEvent);
}

Expand Down Expand Up @@ -83,4 +83,57 @@ void getEventsMapTestMultipleEvents() {
assertEquals(param1, eventsMap.get("event1"));
assertEquals(param2, eventsMap.get("event2"));
}

@Test
void equalsTestSameObject() {
DecodedLog decodedLog1 = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
assertTrue(decodedLog1.equals(decodedLog1));
}

@Test
void equalsTestDifferentObjectSameValues() {
DecodedLog decodedLog1 = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
DecodedLog decodedLog2 = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
assertTrue(decodedLog1.equals(decodedLog2));
}

@Test
void equalsTestDifferentObjectDifferentValues() {
DecodedLog decodedLog1 = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
DecodedLog decodedLog2 = new DecodedLog("name2", "address2", Arrays.asList(new Param("event2", "type2", "value2")));
assertFalse(decodedLog1.equals(decodedLog2));
}

@Test
void equalsTestObjectOfTypeOtherThanDecodedLog() {
DecodedLog decodedLog = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
String otherObject = "Not a DecodedLog";
assertFalse(decodedLog.equals(otherObject));
}

@Test
void equalsTestObjectToNull() {
DecodedLog decodedLog = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
assertFalse(decodedLog.equals(null));
}

@Test
void hashCodeTestEqualObjects() {
DecodedLog decodedLog1 = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
DecodedLog decodedLog2 = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
assertEquals(decodedLog1.hashCode(), decodedLog2.hashCode());
}

@Test
void hashCodeTestNotEqualObjects() {
DecodedLog decodedLog1 = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
DecodedLog decodedLog2 = new DecodedLog("name2", "address2", Arrays.asList(new Param("event2", "type2", "value2")));
assertNotEquals(decodedLog1.hashCode(), decodedLog2.hashCode());
}

@Test
void hashCodeTestSameObject() {
DecodedLog decodedLog1 = new DecodedLog("name1", "address1", Arrays.asList(new Param("event1", "type1", "value1")));
assertEquals(decodedLog1.hashCode(), decodedLog1.hashCode());
}
}
87 changes: 87 additions & 0 deletions src/test/java/com/github/wkennedy/abi/models/LogTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

package com.github.wkennedy.abi.models;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class LogTest {

@Test
public void equals_sameObject_returnsTrue() {
List<String> topics = new ArrayList<>();
topics.add("topic1");
Log log1 = new Log("data", topics, "address");
assertTrue(log1.equals(log1));
}

@Test
public void equals_nullObject_returnsFalse() {
List<String> topics = new ArrayList<>();
topics.add("topic1");
Log log1 = new Log("data", topics, "address");
assertFalse(log1.equals(null));
}

@Test
public void equals_differentTypeObject_returnsFalse() {
List<String> topics = new ArrayList<>();
topics.add("topic1");
Log log1 = new Log("data", topics, "address");
assertFalse(log1.equals("String"));
}

@Test
public void equals_sameValueObject_returnsTrue() {
List<String> topics1 = new ArrayList<>();
topics1.add("topic1");
Log log1 = new Log("data", topics1, "address");

List<String> topics2 = new ArrayList<>();
topics2.add("topic1");
Log log2 = new Log("data", topics2, "address");

assertTrue(log1.equals(log2));
}

@Test
public void equals_differentValueObject_returnsFalse() {
List<String> topics1 = new ArrayList<>();
topics1.add("topic1");
Log log1 = new Log("data", topics1, "address");

List<String> topics2 = new ArrayList<>();
topics2.add("topic2");
Log log2 = new Log("data", topics2, "address");

assertFalse(log1.equals(log2));
}
@Test
public void hashCode_sameValueObjects_returnSameHashcode() {
List<String> topics1 = new ArrayList<>();
topics1.add("topic1");
Log log1 = new Log("data", topics1, "address");

List<String> topics2 = new ArrayList<>();
topics2.add("topic1");
Log log2 = new Log("data", topics2, "address");

assertEquals(log1.hashCode(), log2.hashCode());
}

@Test
public void hashCode_differentValueObjects_returnDifferentHashcode() {
List<String> topics1 = new ArrayList<>();
topics1.add("topic1");
Log log1 = new Log("data", topics1, "address");

List<String> topics2 = new ArrayList<>();
topics2.add("topic2");
Log log2 = new Log("data", topics2, "address");

assertNotEquals(log1.hashCode(), log2.hashCode());
}
}
Loading

0 comments on commit c5a7d2b

Please sign in to comment.