<commit_before>library source_gen.test.project_generator_test;

import 'dart:async';
import 'dart:io';

import 'package:scheduled_test/descriptor.dart' as d;
import 'package:scheduled_test/scheduled_test.dart';
import 'package:path/path.dart' as p;

import 'package:source_gen/src/build_file_generator.dart';
import 'package:source_gen/json_serial/json_generator.dart';

import 'test_utils.dart';

void main() {
  test('Simple test of JsonGenerator', () {
    String projectPath;

    schedule(() async {
      var dir = await _createTempDir();

      d.defaultRoot = dir.path;

      projectPath = await _createPackageStub('pkg');
    });

    schedule(() async {
      var filePath = p.join(projectPath, 'lib', 'person.dart');
      var output =
          await generate(getPackagePath(), filePath, [const JsonGenerator()]);

      expect(output, isNotNull);
      expect(output, isNotEmpty);
    });

    schedule(() async {
      d
          .dir('pkg', [
        d.dir('lib', [
          d.file('person.dart', _personContent),
          d.matcherFile('person.g.dart', isNotEmpty)
        ])
      ])
          .validate();
    });
  });
}

/// Creates a package using [pkgName] an the current [d.defaultRoot].
Future<String> _createPackageStub(String pkgName) async {
  await d
      .dir(pkgName, [d.dir('lib', [d.file('person.dart', _personContent)])])
      .create();

  var pkgPath = p.join(d.defaultRoot, pkgName);
  var exists = await FileSystemEntity.isDirectory(pkgPath);

  assert(exists);

  return pkgPath;
}

Future<Directory> _createTempDir([bool scheduleDelete = true]) async {
  var ticks = new DateTime.now().toUtc().millisecondsSinceEpoch;
  var dir = await Directory.systemTemp.createTemp('source_gen.test.$ticks.');

  currentSchedule.onComplete.schedule(() {
    if (scheduleDelete) {
      return dir.delete(recursive: true);
    } else {
      print('Not deleting $dir');
    }
  });

  return dir;
}

const _personContent = r'''library source_gen.example.person;

import 'package:source_gen/json_serial/json_annotation.dart';

@JsonSerializable()
class Person extends Object with _$PersonSerializerMixin {
  String firstName, middleName, lastName;
  DateTime dob;

  Person();
}
''';<commit_msg>Making generator test generic using a test generator<commit_after>library source_gen.test.project_generator_test;

import 'dart:async';
import 'dart:io';

import 'package:analyzer/src/generated/element.dart';
import 'package:path/path.dart' as p;
import 'package:scheduled_test/descriptor.dart' as d;
import 'package:scheduled_test/scheduled_test.dart';
import 'package:source_gen/src/build_file_generator.dart';
import 'package:source_gen/src/generator.dart';

import 'test_utils.dart';

void main() {
  test('Simple Generator test', () {
    String projectPath;

    schedule(() async {
      var dir = await _createTempDir();

      d.defaultRoot = dir.path;

      projectPath = await _createPackageStub('pkg');
    });

    schedule(() async {
      var filePath = p.join(projectPath, 'lib', 'person.dart');
      var output =
          await generate(getPackagePath(), filePath, [const _TestGenerator()]);

      expect(output, isNotNull);
      expect(output, isNotEmpty);
    });

    schedule(() async {
      d
          .dir('pkg', [
        d.dir('lib', [
          d.file('person.dart', _testLibContent),
          d.matcherFile('person.g.dart', contains(_testGenPartContent))
        ])
      ])
          .validate();
    });
  });
}

/// Creates a package using [pkgName] an the current [d.defaultRoot].
Future<String> _createPackageStub(String pkgName) async {
  await d
      .dir(pkgName, [d.dir('lib', [d.file('person.dart', _testLibContent)])])
      .create();

  var pkgPath = p.join(d.defaultRoot, pkgName);
  var exists = await FileSystemEntity.isDirectory(pkgPath);

  assert(exists);

  return pkgPath;
}

Future<Directory> _createTempDir([bool scheduleDelete = true]) async {
  var ticks = new DateTime.now().toUtc().millisecondsSinceEpoch;
  var dir = await Directory.systemTemp.createTemp('source_gen.test.$ticks.');

  currentSchedule.onComplete.schedule(() {
    if (scheduleDelete) {
      return dir.delete(recursive: true);
    } else {
      print('Not deleting $dir');
    }
  });

  return dir;
}

class _TestGenerator extends Generator {
  const _TestGenerator();

  String generate(Element element) {
    return '// Code for $element';
  }

  String toString() => 'TestGenerator';
}

const _testLibContent = r'''library test_lib;
final int foo = 42;

class Person extends Object with _$PersonSerializerMixin {
  String firstName, middleName, lastName;
  DateTime dob;

  Person();
}
''';

const _testGenPartContent = r'''part of test_lib;

// **************************************************************************
// Generator: TestGenerator
// Target: final int foo
// **************************************************************************

// Code for int foo

// **************************************************************************
// Generator: TestGenerator
// Target: class Person
// **************************************************************************

// Code for Person
''';
{'fraction_non_alphanumeric': 0.18869750338687827, 'fraction_numerical': 0.0003870718018192375, 'mean_word_length': 3.8617121354656634, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '5265044', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1529, 'n_words': 338}