unofficial_jisho_api_dart/test/unofficial_jisho_api_test.dart

37 lines
1.3 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-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']);
expect(result, testCase['expectedResult']);
2020-03-08 21:11:32 +01:00
});
}
}
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-06-14 12:48:53 +02:00
await runTestCases(getFilePaths('kanji_test_cases'), jisho.searchForKanji);
await runTestCases(getFilePaths('example_test_cases'), jisho.searchForExamples);
await runTestCases(getFilePaths('phrase_scrape_test_cases'), jisho.scrapeForPhrase);
2020-03-08 21:11:32 +01:00
}