[core] Add tests for TokenMgrError location

This commit is contained in:
Andreas Dangel
2024-01-05 14:48:10 +01:00
parent 6255274659
commit b65589175c
3 changed files with 61 additions and 1 deletions

View File

@ -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
*/

View File

@ -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());
}
}

View File

@ -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");