Skip to content

Commit

Permalink
Merge pull request #340 from HubSpot/implement-error-equality
Browse files Browse the repository at this point in the history
Implements equals() and hashCode() methods for TemplateError
  • Loading branch information
mattcoley authored Jun 11, 2019
2 parents b2a9bee + 6386d68 commit ec2fbb4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/com/hubspot/jinjava/interpret/TemplateError.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hubspot.jinjava.interpret;

import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import org.apache.commons.lang3.exception.ExceptionUtils;
Expand Down Expand Up @@ -331,4 +332,37 @@ public String toString() {
", categoryErrors=" + categoryErrors +
'}';
}

@Override
public boolean equals(Object o) {
if (!(o instanceof TemplateError)) {
return false;
}

TemplateError other = (TemplateError) o;
return Objects.equals(severity, other.severity)
&& Objects.equals(reason, other.reason)
&& Objects.equals(item, other.item)
&& Objects.equals(message, other.message)
&& Objects.equals(fieldName, other.fieldName)
&& Objects.equals(lineno, other.lineno)
&& Objects.equals(startPosition, other.startPosition)
&& Objects.equals(category, other.category)
&& Objects.equals(categoryErrors, other.categoryErrors)
&& Objects.equals(scopeDepth, other.scopeDepth);
}

@Override
public int hashCode() {
return Objects.hash(severity,
reason,
item,
message,
fieldName,
lineno,
startPosition,
category,
categoryErrors,
scopeDepth);
}
}
45 changes: 45 additions & 0 deletions src/test/java/com/hubspot/jinjava/interpret/TemplateErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

import com.google.common.collect.ImmutableMap;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.interpret.TemplateError.ErrorItem;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
import com.hubspot.jinjava.interpret.errorcategory.BasicTemplateErrorCategory;

public class TemplateErrorTest {

Expand Down Expand Up @@ -59,4 +63,45 @@ public void itLimitsErrorStringToAReasonableSize() {
assertThat(e.getMessage()).startsWith("Cannot resolve property 'other' in '{foo=");
assertThat(e.getMessage().length()).isLessThan(1500);
}


@Test
public void itComparesEquality() {

TemplateError error1 = new TemplateError(ErrorType.WARNING,
ErrorReason.SYNTAX_ERROR,
ErrorItem.TAG,
"error",
"badField",
10,
100,
new Exception(),
BasicTemplateErrorCategory.FROM_CYCLE_DETECTED,
ImmutableMap.of("test1", "test2"));

TemplateError error2 = new TemplateError(ErrorType.WARNING,
ErrorReason.SYNTAX_ERROR,
ErrorItem.TAG,
"error",
"badField",
10,
100,
new Exception(),
BasicTemplateErrorCategory.FROM_CYCLE_DETECTED,
ImmutableMap.of("test1", "test2"));

TemplateError error3 = new TemplateError(ErrorType.WARNING,
ErrorReason.SYNTAX_ERROR,
ErrorItem.TAG,
"error",
"differentField",
10,
100,
new Exception(),
BasicTemplateErrorCategory.FROM_CYCLE_DETECTED,
ImmutableMap.of("test1", "test2"));

assertThat(error1).isEqualTo(error2);
assertThat(error1).isNotEqualTo(error3);
}
}

0 comments on commit ec2fbb4

Please sign in to comment.