### Question:
JavaLexed implements AutoCloseable { public int replace(MatchResult result, List<LexicalElement>... lists) { assertOpen(); if (result == null) { throw new IllegalArgumentException("Illegal sequence of invocation, there is no match result."); } if (result.matches) { return replace(result.start, result.end, lists); } return -1; } JavaLexed(Source source); @Override void close(); Iterable<LexicalElement> lexicalElements(); LexicalElement get(int i); LexicalElement remove(int i); void removeRange(int start, int end); int replaceWith(List<LexicalElement>... lists); int replace(MatchResult result, List<LexicalElement>... lists); int replace(int start, int end, List<LexicalElement>... lists); void add(int index, LexicalElement le); int size(); JavaLexed match(BiFunction<JavaLexed, Lexpression, LexMatcher> expression); JavaLexed find(BiFunction<JavaLexed, Lexpression, LexMatcher> expression); JavaLexed sensitivity(int sensitivity); JavaLexed fromStart(); JavaLexed fromIndex(int i); Optional<java.util.regex.MatchResult> regexGroups(final String name); List<javax0.geci.javacomparator.LexicalElement> group(final String name); MatchResult result();  }### Answer:
@Test @DisplayName("Can replace a range of lexical elements at the start.") void replacesRangeStart() { final var source = new TestSource("final private"); final var source3 = new TestSource("private var"); final String lexed; try(final var sut = new JavaLexed(source)) { sut.replace(0, 2, Arrays.asList(new LexicalElement.Identifier("static"), new LexicalElement.Spacing(" "))); lexed = toLexicalString(sut); } Assertions.assertEquals("IDENTIFIER[static]\n" + "SPACING[ ]\n" + "IDENTIFIER[private]\n", lexed); }
@Test @DisplayName("Can replace a range of lexical elements between the start and the end.") void replacesRangeMiddle() { final var source = new TestSource("public final static var"); final String lexed; try(final var sut = new JavaLexed(source)) { sut.replace(2, 4, Arrays.asList(new LexicalElement.Identifier("static"), new LexicalElement.Spacing(" "))); lexed = toLexicalString(sut); } Assertions.assertEquals("IDENTIFIER[public]\n" + "SPACING[ ]\n" + "IDENTIFIER[static]\n" + "SPACING[ ]\n" + "IDENTIFIER[static]\n" + "SPACING[ ]\n" + "IDENTIFIER[var]\n", lexed); }
@Test @DisplayName("Can replace a range of lexical elements at the end.") void replacesRangeEnd() { final var source = new TestSource("public final static"); final String lexed; try(final var sut = new JavaLexed(source)) { sut.replace(2, 5, Arrays.asList(new LexicalElement.Identifier("static"), new LexicalElement.Spacing(" "))); lexed = toLexicalString(sut); } Assertions.assertEquals("IDENTIFIER[public]\n" + "SPACING[ ]\n" + "IDENTIFIER[static]\n" + "SPACING[ ]\n", lexed); }
@Test @DisplayName("Can replace a range of lexical elements with the Lex utility class.") void replacesRangeWithLex() { final var source = new TestSource("final private"); final String lexed; try(final var sut = new JavaLexed(source)) { sut.replace(0, 2, Lex.of("static ")); lexed = toLexicalString(sut); } Assertions.assertEquals("IDENTIFIER[static]\n" + "SPACING[ ]\n" + "IDENTIFIER[private]\n", lexed); }