Search in sources :

Example 1 with ScalaFile

use of org.jetbrains.plugins.scala.lang.psi.api.ScalaFile in project intellij by bazelbuild.

the class BlazeScalaMainClassRunConfigurationProducer method getMainObject.

@Nullable
private static ScObject getMainObject(ConfigurationContext context) {
    Location location = context.getLocation();
    if (location == null) {
        return null;
    }
    location = JavaExecutionUtil.stepIntoSingleClass(context.getLocation());
    if (location == null) {
        return null;
    }
    PsiElement element = location.getPsiElement();
    if (!(element.getContainingFile() instanceof ScalaFile)) {
        return null;
    }
    if (!element.isPhysical()) {
        return null;
    }
    return getMainObjectFromElement(element);
}
Also used : ScalaFile(org.jetbrains.plugins.scala.lang.psi.api.ScalaFile) PsiElement(com.intellij.psi.PsiElement) Location(com.intellij.execution.Location) Nullable(javax.annotation.Nullable)

Example 2 with ScalaFile

use of org.jetbrains.plugins.scala.lang.psi.api.ScalaFile in project intellij by bazelbuild.

the class BlazeScalaSpecs2TestExprConfigurationProducerTest method testSpecs2TestProducedFromPsiClass.

@Test
public void testSpecs2TestProducedFromPsiClass() {
    PsiFile file = createTestPsiFile();
    assertThat(file).isInstanceOf(ScalaFile.class);
    ScalaFile scalaFile = (ScalaFile) file;
    PsiClass[] classes = scalaFile.getClasses();
    assertThat(classes).isNotEmpty();
    PsiClass testClass = classes[0];
    MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
    builder.setTargetMap(TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setKind("scala_junit_test").setLabel("//scala/com/google/test:TestClass").addSource(sourceRoot("scala/com/google/test/TestClass.scala")).build()).build());
    registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));
    ConfigurationContext context = createContextFromPsi(testClass);
    List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
    assertThat(configurations).isNotNull();
    assertThat(configurations).hasSize(1);
    ConfigurationFromContext fromContext = configurations.get(0);
    assertThat(fromContext.isProducedBy(BlazeJavaTestClassConfigurationProducer.class)).isTrue();
    assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);
    BlazeCommandRunConfiguration config = (BlazeCommandRunConfiguration) fromContext.getConfiguration();
    assertThat(config.getTarget()).isEqualTo(TargetExpression.fromStringSafe("//scala/com/google/test:TestClass"));
    assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass#");
    assertThat(config.getName()).isEqualTo("Blaze test TestClass");
    assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
