Rewrite tests to use groups

master
Oystein Kristoffer Tveit 2020-06-26 11:44:46 +02:00
parent 093ae02c56
commit 1db585cc8b
1 changed files with 42 additions and 15 deletions

View File

@ -1,29 +1,56 @@
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as path;
import 'dart:convert';
import 'package:unofficial_jisho_api/api.dart';
import 'package:test/test.dart';
List<String> getFilePaths(String dirname) {
final currentdir = Directory.current.path;
final filenames = Directory(path.join(currentdir, 'test', dirname)).listSync();
return filenames.map((filename) => path.join(currentdir, 'test', dirname, filename.path)).toList();
final testDir = path.join(currentdir, 'test', dirname);
final filenames = Directory(testDir).listSync();
return filenames.map((filename) => path.join(testDir, filename.path));
}
void runTestCases(List<String> testCaseFiles, Function apiFunction) async {
List getTestCases(String directoryName) {
final testCaseFiles = getFilePaths(directoryName);
var result = [];
for (var testCount = 0; testCount < testCaseFiles.length; testCount++) {
final file = await File(testCaseFiles[testCount]).readAsString();
final testCase = jsonDecode(file);
await test('Test ${testCount}', () async {
final result = await apiFunction(testCase['query']);
expect(jsonEncode(result), jsonEncode(testCase));
});
final file = File(testCaseFiles[testCount]).readAsStringSync();
result.add(jsonDecode(file));
}
return result;
}
void main() async {
await runTestCases(getFilePaths('kanji_test_cases'), searchForKanji);
await runTestCases(getFilePaths('example_test_cases'), searchForExamples);
await runTestCases(getFilePaths('phrase_scrape_test_cases'), scrapeForPhrase);
}
void main() {
group('searchForKanji', () {
final testCases = getTestCases('kanji_test_cases');
for (var testCase in testCases) {
test('Query "${testCase['query']}"', () async {
final result = await searchForKanji(testCase['query']);
expect(jsonEncode(result), jsonEncode(testCase));
});
}
});
group('searchForExamples', () {
final testCases = getTestCases('example_test_cases');
for (var testCase in testCases) {
test('Query "${testCase['query']}"', () async {
final result = await searchForExamples(testCase['query']);
expect(jsonEncode(result), jsonEncode(testCase));
});
}
});
group('scrapeForPhrase', () {
final testCases = getTestCases('phrase_scrape_test_cases');
for (var testCase in testCases) {
test('Query "${testCase['query']}"', () async {
final result = await scrapeForPhrase(testCase['query']);
expect(jsonEncode(result), jsonEncode(testCase));
});
}
});
}