generateTest method

void generateTest(
  1. DartOptions generatorOptions,
  2. Root root,
  3. StringSink sink, {
  4. required String dartPackageName,
  5. required String dartOutputPackageName,
})

Generates Dart source code for test support libraries based on the given AST represented by root, outputting the code to sink. sourceOutPath is the path of the generated dart code to be tested. testOutPath is where the test code will be generated.

Implementation

void generateTest(
  DartOptions generatorOptions,
  Root root,
  StringSink sink, {
  required String dartPackageName,
  required String dartOutputPackageName,
}) {
  final Indent indent = Indent(sink);
  final String sourceOutPath = generatorOptions.sourceOutPath ?? '';
  final String testOutPath = generatorOptions.testOutPath ?? '';
  _writeTestPrologue(generatorOptions, root, indent);
  _writeTestImports(generatorOptions, root, indent);
  final String relativeDartPath =
      path.Context(style: path.Style.posix).relative(
    _posixify(sourceOutPath),
    from: _posixify(path.dirname(testOutPath)),
  );
  if (!relativeDartPath.contains('/lib/')) {
    // If we can't figure out the package name or the relative path doesn't
    // include a 'lib' directory, try relative path import which only works in
    // certain (older) versions of Dart.
    // TODO(gaaclarke): We should add a command-line parameter to override this import.
    indent.writeln(
        "import '${_escapeForDartSingleQuotedString(relativeDartPath)}';");
  } else {
    final String path =
        relativeDartPath.replaceFirst(RegExp(r'^.*/lib/'), '');
    indent.writeln("import 'package:$dartOutputPackageName/$path';");
  }
  writeGeneralCodec(generatorOptions, root, indent,
      dartPackageName: dartPackageName);
  for (final AstHostApi api in root.apis.whereType<AstHostApi>()) {
    if (api.dartHostTestHandler != null) {
      final AstFlutterApi mockApi = AstFlutterApi(
        name: api.dartHostTestHandler!,
        methods: api.methods,
        documentationComments: api.documentationComments,
      );
      writeFlutterApi(
        generatorOptions,
        root,
        indent,
        mockApi,
        channelNameFunc: (Method func) =>
            makeChannelName(api, func, dartPackageName),
        isMockHandler: true,
        dartPackageName: dartPackageName,
      );
    }
  }
}