| /*=============================================================================# |
| # Copyright (c) 2020 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.r.core.sourcemodel; |
| |
| import static org.junit.jupiter.api.Assertions.assertEquals; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.jface.text.Document; |
| |
| import org.junit.jupiter.api.Test; |
| |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.text.core.input.StringParserInput; |
| |
| import org.eclipse.statet.ltk.core.Ltk; |
| import org.eclipse.statet.ltk.core.SourceContent; |
| import org.eclipse.statet.ltk.core.WorkingContext; |
| import org.eclipse.statet.ltk.core.input.BasicSourceFragment; |
| import org.eclipse.statet.ltk.issues.core.Problem; |
| import org.eclipse.statet.ltk.issues.core.ProblemRequestor; |
| import org.eclipse.statet.ltk.model.core.ModelManager; |
| import org.eclipse.statet.r.core.model.RFragmentSourceUnit; |
| import org.eclipse.statet.r.core.model.RSourceUnit; |
| import org.eclipse.statet.r.core.rsource.RSourceConstants; |
| import org.eclipse.statet.r.core.rsource.ast.RParser; |
| import org.eclipse.statet.r.core.rsource.ast.SourceComponent; |
| |
| |
| @NonNullByDefault |
| public class SyntaxProblemReporterTest { |
| |
| |
| private final RParser rParser= new RParser(ModelManager.MODEL_FILE); |
| |
| private final SyntaxProblemReporter problemReporter= new SyntaxProblemReporter(); |
| |
| |
| public SyntaxProblemReporterTest() { |
| } |
| |
| |
| private RSourceUnit createSourceUnit(final String source) { |
| final BasicSourceFragment fragment= new BasicSourceFragment("test", "Test", "Test", |
| new Document(source) ); |
| return new RFragmentSourceUnit("test", fragment) { |
| @Override |
| public WorkingContext getWorkingContext() { |
| return Ltk.PERSISTENCE_CONTEXT; |
| } |
| |
| }; |
| } |
| |
| private List<Problem> collectProblems(final String source) { |
| final RSourceUnit su= createSourceUnit(source); |
| final SourceContent sourceContent= su.getContent(new NullProgressMonitor()); |
| final SourceComponent sourceComponent= this.rParser.scanSourceUnit( |
| new StringParserInput(sourceContent.getText()).init() ); |
| final List<Problem> collectedProblems= new ArrayList<>(); |
| this.problemReporter.run(su, sourceContent, sourceComponent, new ProblemRequestor() { |
| @Override |
| public void acceptProblems(final String categoryId, final List<Problem> problems) { |
| collectedProblems.addAll(problems); |
| } |
| @Override |
| public void acceptProblems(final Problem problem) { |
| collectedProblems.add(problem); |
| } |
| @Override |
| public void finish() { |
| } |
| }); |
| return collectedProblems; |
| } |
| |
| private void assertProblem(final int expectedSeverity, final int expectedCode, final String expectedMessage, |
| final int expectedStartOffset, final int expectedEndOffset, |
| final Problem actual) { |
| assertEquals(expectedSeverity, actual.getSeverity()); |
| assertEquals(expectedCode, actual.getCode()); |
| assertEquals(expectedMessage, actual.getMessage()); |
| assertEquals(expectedStartOffset, actual.getSourceStartOffset()); |
| assertEquals(expectedEndOffset, actual.getSourceEndOffset()); |
| } |
| |
| |
| @Test |
| public void StringR_incompleteOpening() { |
| List<Problem> problems; |
| |
| problems= collectProblems(" r\" + 2"); |
| assertEquals(1, problems.size()); |
| assertProblem(Problem.SEVERITY_ERROR, RSourceConstants.STATUS12_SYNTAX_TOKEN_OPENING_INCOMPLETE, |
| "Syntax Error/Invalid String: the opening delimiter for raw string literal is incomplete.", |
| 3, 5, |
| problems.get(0) ); |
| |
| // with dashes |
| problems= collectProblems(" R\'---"); |
| assertEquals(1, problems.size()); |
| assertProblem(Problem.SEVERITY_ERROR, RSourceConstants.STATUS12_SYNTAX_TOKEN_OPENING_INCOMPLETE, |
| "Syntax Error/Invalid String: the opening delimiter for raw string literal is incomplete.", |
| 3, 8, |
| problems.get(0) ); |
| } |
| |
| @Test |
| public void StringR_notClosed() { |
| List<Problem> problems; |
| |
| problems= collectProblems(" r\"(abc)"); |
| assertEquals(1, problems.size()); |
| assertProblem(Problem.SEVERITY_ERROR, RSourceConstants.STATUS12_SYNTAX_TOKEN_NOT_CLOSED, |
| "Syntax Error/Unclosed String: the raw string literal \u203A\u200Aabc)\u200A\u2039 is not closed; delimiter \u203A\u200A)\"\u200A\u2039 expected.", |
| 3, 10, |
| problems.get(0) ); |
| |
| // with dashes |
| problems= collectProblems(" R\'---{abc"); |
| assertEquals(1, problems.size()); |
| assertProblem(Problem.SEVERITY_ERROR, RSourceConstants.STATUS12_SYNTAX_TOKEN_NOT_CLOSED, |
| "Syntax Error/Unclosed String: the raw string literal \u203A\u200Aabc\u200A\u2039 is not closed; delimiter \u203A\u200A}---'\u200A\u2039 expected.", |
| 3, 12, |
| problems.get(0) ); |
| |
| // escaped cite |
| problems= collectProblems(" R\'---{a\tbc"); |
| assertEquals(1, problems.size()); |
| assertProblem(Problem.SEVERITY_ERROR, RSourceConstants.STATUS12_SYNTAX_TOKEN_NOT_CLOSED, |
| "Syntax Error/Unclosed String: the raw string literal \u203A\u200Aa\\tbc\u200A\u2039 is not closed; delimiter \u203A\u200A}---'\u200A\u2039 expected.", |
| 3, 13, |
| problems.get(0) ); |
| } |
| |
| @Test |
| public void StringR_invalidText() { |
| List<Problem> problems; |
| |
| // null char |
| problems= collectProblems(" r\"(ab\u0000c)\" "); |
| assertEquals(1, problems.size()); |
| assertProblem(Problem.SEVERITY_ERROR, RSourceConstants.STATUS123_SYNTAX_TEXT_NULLCHAR, |
| "Syntax Error/Invalid Text: null character is not allowed.", |
| 8, 9, |
| problems.get(0) ); |
| } |
| |
| } |