Example #1
0
  protected void doTest(@NotNull String path) throws Exception {
    File mainFile = new File(path);
    String mainFileName = FileUtil.getNameWithoutExtension(mainFile);
    IntentionAction intentionAction = createIntention(mainFile);
    List<String> sourceFilePaths = new ArrayList<String>();
    File parentDir = mainFile.getParentFile();
    extraFileLoop:
    //noinspection ForLoopThatDoesntUseLoopVariable
    for (int i = 1; true; i++) {
      for (String extension : EXTENSIONS) {
        File extraFile = new File(parentDir, mainFileName + "." + i + extension);
        if (extraFile.exists()) {
          sourceFilePaths.add(extraFile.getPath());
          continue extraFileLoop;
        }
      }
      break;
    }
    sourceFilePaths.add(path);

    Map<String, PsiFile> pathToFile =
        ContainerUtil.newMapFromKeys(
            sourceFilePaths.iterator(),
            new Convertor<String, PsiFile>() {
              @Override
              public PsiFile convert(String path) {
                try {
                  configureByFile(path);
                } catch (Exception e) {
                  throw new RuntimeException(e);
                }
                return myFile;
              }
            });

    String fileText = FileUtil.loadFile(mainFile, true);

    String minJavaVersion =
        InTextDirectivesUtils.findStringWithPrefixes(fileText, "// MIN_JAVA_VERSION: ");
    if (minJavaVersion != null && !SystemInfo.isJavaVersionAtLeast(minJavaVersion)) return;

    boolean isWithRuntime =
        InTextDirectivesUtils.findStringWithPrefixes(fileText, "// WITH_RUNTIME") != null;

    try {
      if (isWithRuntime) {
        ConfigLibraryUtil.configureKotlinRuntime(getModule(), getFullJavaJDK());
      }

      DirectiveBasedActionUtils.checkForUnexpectedErrors((JetFile) getFile());

      doTestFor(pathToFile, intentionAction, fileText);
    } finally {
      if (isWithRuntime) {
        ConfigLibraryUtil.unConfigureKotlinRuntime(getModule(), getTestProjectJdk());
      }
    }
  }
Example #2
0
  private void doTestFor(
      Map<String, PsiFile> pathToFile, final IntentionAction intentionAction, String fileText)
      throws Exception {
    String isApplicableString =
        InTextDirectivesUtils.findStringWithPrefixes(fileText, "// IS_APPLICABLE: ");
    boolean isApplicableExpected = isApplicableString == null || isApplicableString.equals("true");

    Assert.assertTrue(
        "isAvailable() for "
            + intentionAction.getClass()
            + " should return "
            + isApplicableExpected,
        isApplicableExpected == intentionAction.isAvailable(getProject(), getEditor(), getFile()));

    String intentionTextString =
        InTextDirectivesUtils.findStringWithPrefixes(fileText, "// INTENTION_TEXT: ");

    if (intentionTextString != null) {
      assertEquals("Intention text mismatch.", intentionTextString, intentionAction.getText());
    }

    String shouldFailString =
        InTextDirectivesUtils.findStringWithPrefixes(fileText, "// SHOULD_FAIL_WITH: ");

    try {
      if (isApplicableExpected) {
        ApplicationPackage.executeWriteCommand(
            getProject(),
            intentionAction.getText(),
            new Function0<Object>() {
              @Override
              public Object invoke() {
                intentionAction.invoke(getProject(), getEditor(), getFile());
                return null;
              }
            });
        // Don't bother checking if it should have failed.
        if (shouldFailString == null) {
          for (Map.Entry<String, PsiFile> entry : pathToFile.entrySet()) {
            //noinspection AssignmentToStaticFieldFromInstanceMethod
            myFile = entry.getValue();
            String canonicalPathToExpectedFile =
                PathUtil.getCanonicalPath(entry.getKey() + ".after");

            checkResultByFile(canonicalPathToExpectedFile);
          }
        }
      }
      assertNull("Expected test to fail.", shouldFailString);
    } catch (IntentionTestException e) {
      assertEquals("Failure message mismatch.", shouldFailString, e.getMessage());
    } catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
      assertEquals(
          "Failure message mismatch.", shouldFailString, StringUtil.join(e.getMessages(), ", "));
    }
  }
Example #3
0
  protected static String getTypeTextFromFile() {
    String text = getEditor().getDocument().getText();

    String[] directives = InTextDirectivesUtils.findArrayWithPrefixes(text, TYPE_DIRECTIVE_PREFIX);
    assertEquals(
        "One directive with \"" + TYPE_DIRECTIVE_PREFIX + "\" expected", 1, directives.length);

    return StringUtil.unquoteString(directives[0]);
  }