From b65589175cb69dd59df202a173efbe76d175797e Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 5 Jan 2024 14:48:10 +0100 Subject: [PATCH] [core] Add tests for TokenMgrError location --- .../pmd/lang/ast/TokenMgrError.java | 2 +- .../pmd/lang/ast/TokenMgrErrorTest.java | 37 +++++++++++++++++++ .../pmd/lang/java/cpd/JavaTokenizerTest.java | 23 ++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/TokenMgrErrorTest.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java index 3b6366fb45..28bee8a79e 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java @@ -27,7 +27,7 @@ public final class TokenMgrError extends FileAnalysisException { * * @param line Line number * @param column Column number - * @param filename Filename. If unknown, it can be completed with {@link #setFileName(String)} later + * @param filename Filename. If unknown, it can be completed with {@link #setFileId(FileId)}} later * @param message Message of the error * @param cause Cause of the error, if any */ diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/TokenMgrErrorTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/TokenMgrErrorTest.java new file mode 100644 index 0000000000..806d5e743e --- /dev/null +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/TokenMgrErrorTest.java @@ -0,0 +1,37 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class TokenMgrErrorTest { + @Test + void invalidLocation() { + TokenMgrError error = new TokenMgrError(2, 0, null, "test", null); + // this shouldn't throw a IllegalArgumentException + assertEquals("line 2, column 1", error.location().startPosToString()); + } + + @Test + void invalidLocationJavaCC() { + TokenMgrError error = new TokenMgrError(false, "DEFAULT", 2, 0, "}", '\n'); + // this shouldn't throw a IllegalArgumentException + assertEquals("line 2, column 1", error.location().startPosToString()); + } + + @Test + void validLocation() { + TokenMgrError error = new TokenMgrError(1, 1, null, "test", null); + assertEquals("line 1, column 1", error.location().startPosToString()); + } + + @Test + void validLocationJavaCC() { + TokenMgrError error = new TokenMgrError(false, "DEFAULT", 1, 1, "}", '\n'); + assertEquals("line 1, column 1", error.location().startPosToString()); + } +} diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/cpd/JavaTokenizerTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/cpd/JavaTokenizerTest.java index 67e211f13c..17a873fc1c 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/cpd/JavaTokenizerTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/cpd/JavaTokenizerTest.java @@ -4,12 +4,21 @@ package net.sourceforge.pmd.lang.java.cpd; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertThrows; + import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import net.sourceforge.pmd.cpd.CpdLanguageProperties; +import net.sourceforge.pmd.cpd.Tokenizer; +import net.sourceforge.pmd.cpd.Tokens; import net.sourceforge.pmd.cpd.test.CpdTextComparisonTest; import net.sourceforge.pmd.cpd.test.LanguagePropertyConfig; +import net.sourceforge.pmd.lang.ast.TokenMgrError; +import net.sourceforge.pmd.lang.document.FileId; +import net.sourceforge.pmd.lang.document.TextDocument; import net.sourceforge.pmd.lang.java.JavaLanguageModule; // TODO - enable tests @@ -29,6 +38,20 @@ class JavaTokenizerTest extends CpdTextComparisonTest { doTest("StringTemplateReduction"); } + @Test + void testLexExceptionLocation() { + Tokenizer tokenizer = newTokenizer(defaultProperties()); + Tokens tokens = new Tokens(); + TokenMgrError lexException = assertThrows(TokenMgrError.class, () -> + Tokenizer.tokenize(tokenizer, + // note: the source deliberately contains an unbalanced quote, unterminated string literal + TextDocument.readOnlyString("class F {\n String s=\"abc\";\"\n}\n", FileId.UNKNOWN, getLanguage().getDefaultVersion()), + tokens) + ); + // this shouldn't throw a IllegalArgumentException + assertThat(lexException.getMessage(), containsString("at line 3, column 1")); + } + @Test void testStringTemplateReduction2() { doTest("StringTemplateReduction2");