2020-03-12 10:56:52 +01:00
|
|
|
import 'dart:io';
|
2020-03-08 21:11:32 +01:00
|
|
|
import 'package:path/path.dart' as path;
|
|
|
|
import 'dart:convert';
|
|
|
|
|
2020-06-25 22:42:16 +02:00
|
|
|
import 'package:unofficial_jisho_api/api.dart';
|
2020-03-08 21:11:32 +01:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
List<String> getFilePaths(String dirname) {
|
2020-03-12 10:56:52 +01:00
|
|
|
final currentdir = Directory.current.path;
|
|
|
|
final filenames = Directory(path.join(currentdir, 'test', dirname)).listSync();
|
2020-03-08 21:11:32 +01:00
|
|
|
return filenames.map((filename) => path.join(currentdir, 'test', dirname, filename.path)).toList();
|
|
|
|
}
|
|
|
|
|
2020-06-14 12:48:53 +02:00
|
|
|
void runTestCases(List<String> testCaseFiles, Function apiFunction) async {
|
2020-03-08 21:11:32 +01:00
|
|
|
for (var testCount = 0; testCount < testCaseFiles.length; testCount++) {
|
2020-03-12 10:56:52 +01:00
|
|
|
final file = await File(testCaseFiles[testCount]).readAsString();
|
2020-03-08 21:11:32 +01:00
|
|
|
final testCase = jsonDecode(file);
|
2020-03-12 10:56:52 +01:00
|
|
|
await test('Test ${testCount}', () async {
|
2020-06-14 12:48:53 +02:00
|
|
|
final result = await apiFunction(testCase['query']);
|
2020-06-23 12:04:31 +02:00
|
|
|
expect(jsonEncode(result), jsonEncode(testCase));
|
2020-03-08 21:11:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 10:56:52 +01:00
|
|
|
void main() async {
|
2020-06-25 22:42:16 +02:00
|
|
|
await runTestCases(getFilePaths('kanji_test_cases'), searchForKanji);
|
|
|
|
await runTestCases(getFilePaths('example_test_cases'), searchForExamples);
|
|
|
|
await runTestCases(getFilePaths('phrase_scrape_test_cases'), scrapeForPhrase);
|
2020-03-08 21:11:32 +01:00
|
|
|
}
|