private void splitFiles(String[] input) { Compiler compiler = new Compiler(); List<SourceFile> files = Lists.newArrayList(); for (int i = 0; i < input.length; i++) { files.add(SourceFile.fromCode("file" + i, input[i])); } compiler.init(ImmutableList.<SourceFile>of(), files, new CompilerOptions()); compiler.parse(); Node original = compiler.getRoot(); Node root = original.cloneTree(); AstParallelizer parallelizer = AstParallelizer.createNewFileLevelAstParallelizer(root); List<Node> forest = parallelizer.split(); assertEquals(input.length, forest.size()); int i = 0; for (Node n : forest) { Node tree = compiler.parseTestCode(input[i++]); assertEquals(compiler.toSource(tree), compiler.toSource(n)); } parallelizer.join(); assertTrue(original.isEquivalentTo(root)); }
protected void testExternChanges(String extern, String input, String expectedExtern) { Compiler compiler = createCompiler(); CompilerOptions options = getOptions(); compiler.init( ImmutableList.of(SourceFile.fromCode("extern", extern)), ImmutableList.of(SourceFile.fromCode("input", input)), options); compiler.parseInputs(); assertFalse(compiler.hasErrors()); Node externsAndJs = compiler.getRoot(); Node root = externsAndJs.getLastChild(); Node externs = externsAndJs.getFirstChild(); Node expected = compiler.parseTestCode(expectedExtern); assertFalse(compiler.hasErrors()); (getProcessor(compiler)).process(externs, root); String externsCode = compiler.toSource(externs); String expectedCode = compiler.toSource(expected); assertEquals(expectedCode, externsCode); }
private void test(String js, FunctionInformationMap expected) { Compiler compiler = new Compiler(); compiler.init( new JSSourceFile[] {JSSourceFile.fromCode("externs", "")}, new JSSourceFile[] {JSSourceFile.fromCode("testcode", js)}, new CompilerOptions()); test(compiler, expected); }
/** * Verifies that the compiler pass's JS output matches the expected output and (optionall) that an * expected warning is issued. Or, if an error is expected, this method just verifies that the * error is encountered. * * @param inputs Inputs * @param expected Expected JS output * @param error Expected error, or null if no error is expected * @param description The description of the expected warning, or null if no warning is expected * or if the warning's description should no be examined */ public void test( List<SourceFile> inputs, String[] expected, DiagnosticType error, DiagnosticType warning, String description) { Compiler compiler = createCompiler(); lastCompiler = compiler; compiler.init(externsInputs, inputs, getOptions()); test(compiler, expected, error, warning, description); }
/** Parses expected JS inputs and returns the root of the parse tree. */ protected Node parseExpectedJs(List<SourceFile> inputs) { Compiler compiler = createCompiler(); compiler.init(externsInputs, inputs, getOptions()); Node root = compiler.parseInputs(); assertTrue( "Unexpected parse error(s): " + Joiner.on("\n").join(compiler.getErrors()), root != null); Node externsRoot = root.getFirstChild(); Node mainRoot = externsRoot.getNext(); // Only run the normalize pass, if asked. if (normalizeEnabled && normalizeExpected && !compiler.hasErrors()) { Normalize normalize = new Normalize(compiler, false); normalize.process(externsRoot, mainRoot); } return mainRoot; }
private Node parse(String[] original) { String[] argStrings = args.toArray(new String[] {}); CommandLineRunner runner = new CommandLineRunner(argStrings); Compiler compiler = runner.createCompiler(); List<SourceFile> inputs = Lists.newArrayList(); for (int i = 0; i < original.length; i++) { inputs.add(SourceFile.fromCode(getFilename(i), original[i])); } CompilerOptions options = new CompilerOptions(); // ECMASCRIPT5 is the most forgiving. options.setLanguageIn(LanguageMode.ECMASCRIPT5); compiler.init(externs, inputs, options); Node all = compiler.parseInputs(); Preconditions.checkState(compiler.getErrorCount() == 0); Preconditions.checkNotNull(all); Node n = all.getLastChild(); return n; }
/** Parses expected JS inputs and returns the root of the parse tree. */ protected Node parseExpectedJs(List<SourceFile> inputs) { Compiler compiler = createCompiler(); compiler.init(externsInputs, inputs, getOptions()); Node root = compiler.parseInputs(); assertNotNull("Unexpected parse error(s): " + Joiner.on("\n").join(compiler.getErrors()), root); Node externsRoot = root.getFirstChild(); Node mainRoot = externsRoot.getNext(); // Only run the normalize pass, if asked. if (normalizeEnabled && normalizeExpected && !compiler.hasErrors()) { Normalize normalize = new Normalize(compiler, false); normalize.process(externsRoot, mainRoot); } if (closurePassEnabled && closurePassEnabledForExpected && !compiler.hasErrors()) { new ProcessClosurePrimitives(compiler, null, CheckLevel.ERROR, false).process(null, mainRoot); } return mainRoot; }
protected void testExternChanges(String extern, String input, String expectedExtern) { Compiler compiler = createCompiler(); CompilerOptions options = getOptions(); compiler.init( ImmutableList.of(SourceFile.fromCode("extern", extern)), ImmutableList.of(SourceFile.fromCode("input", input)), options); compiler.parseInputs(); assertFalse(compiler.hasErrors()); Node externsAndJs = compiler.getRoot(); Node root = externsAndJs.getLastChild(); Node externs = externsAndJs.getFirstChild(); Node expected = compiler.parseTestCode(expectedExtern); assertFalse(compiler.hasErrors()); (getProcessor(compiler)).process(externs, root); if (compareAsTree) { // Expected output parsed without implied block. Preconditions.checkState(externs.isBlock()); Preconditions.checkState(compareJsDoc); Preconditions.checkState( externs.hasOneChild(), "Compare as tree only works when output has a single script."); externs = externs.getFirstChild(); String explanation = expected.checkTreeEqualsIncludingJsDoc(externs); assertNull( "\nExpected: " + compiler.toSource(expected) + "\nResult: " + compiler.toSource(externs) + "\n" + explanation, explanation); } else { String externsCode = compiler.toSource(externs); String expectedCode = compiler.toSource(expected); assertEquals(expectedCode, externsCode); } }
private String printHelper(String js, boolean runProcessor) { Compiler compiler = createCompiler(); CompilerOptions options = getOptions(); compiler.init( ImmutableList.<SourceFile>of(), ImmutableList.of(SourceFile.fromCode("testcode", js)), options); Node root = compiler.parseInputs(); assertNotNull( "Unexpected parse error(s): " + Joiner.on("\n").join(compiler.getErrors()) + "\nEXPR: " + js, root); Node externsRoot = root.getFirstChild(); Node mainRoot = externsRoot.getNext(); if (runProcessor) { getProcessor(compiler).process(externsRoot, mainRoot); } return compiler.toSource(mainRoot); }
/** * Verifies that the compiler pass's JS output matches the expected output and (optionally) that * an expected warning is issued. Or, if an error is expected, this method just verifies that the * error is encountered. * * @param externs Externs inputs * @param js Inputs * @param expected Expected output, or null if an error is expected * @param error Expected error, or null if no error is expected * @param warning Expected warning, or null if no warning is expected * @param description The description of the expected warning, or null if no warning is expected * or if the warning's description should not be examined */ private void test( List<SourceFile> externs, List<SourceFile> js, String expected, DiagnosticType error, DiagnosticType warning, String description) { Compiler compiler = createCompiler(); lastCompiler = compiler; CompilerOptions options = getOptions(); options.setLanguageIn(acceptedLanguage); // Note that in this context, turning on the checkTypes option won't // actually cause the type check to run. options.checkTypes = parseTypeInfo; compiler.init(externs, js, options); BaseJSTypeTestCase.addNativeProperties(compiler.getTypeRegistry()); test(compiler, maybeCreateArray(expected), error, warning, description); }
private void test(JSModule[] modules, FunctionInformationMap expected) { Compiler compiler = new Compiler(); compiler.init( new JSSourceFile[] {JSSourceFile.fromCode("externs", "")}, modules, new CompilerOptions()); test(compiler, expected); }