// TODO: add tests for infix expression run configurations
// TODO: also test BlazeScalaTestEventsHandler
}
Also used : ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) ScalaFile(org.jetbrains.plugins.scala.lang.psi.api.ScalaFile) MockBlazeProjectDataBuilder(com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Test(org.junit.Test)

Example 3 with ScalaFile

use of org.jetbrains.plugins.scala.lang.psi.api.ScalaFile in project intellij by bazelbuild.

the class BlazeScalaTestClassConfigurationProducerTest method testJunitTestProducedFromPsiClass.

@Test
public void testJunitTestProducedFromPsiClass() {
    PsiFile file = createAndIndexFile(new WorkspacePath("scala/com/google/test/TestClass.scala"), "package com.google.test {", "  class TestClass {", "    @org.junit.Test", "    def testMethod() {}", "  }", "}", "package org.junit { trait Test }");
    assertThat(file).isInstanceOf(ScalaFile.class);
    ScalaFile scalaFile = (ScalaFile) file;
    PsiClass[] classes = scalaFile.getClasses();
    assertThat(classes).isNotEmpty();
    PsiClass testClass = classes[0];
    MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
    builder.setTargetMap(TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setKind("scala_junit_test").setLabel("//scala/com/google/test:TestClass").addSource(sourceRoot("scala/com/google/test/TestClass.scala")).build()).build());
    registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));
    ConfigurationContext context = createContextFromPsi(testClass);
    List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
    assertThat(configurations).isNotNull();
    assertThat(configurations).hasSize(1);
    ConfigurationFromContext fromContext = configurations.get(0);
    assertThat(fromContext.isProducedBy(BlazeJavaTestClassConfigurationProducer.class)).isTrue();
    assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);
    BlazeCommandRunConfiguration config = (BlazeCommandRunConfiguration) fromContext.getConfiguration();
    assertThat(config.getTarget()).isEqualTo(TargetExpression.fromStringSafe("//scala/com/google/test:TestClass"));
    assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass#");
    assertThat(config.getName()).isEqualTo("Blaze test TestClass");
    assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) ScalaFile(org.jetbrains.plugins.scala.lang.psi.api.ScalaFile) MockBlazeProjectDataBuilder(com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Test(org.junit.Test)

Example 4 with ScalaFile

use of org.jetbrains.plugins.scala.lang.psi.api.ScalaFile in project intellij by bazelbuild.

the class BlazeScalaTestClassConfigurationProducerTest method testScalaTestProducedFromPsiClass.

@Test
public void testScalaTestProducedFromPsiClass() {
    PsiFile file = createAndIndexFile(WorkspacePath.createIfValid("scala/com/google/test/TestClass.scala"), "package com.google.test {", "  class TestClass extends org.scalatest.FlatSpec {", "    \"this test\" should \"pass\" in {}", "  }", "}", "package org.scalatest {", "  trait FlatSpec extends Suite", "  trait Suite", "}");
    assertThat(file).isInstanceOf(ScalaFile.class);
    ScalaFile scalaFile = (ScalaFile) file;
    PsiClass[] classes = scalaFile.getClasses();
    assertThat(classes).isNotEmpty();
    PsiClass testClass = classes[0];
    MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
    builder.setTargetMap(TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setKind("scala_test").setLabel("//scala/com/google/test:TestClass").addSource(sourceRoot("scala/com/google/test/TestClass.scala")).build()).build());
    registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));
    ConfigurationContext context = createContextFromPsi(testClass);
    List<ConfigurationFromContext> configurations = context.getConfigurationsFromContext();
    assertThat(configurations).isNotNull();
    assertThat(configurations).hasSize(1);
    ConfigurationFromContext fromContext = configurations.get(0);
    assertThat(fromContext.isProducedBy(BlazeScalaTestClassConfigurationProducer.class)).isTrue();
    assertThat(fromContext.getConfiguration()).isInstanceOf(BlazeCommandRunConfiguration.class);
    BlazeCommandRunConfiguration config = (BlazeCommandRunConfiguration) fromContext.getConfiguration();
    assertThat(config.getTarget()).isEqualTo(TargetExpression.fromStringSafe("//scala/com/google/test:TestClass"));
    assertThat(getTestFilterContents(config)).isEqualTo("--test_filter=com.google.test.TestClass");
    assertThat(config.getName()).isEqualTo("Blaze test TestClass");
    assertThat(getCommandType(config)).isEqualTo(BlazeCommandName.TEST);
}
Also used : ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) ScalaFile(org.jetbrains.plugins.scala.lang.psi.api.ScalaFile) MockBlazeProjectDataBuilder(com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Test(org.junit.Test)

Aggregations

ScalaFile (org.jetbrains.plugins.scala.lang.psi.api.ScalaFile)4 MockBlazeProjectDataBuilder (com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder)3 MockBlazeProjectDataManager (com.google.idea.blaze.base.model.MockBlazeProjectDataManager)3 BlazeCommandRunConfiguration (com.google.idea.blaze.base.run.BlazeCommandRunConfiguration)3 ConfigurationContext (com.intellij.execution.actions.ConfigurationContext)3 ConfigurationFromContext (com.intellij.execution.actions.ConfigurationFromContext)3 PsiClass (com.intellij.psi.PsiClass)3 PsiFile (com.intellij.psi.PsiFile)3 Test (org.junit.Test)3 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)1 Location (com.intellij.execution.Location)1 PsiElement (com.intellij.psi.PsiElement)1 Nullable (javax.annotation.Nullable)1