Search in sources :

Example 1 with RefasterJsScanner

use of com.google.javascript.refactoring.RefasterJsScanner in project closure-compiler by google.

the class RefasterJsTestUtils method assertFileRefactoring.

/**
 * Performs refactoring using a RefasterJs template and asserts that result is as expected.
 *
 * @param refasterJsTemplate path of the file or resource containing the RefasterJs template to
 *     apply
 * @param testDataPathPrefix path prefix of the directory from which input and expected-output
 *     file will be read
 * @param originalFile file name of the JavaScript source file to apply the refaster template to
 * @param additionalSourceFiles list of additional source files to provide to the compiler (e.g.
 *     dependencies)
 * @param expectedFileChoices the expected result options of applying the specified template to
 *     {@code originalFile}
 * @throws IOException
 */
public static void assertFileRefactoring(String refasterJsTemplate, String testDataPathPrefix, String originalFile, List<String> additionalSourceFiles, String... expectedFileChoices) throws IOException {
    RefasterJsScanner scanner = new RefasterJsScanner();
    scanner.loadRefasterJsTemplate(refasterJsTemplate);
    final String originalFilePath = testDataPathPrefix + File.separator + originalFile;
    ImmutableList.Builder<String> expectedCodeBuilder = ImmutableList.builder();
    for (String expectedFile : expectedFileChoices) {
        expectedCodeBuilder.add(slurpFile(testDataPathPrefix + File.separator + expectedFile));
    }
    final ImmutableList<String> expectedCode = expectedCodeBuilder.build();
    RefactoringDriver.Builder driverBuilder = new RefactoringDriver.Builder().addExterns(CommandLineRunner.getBuiltinExterns(CompilerOptions.Environment.BROWSER));
    for (String additionalSource : additionalSourceFiles) {
        driverBuilder.addInputsFromFile(testDataPathPrefix + File.separator + additionalSource);
    }
    RefactoringDriver driver = driverBuilder.addInputsFromFile(originalFilePath).build();
    List<SuggestedFix> fixes = driver.drive(scanner);
    assertThat(driver.getCompiler().getErrors()).isEmpty();
    assertThat(driver.getCompiler().getWarnings()).isEmpty();
    ImmutableList<String> newCode = ApplySuggestedFixes.applyAllSuggestedFixChoicesToCode(fixes, ImmutableMap.of(originalFilePath, slurpFile(originalFilePath))).stream().map(m -> m.get(originalFilePath)).collect(ImmutableList.toImmutableList());
    assertThat(newCode).comparingElementsUsing(new IgnoringWhitespaceCorrespondence()).containsExactlyElementsIn(expectedCode);
}
Also used : ApplySuggestedFixes(com.google.javascript.refactoring.ApplySuggestedFixes) RefasterJsScanner(com.google.javascript.refactoring.RefasterJsScanner) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) ImmutableMap(com.google.common.collect.ImmutableMap) IOException(java.io.IOException) Truth.assertThat(com.google.common.truth.Truth.assertThat) SuggestedFix(com.google.javascript.refactoring.SuggestedFix) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Correspondence(com.google.common.truth.Correspondence) List(java.util.List) CommandLineRunner(com.google.javascript.jscomp.CommandLineRunner) ImmutableList(com.google.common.collect.ImmutableList) Files(com.google.common.io.Files) RefactoringDriver(com.google.javascript.refactoring.RefactoringDriver) RefactoringDriver(com.google.javascript.refactoring.RefactoringDriver) SuggestedFix(com.google.javascript.refactoring.SuggestedFix) ImmutableList(com.google.common.collect.ImmutableList) RefasterJsScanner(com.google.javascript.refactoring.RefasterJsScanner)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Files (com.google.common.io.Files)1 Correspondence (com.google.common.truth.Correspondence)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 CommandLineRunner (com.google.javascript.jscomp.CommandLineRunner)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 ApplySuggestedFixes (com.google.javascript.refactoring.ApplySuggestedFixes)1 RefactoringDriver (com.google.javascript.refactoring.RefactoringDriver)1 RefasterJsScanner (com.google.javascript.refactoring.RefasterJsScanner)1 SuggestedFix (com.google.javascript.refactoring.SuggestedFix)1 File (java.io.File)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 List (java.util.List)1