Rewrite tests to use groups

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