unofficial_jisho_api_dart/test/unofficial_jisho_api_test.dart

32 lines
1.2 KiB
Dart
Raw Permalink 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';
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']);
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-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
}