From 10774f6e05bd54e1b505d02ab7ad2e20f5546d00 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 3 Nov 2014 10:32:18 +0100 Subject: [PATCH] Use LexerTestCase instead of custom lexer test --- .../intellij/lang/regexp/RegExpLexerTest.java | 40 +++++++++++-------- .../intellij/testFramework/LexerTestCase.java | 9 ++++- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/RegExpSupport/test/org/intellij/lang/regexp/RegExpLexerTest.java b/RegExpSupport/test/org/intellij/lang/regexp/RegExpLexerTest.java index 5244a979ec40..8650991bd669 100644 --- a/RegExpSupport/test/org/intellij/lang/regexp/RegExpLexerTest.java +++ b/RegExpSupport/test/org/intellij/lang/regexp/RegExpLexerTest.java @@ -16,40 +16,48 @@ package org.intellij.lang.regexp; import com.intellij.lexer.Lexer; -import junit.framework.TestCase; +import com.intellij.testFramework.LexerTestCase; import java.util.EnumSet; /** * @author Bas Leijdekkers */ -public class RegExpLexerTest extends TestCase { +public class RegExpLexerTest extends LexerTestCase { public void testAmpersand() { final RegExpLexer lexer = new RegExpLexer(EnumSet.noneOf(RegExpCapability.class)); - doTest("[a&&]", lexer, "CLASS_BEGIN([) CHARACTER(a) CHARACTER(&) CHARACTER(&) CLASS_END(])"); + doTest("[a&&]", "CLASS_BEGIN ('[')\n" + + "CHARACTER ('a')\n" + + "CHARACTER ('&')\n" + + "CHARACTER ('&')\n" + + "CLASS_END (']')", lexer); } public void testIntersection() { final RegExpLexer lexer = new RegExpLexer(EnumSet.of(RegExpCapability.NESTED_CHARACTER_CLASSES)); - doTest("[a&&]", lexer, "CLASS_BEGIN([) CHARACTER(a) ANDAND(&&) CLASS_END(])"); + doTest("[a&&]", "CLASS_BEGIN ('[')\n" + + "CHARACTER ('a')\n" + + "ANDAND ('&&')\n" + + "CLASS_END (']')", lexer); } public void testPosixBracketExpression() { final RegExpLexer lexer = new RegExpLexer(EnumSet.of(RegExpCapability.POSIX_BRACKET_EXPRESSIONS)); - doTest("[[:xdigit:]]", lexer, "CLASS_BEGIN([) BRACKET_EXPRESSION_BEGIN([:) NAME(xdigit) BRACKET_EXPRESSION_END(:]) CLASS_END(])"); + doTest("[[:xdigit:]]", "CLASS_BEGIN ('[')\n" + + "BRACKET_EXPRESSION_BEGIN ('[:')\n" + + "NAME ('xdigit')\n" + + "BRACKET_EXPRESSION_END (':]')\n" + + "CLASS_END (']')", lexer); } - public static void doTest(String text, Lexer lexer, String expectedTokens) { - lexer.start(text); - final StringBuilder actualTokens = new StringBuilder(); - boolean first = true; - while (lexer.getTokenType() != null) { - if (first) first = false; - else actualTokens.append(" "); - actualTokens.append(lexer.getTokenType()).append('(').append(lexer.getTokenText()).append(')'); - lexer.advance(); - } - assertEquals(expectedTokens, actualTokens.toString()); + @Override + protected Lexer createLexer() { + return null; + } + + @Override + protected String getDirPath() { + return ""; } } diff --git a/platform/testFramework/src/com/intellij/testFramework/LexerTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LexerTestCase.java index e2d7475f1afd..fbac6befe19b 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LexerTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LexerTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.tree.IElementType; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; @@ -37,7 +38,11 @@ public abstract class LexerTestCase extends UsefulTestCase { } protected void doTest(@NonNls String text, @Nullable String expected) { - String result = printTokens(text, 0); + doTest(text, expected, createLexer()); + } + + protected void doTest(@NonNls String text, @Nullable String expected, @NotNull Lexer lexer) { + String result = printTokens(text, 0, lexer); if (expected != null) { assertSameLines(expected, result);