|
6 | 6 | import com.hubspot.jinjava.interpret.IndexOutOfRangeException; |
7 | 7 | import com.hubspot.jinjava.interpret.RenderResult; |
8 | 8 | import com.hubspot.jinjava.interpret.TemplateError; |
| 9 | +import java.util.ArrayList; |
9 | 10 | import java.util.Collections; |
10 | 11 | import org.junit.Test; |
11 | 12 |
|
@@ -261,4 +262,38 @@ public void itDisallowsAppendingSelf() { |
261 | 262 | ) |
262 | 263 | .isEqualTo("[1, 2]"); |
263 | 264 | } |
| 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 | + } |
264 | 299 | } |
0 commit comments