Skip to content

Commit 74a88a7

Browse files
committed
Add additional testing for PyList
1 parent e5753f3 commit 74a88a7

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/test/java/com/hubspot/jinjava/objects/collections/PyListTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.hubspot.jinjava.interpret.IndexOutOfRangeException;
77
import com.hubspot.jinjava.interpret.RenderResult;
88
import com.hubspot.jinjava.interpret.TemplateError;
9+
import java.util.ArrayList;
910
import java.util.Collections;
1011
import org.junit.Test;
1112

@@ -261,4 +262,38 @@ public void itDisallowsAppendingSelf() {
261262
)
262263
.isEqualTo("[1, 2]");
263264
}
265+
266+
@Test
267+
public void itComputesHashCodeWhenListContainsItself() {
268+
PyList list1 = new PyList(new ArrayList<>());
269+
PyList list2 = new PyList(new ArrayList<>());
270+
list1.add(list2);
271+
int initialHashCode = list1.hashCode();
272+
list2.add(list1);
273+
int hashCodeWithInfiniteRecursion = list1.hashCode();
274+
assertThat(initialHashCode).isNotEqualTo(hashCodeWithInfiniteRecursion);
275+
assertThat(list1.hashCode())
276+
.isEqualTo(hashCodeWithInfiniteRecursion)
277+
.describedAs("Hash code should be consistent on multiple calls");
278+
assertThat(list2.hashCode())
279+
.isEqualTo(list1.hashCode())
280+
.describedAs(
281+
"The two lists are currently the same as they are both a list1 of a single infinitely recurring list"
282+
);
283+
list1.add(123456);
284+
assertThat(list2.hashCode())
285+
.isNotEqualTo(list1.hashCode())
286+
.describedAs(
287+
"The two lists are no longer the same as list1 has 2 elements while list2 has one"
288+
);
289+
PyList copy = list1.copy();
290+
assertThat(copy.hashCode())
291+
.isNotEqualTo(list1.hashCode())
292+
.describedAs(
293+
"copy is not the same as list1 because it is a list of a list of recursion, whereas list1 is a list of recursion"
294+
);
295+
assertThat(list1.copy().hashCode())
296+
.isEqualTo(copy.hashCode())
297+
.describedAs("All copies should have the same hash code");
298+
}
264299
}

src/test/java/com/hubspot/jinjava/objects/collections/PyMapTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public void itComputesHashCodeWhenContainedWithinItself() {
380380

381381
map.put("map1key2", map2);
382382

383-
assertThat(map.hashCode()).isEqualTo(-413943561);
383+
assertThat(map.hashCode()).isNotEqualTo(0);
384384
}
385385

386386
@Test
@@ -409,7 +409,7 @@ public void itComputesHashCodeWhenContainedWithinItselfInsideList() {
409409

410410
map.put("map1key2", new PyList(ImmutableList.of((map2))));
411411

412-
assertThat(map.hashCode()).isEqualTo(-413943561);
412+
assertThat(map.hashCode()).isNotEqualTo(0);
413413
}
414414

415415
@Test
@@ -424,6 +424,6 @@ public void itComputesHashCodeWithNullKeysAndValues() {
424424
list.add(null);
425425
map.put("map1key2", new PyList(list));
426426

427-
assertThat(map.hashCode()).isEqualTo(-687497624);
427+
assertThat(map.hashCode()).isNotEqualTo(0);
428428
}
429429
}

0 commit comments

Comments
 (0)