Search in sources :

Example 91 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class SourceDefinedSymbolDeclarationReferenceFinderTest method testFindReferenceOnMethodDeclaration.

@Test
void testFindReferenceOnMethodDeclaration() {
    // given
    var documentContext = TestUtils.getDocumentContextFromFile("./src/test/resources/references/SourceDefinedSymbolDeclarationReferenceFinder.bsl");
    var module = documentContext.getSymbolTree().getModule();
    var method = documentContext.getSymbolTree().getMethods().get(0);
    // when
    var optionalReference = referenceFinder.findReference(documentContext.getUri(), new Position(0, 15));
    // then
    assertThat(optionalReference).isPresent().hasValueSatisfying(reference -> assertThat(reference.getFrom()).isEqualTo(module)).hasValueSatisfying(reference -> assertThat(reference.getSymbol()).isEqualTo(method)).hasValueSatisfying(reference -> assertThat(reference.getSelectionRange()).isEqualTo(method.getSelectionRange()));
}
Also used : Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Position(org.eclipse.lsp4j.Position) Autowired(org.springframework.beans.factory.annotation.Autowired) TestUtils(com.github._1c_syntax.bsl.languageserver.util.TestUtils) Position(org.eclipse.lsp4j.Position) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 92 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class ConsoleReporterTest method report.

@Test
void report() {
    // given
    Diagnostic diagnostic = new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test");
    DocumentContext documentContext = TestUtils.getDocumentContext("");
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, Collections.singletonList(diagnostic));
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    ConsoleReporter reporter = new ConsoleReporter();
    // when
    reporter.report(analysisInfo, Path.of(sourceDir));
// then
// FIXME How test logger?
// assertThat(outContent.toString()).containsIgnoringCase("Analysis date: ");
}
Also used : FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 93 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class GenericReporterTest method report.

@Test
void report() throws IOException {
    // given
    List<Diagnostic> diagnostics = new ArrayList<>();
    var iterator = diagnosticInfos.entrySet().iterator();
    var firstInfo = iterator.next().getValue();
    var secondInfo = iterator.next().getValue();
    diagnostics.add(new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", firstInfo.getCode().getStringValue()));
    diagnostics.add(new Diagnostic(Ranges.create(0, 1, 2, 4), "message4", DiagnosticSeverity.Error, "test-source2", firstInfo.getCode().getStringValue()));
    diagnostics.add(new Diagnostic(Ranges.create(3, 1, 4, 4), "message4", DiagnosticSeverity.Error, "test-source2", secondInfo.getCode().getStringValue()));
    DocumentContext documentContext = TestUtils.getDocumentContext("");
    Location location = new Location("file:///fake-uri2.bsl", Ranges.create(0, 2, 2, 3));
    diagnostics.get(0).setRelatedInformation(Collections.singletonList(new DiagnosticRelatedInformation(location, "message")));
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, diagnostics);
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    // when
    reporter.report(analysisInfo, Path.of(sourceDir));
    // then
    ObjectMapper mapper = new ObjectMapper();
    GenericIssueReport report = mapper.readValue(file, GenericIssueReport.class);
    assertThat(report).isNotNull();
    assertThat(report.getIssues()).isNotNull();
    assertThat(report.getIssues().size()).isEqualTo(3);
    assertThat(report.getIssues().get(0).getPrimaryLocation()).isNotNull();
    assertThat(report.getIssues().get(0).getSecondaryLocations()).isNotNull();
    assertThat(report.getIssues().get(0).getSecondaryLocations().size()).isEqualTo(1);
    assertThat(report.getIssues().get(2).getRuleId()).isEqualTo(secondInfo.getCode().getStringValue());
    assertThat(report.getIssues().get(1).getSeverity()).isEqualTo(firstInfo.getSeverity().name());
}
Also used : FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) DiagnosticRelatedInformation(org.eclipse.lsp4j.DiagnosticRelatedInformation) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Location(org.eclipse.lsp4j.Location) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 94 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class ReportersAggregatorTest method report.

@Test
void report() {
    // given
    Diagnostic diagnostic = new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test");
    DocumentContext documentContext = TestUtils.getDocumentContext("");
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, Collections.singletonList(diagnostic));
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    aggregator.report(analysisInfo, Path.of(sourceDir));
// then
// FIXME How test logger?
// assertThat(outContent.toString()).containsIgnoringCase("Analysis date: ");
}
Also used : FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 95 with DocumentContext

