### Question:
Replacements { public Replacements add(Replacement replacement) { return add(replacement, CoalescePolicy.REJECT); }  Replacements add(Replacement replacement); Replacements add(Replacement replacement, CoalescePolicy coalescePolicy); Set<Replacement> descending(); boolean isEmpty();  }### Answer:
@Test public void duplicate() { Replacements replacements = new Replacements(); replacements.add(Replacement.create(42, 43, "hello")); try { replacements.add(Replacement.create(42, 43, "goodbye")); fail(); } catch (IllegalArgumentException expected) { assertThat(expected).hasMessageThat().contains("conflicts with existing replacement"); } }
@Test public void overlap() { Replacements replacements = new Replacements(); Replacement hello = Replacement.create(2, 4, "hello"); Replacement goodbye = Replacement.create(3, 5, "goodbye"); replacements.add(hello); try { replacements.add(goodbye); fail(); } catch (IllegalArgumentException expected) { assertThat(expected) .hasMessageThat() .isEqualTo(String.format("%s overlaps with existing replacements: %s", goodbye, hello)); } }
@Test public void identicalDuplicatesOK() { Replacements replacements = new Replacements(); replacements.add(Replacement.create(42, 43, "hello")); replacements.add(Replacement.create(42, 43, "hello")); }
@Test public void zeroLengthRangeOverlaps() { Replacements replacements = new Replacements(); replacements.add(Replacement.create(1, 1, "Something")); Replacement around = Replacement.create(0, 2, "Around"); assertThrows(IllegalArgumentException.class, () -> replacements.add(around)); }