unofficial_jisho_api_dart/test/unofficial_jisho_api_test.dart

55 lines
1.8 KiB
Dart
Raw Normal View History

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';
import 'package:unofficial_jisho_api/unofficial_jisho_api.dart';
2020-04-21 22:36:26 +02:00
import 'local_function_test_cases.dart' show test_local_functions;
2020-03-08 21:11:32 +01:00
import 'package:test/test.dart';
2020-03-12 10:56:52 +01:00
final jisho = JishoApi();
2020-03-08 21:11:32 +01:00
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-03-12 10:56:52 +01:00
void runTestCases(List<String> testCaseFiles, String 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-03-08 21:11:32 +01:00
switch(apiFunction) {
case 'searchForKanji': {
2020-03-12 10:56:52 +01:00
final result = await jisho.searchForKanji(testCase['query']);
2020-04-21 22:36:26 +02:00
expect(result, jsonDecode(testCase['expectedResult']));
2020-03-08 21:11:32 +01:00
break;
}
case 'searchForExamples': {
2020-03-12 10:56:52 +01:00
final result = await jisho.searchForExamples(testCase['query']);
expect(result, testCase['expectedResult']);
2020-03-08 21:11:32 +01:00
break;
}
case 'scrapeForPhrase': {
2020-03-12 10:56:52 +01:00
final result = await jisho.scrapeForPhrase(testCase['query']);
expect(result, testCase['expectedResult']);
2020-03-08 21:11:32 +01:00
break;
}
throw 'No API function provided';
}
});
}
}
2020-03-12 10:56:52 +01:00
void main() async {
2020-03-08 21:11:32 +01:00
2020-04-21 22:36:26 +02:00
await test_local_functions();
2020-03-12 10:56:52 +01:00
await runTestCases(getFilePaths('kanji_test_cases'), 'searchForKanji');
2020-03-08 21:11:32 +01:00
2020-03-12 10:56:52 +01:00
await runTestCases(getFilePaths('example_test_cases'), 'searchForExamples');
2020-03-08 21:11:32 +01:00
2020-03-12 10:56:52 +01:00
await runTestCases(getFilePaths('phrase_scrape_test_cases'), 'scrapeForPhrase');
2020-03-08 21:11:32 +01:00
}