use of com.github._1c_syntax.bsl.languageserver.context.DocumentContext in project bsl-language-server by 1c-syntax.

the class SarifReporterTest method report.

@Test
void report() throws IOException {
    // given
    configuration.getDiagnosticsOptions().getParameters().put("Typo", Either.forLeft(false));
    configuration.getDiagnosticsOptions().getParameters().put("test", Either.forLeft(true));
    configuration.getDiagnosticsOptions().getParameters().put("some", Either.forRight(Map.of("test", 1)));
    Diagnostic diagnostic = new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test");
    DocumentContext documentContext = TestUtils.getDocumentContext("");
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, Collections.singletonList(diagnostic));
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    // when
    reporter.report(analysisInfo, Path.of(sourceDir));
    // then
    ObjectMapper mapper = new ObjectMapper();
    var report = mapper.readValue(file, SarifSchema210.class);
    assertThat(report).isNotNull();
    var run = report.getRuns().get(0);
    assertThat(run.getTool().getDriver().getName()).isEqualTo("BSL Language Server");
    assertThat(run.getTool().getDriver().getRules()).hasSize(diagnosticInfos.size());
    var invocation = run.getInvocations().get(0);
    assertThat(invocation.getRuleConfigurationOverrides()).hasSizeGreaterThan(0).anyMatch(configurationOverride -> configurationOverride.getDescriptor().getId().equals("Typo") && !configurationOverride.getConfiguration().getEnabled()).anyMatch(configurationOverride -> configurationOverride.getDescriptor().getId().equals("test") && configurationOverride.getConfiguration().getEnabled()).anyMatch(configurationOverride -> configurationOverride.getDescriptor().getId().equals("some") && configurationOverride.getConfiguration().getParameters().getAdditionalProperties().get("test").equals(1));
    assertThat(run.getResults()).hasSize(1).element(0).matches(result -> result.getRuleId().equals("test")).matches(result -> result.getLevel() == Result.Level.ERROR).matches(result -> result.getMessage().getText().equals("message")).matches(result -> result.getAnalysisTarget().getUri().equals(documentContext.getUri().toString())).extracting(Result::getLocations).extracting(locations -> locations.get(0)).extracting(Location::getPhysicalLocation).extracting(PhysicalLocation::getRegion).matches(region -> region.getStartLine().equals(diagnostic.getRange().getStart().getLine() + 1)).matches(region -> region.getStartColumn().equals(diagnostic.getRange().getStart().getCharacter() + 1)).matches(region -> region.getEndLine().equals(diagnostic.getRange().getEnd().getLine() + 1)).matches(region -> region.getEndColumn().equals(diagnostic.getRange().getEnd().getCharacter() + 1));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) LocalDateTime(java.time.LocalDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) Diagnostic(org.eclipse.lsp4j.Diagnostic) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) Ranges(com.github._1c_syntax.bsl.languageserver.utils.Ranges) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) TestUtils(com.github._1c_syntax.bsl.languageserver.util.TestUtils) Path(java.nio.file.Path) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) Location(com.contrastsecurity.sarif.Location) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) File(java.io.File) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) CleanupContextBeforeClassAndAfterClass(com.github._1c_syntax.bsl.languageserver.util.CleanupContextBeforeClassAndAfterClass) Collections(java.util.Collections) PhysicalLocation(com.contrastsecurity.sarif.PhysicalLocation) Result(com.contrastsecurity.sarif.Result) SarifSchema210(com.contrastsecurity.sarif.SarifSchema210) FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PhysicalLocation(com.contrastsecurity.sarif.PhysicalLocation) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)86 Test (org.junit.jupiter.api.Test)69 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)57 Diagnostic (org.eclipse.lsp4j.Diagnostic)36 List (java.util.List)22 Position (org.eclipse.lsp4j.Position)21 SneakyThrows (lombok.SneakyThrows)17 Path (java.nio.file.Path)16 CodeAction (org.eclipse.lsp4j.CodeAction)16 TestUtils (com.github._1c_syntax.bsl.languageserver.util.TestUtils)15 Autowired (org.springframework.beans.factory.annotation.Autowired)15 Collectors (java.util.stream.Collectors)14 Range (org.eclipse.lsp4j.Range)13 ArrayList (java.util.ArrayList)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)12 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)12 LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)11 MethodSymbol (com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol)10 MDCommonModule (com.github._1c_syntax.mdclasses.mdo.MDCommonModule)10