diff --git a/tool/test/org/antlr/v4/test/TestTokenStreamRewriter.java b/tool/test/org/antlr/v4/test/TestTokenStreamRewriter.java
index acac405ca..99edd508e 100644
--- a/tool/test/org/antlr/v4/test/TestTokenStreamRewriter.java
+++ b/tool/test/org/antlr/v4/test/TestTokenStreamRewriter.java
@@ -881,4 +881,25 @@ public class TestTokenStreamRewriter extends BaseTest {
assertEquals(expecting, result);
}
+ // Test for https://github.com/antlr/antlr4/issues/550
+ @Test public void testPreservesOrderOfContiguousInserts() throws Exception {
+ LexerGrammar g = new LexerGrammar(
+ "lexer grammar T;\n"+
+ "A : 'a';\n" +
+ "B : 'b';\n" +
+ "C : 'c';\n");
+ String input = "aa";
+ LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
+ CommonTokenStream stream = new CommonTokenStream(lexEngine);
+ stream.fill();
+ TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
+ tokens.insertBefore(0, "");
+ tokens.insertAfter(0, "");
+ tokens.insertBefore(1, "");
+ tokens.insertAfter(1, "");
+ String result = tokens.getText();
+ String expecting = "aa"; // fails with aa"
+ assertEquals(expecting, result);
+ }
+
}