Skip to content

Commit

Permalink
Use generated tests instead files.
Browse files Browse the repository at this point in the history
  • Loading branch information
dikmax committed Aug 10, 2015
1 parent f3fceb1 commit 1b7b4e4
Show file tree
Hide file tree
Showing 13 changed files with 4,283 additions and 43 deletions.
14 changes: 14 additions & 0 deletions build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
library md_proc.build_file;

import 'package:source_gen/source_gen.dart';
import 'package:md_proc/generators/embed_tests_generator.dart';

void main(List<String> args) {
build(args, const [
const EmbedTestsGenerator()
], librarySearchPaths: [
'test'
]).then((msg) {
print(msg);
});
}
7 changes: 7 additions & 0 deletions lib/generators/embed_tests.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library md_proc.generators.embed_tests;

class EmbedTests {
final String path;

const EmbedTests(this.path);
}
83 changes: 83 additions & 0 deletions lib/generators/embed_tests_generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
library md_proc.generators.embed_tests_generator;

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

import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:path/path.dart' as path;
import 'package:source_gen/source_gen.dart';

import 'embed_tests.dart';


class EmbedTestsGenerator extends GeneratorForAnnotation<EmbedTests> {
static const int stateWait = 0;
static const int stateSource = 1;
static const int stateDestination = 2;

@override
final AssociatedFileSet associatedFileSet;

/// If [associatedFileSet] is not set, the default value of
/// [AssociatedFileSet.sameDirectory] is used.
const EmbedTestsGenerator(
{AssociatedFileSet associatedFileSet: AssociatedFileSet.sameDirectory})
: this.associatedFileSet = associatedFileSet;

Map<String, String> readFile(fileName) {
Map<String, String> result = <String, String>{};

File file = new File(fileName);
int state = stateWait;
List<String> destination = [];
List<String> source = [];
List<String> lines = file.readAsLinesSync();
for (String line in lines) {
if (line == ".") {
state++;
if (state == 3) {
result[source.map((line) => line + "\n").join()] = destination.map((line) => line + "\n").join();
state = stateWait;
destination = [];
source = [];
}
} else if (state == stateSource) {
source.add(line);
} else if (state == stateDestination) {
destination.add(line);
}
}

return result;
}

@override
Future<String> generateForAnnotatedElement(
Element element, EmbedTests annotation) async {
if (path.isAbsolute(annotation.path)) {
throw 'must be relative path to the source file';
}

var source = element.source as FileBasedSource;
var sourcePath = source.file.getAbsolutePath();

var sourcePathDir = path.dirname(sourcePath);

var filePath = path.join(sourcePathDir, annotation.path);

if (!await FileSystemEntity.isFile(filePath)) {
throw 'Not a file! - $filePath';
}

var content = readFile(filePath);

var result = 'final Map<String, String> _\$${element.displayName}Tests = {\n';
content.forEach((k, v) {
result += "r'''" + k + "''': r'''" + v + "''',\n";
});
result += '};\n';

return result;
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ dev_dependencies:
test: any
linter: any
dart_coveralls: any

source_gen: '>=0.4.2 <0.5.0'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions test/data/test_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
library md_proc.test.data.test_data;

import 'package:md_proc/generators/embed_tests.dart';

part 'test_data.g.dart';

@EmbedTests('spec.txt')
final Map<String, String> specification = _$specificationTests;

@EmbedTests('smart_punct.txt')
final Map<String, String> smartPunctuation = _$smartPunctuationTests;

@EmbedTests('markdownToMarkdown.txt')
final Map<String, String> markdownToMarkdown = _$markdownToMarkdownTests;

@EmbedTests('additionalMarkdownToHtml.txt')
final Map<String, String> additionalMarkdownToHtml = _$additionalMarkdownToHtmlTests;

0 comments on commit 1b7b4e4

Please sign in to comment.