<commit_before>// Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Test that the dart2js copy of [KernelVisitor] generates the expected IR as
/// defined by kernel spec-mode test files.

import 'dart:io';
import 'dart:async';
import 'package:compiler/src/compiler.dart' show Compiler;
import 'package:compiler/src/elements/elements.dart';
import 'package:compiler/src/js_backend/backend.dart' show JavaScriptBackend;
import 'package:compiler/src/commandline_options.dart' show Flags;
import 'package:kernel/ast.dart';
import 'package:kernel/text/ast_to_text.dart';
import 'package:kernel/transformations/mixin_full_resolution.dart';
import 'package:kernel/class_hierarchy.dart';
import 'package:path/path.dart' as pathlib;
import 'package:test/test.dart';

import '../memory_compiler.dart';

const String TESTCASE_DIR = 'pkg/kernel/testcases/';

const List<String> SKIP_TESTS = const <String>[
  /// The test expects an unpatched api.
  'external',
];

main(List<String> arguments) async {

  Directory directory = new Directory('${TESTCASE_DIR}/input');
  for (FileSystemEntity file in directory.listSync()) {
    if (file is File && file.path.endsWith('.dart')) {
      String name = pathlib.basenameWithoutExtension(file.path);
      bool selected = arguments.contains(name);
      if (!selected) {
        if (arguments.isNotEmpty) continue;
        if (SKIP_TESTS.contains(name)) continue;
      }

      test(name, () async {
        var compiler = await newCompiler();
        await compiler.run(file.absolute.uri);
        LibraryElement library =
            await compiler.libraryLoader.loadLibrary(file.absolute.uri);
        JavaScriptBackend backend = compiler.backend;
        StringBuffer buffer = new StringBuffer();
        Program program = backend.kernelTask.buildProgram(library);
        new MixinFullResolution().transform(program);
        new Printer(buffer)
            .writeLibraryFile(program.mainMethod.enclosingLibrary);
        String actual = buffer.toString();
        String expected =
            new File('${TESTCASE_DIR}/spec-mode/$name.baseline.txt')
                .readAsStringSync();
        if (selected) {
          String input =
              new File('${TESTCASE_DIR}/input/$name.dart').readAsStringSync();
          print('============================================================');
          print(name);
          print('--input-----------------------------------------------------');
          print(input);
          print('--expected--------------------------------------------------');
          print(expected);
          print('--actual----------------------------------------------------');
          print(actual);
        }
        expect(actual, equals(expected));
      });
    }
  }
}

Future<Compiler> newCompiler() async {
  Compiler compiler = compilerFor(
      options: [Flags.analyzeOnly, Flags.analyzeAll, Flags.useKernel]);
  await compiler.setupSdk();

  // The visitor no longer enqueues elements that are not reachable from the
  // program. The mixin-full resolution transform run by the test expects to
  // find dart.core::Iterator.
  var core = await compiler.libraryLoader.loadLibrary(Uri.parse('dart:core'));
  var cls = core.implementation.localLookup('Iterator');
  cls.ensureResolved(compiler.resolution);
  return compiler;
}<commit_msg>Make analyzer happy about CompilerImpl use of setupSdk.<commit_after>// Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Test that the dart2js copy of [KernelVisitor] generates the expected IR as
/// defined by kernel spec-mode test files.

import 'dart:io';
import 'dart:async';
import 'package:compiler/src/compiler.dart' show Compiler;
import 'package:compiler/src/elements/elements.dart';
import 'package:compiler/src/js_backend/backend.dart' show JavaScriptBackend;
import 'package:compiler/src/commandline_options.dart' show Flags;
import 'package:kernel/ast.dart';
import 'package:kernel/text/ast_to_text.dart';
import 'package:kernel/transformations/mixin_full_resolution.dart';
import 'package:kernel/class_hierarchy.dart';
import 'package:path/path.dart' as pathlib;
import 'package:test/test.dart';

import '../memory_compiler.dart';

const String TESTCASE_DIR = 'pkg/kernel/testcases/';

const List<String> SKIP_TESTS = const <String>[
  /// The test expects an unpatched api.
  'external',
];

main(List<String> arguments) async {

  Directory directory = new Directory('${TESTCASE_DIR}/input');
  for (FileSystemEntity file in directory.listSync()) {
    if (file is File && file.path.endsWith('.dart')) {
      String name = pathlib.basenameWithoutExtension(file.path);
      bool selected = arguments.contains(name);
      if (!selected) {
        if (arguments.isNotEmpty) continue;
        if (SKIP_TESTS.contains(name)) continue;
      }

      test(name, () async {
        var compiler = await newCompiler();
        await compiler.run(file.absolute.uri);
        LibraryElement library =
            await compiler.libraryLoader.loadLibrary(file.absolute.uri);
        JavaScriptBackend backend = compiler.backend;
        StringBuffer buffer = new StringBuffer();
        Program program = backend.kernelTask.buildProgram(library);
        new MixinFullResolution().transform(program);
        new Printer(buffer)
            .writeLibraryFile(program.mainMethod.enclosingLibrary);
        String actual = buffer.toString();
        String expected =
            new File('${TESTCASE_DIR}/spec-mode/$name.baseline.txt')
                .readAsStringSync();
        if (selected) {
          String input =
              new File('${TESTCASE_DIR}/input/$name.dart').readAsStringSync();
          print('============================================================');
          print(name);
          print('--input-----------------------------------------------------');
          print(input);
          print('--expected--------------------------------------------------');
          print(expected);
          print('--actual----------------------------------------------------');
          print(actual);
        }
        expect(actual, equals(expected));
      });
    }
  }
}

Future<Compiler> newCompiler() async {
  var compiler = compilerFor(
      options: [Flags.analyzeOnly, Flags.analyzeAll, Flags.useKernel]);
  await compiler.setupSdk();

  // The visitor no longer enqueues elements that are not reachable from the
  // program. The mixin-full resolution transform run by the test expects to
  // find dart.core::Iterator.
  var core = await compiler.libraryLoader.loadLibrary(Uri.parse('dart:core'));
  var cls = core.implementation.localLookup('Iterator');
  cls.ensureResolved(compiler.resolution);
  return compiler;
}
{'fraction_non_alphanumeric': 0.1735725473444397, 'fraction_numerical': 0.0014238929232521715, 'mean_word_length': 2.823625476320087, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3825870', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1840, 'n_words': 475}