unofficial_jisho_api_dart/test/unofficial_jisho_api_test.dart

32 lines
1.2 KiB
Dart

import 'dart:io';
import 'package:path/path.dart' as path;
import 'dart:convert';
import 'package:unofficial_jisho_api/unofficial_jisho_api.dart';
import 'package:test/test.dart';
final jisho = JishoApi();
List<String> getFilePaths(String dirname) {
final currentdir = Directory.current.path;
final filenames = Directory(path.join(currentdir, 'test', dirname)).listSync();
return filenames.map((filename) => path.join(currentdir, 'test', dirname, filename.path)).toList();
}
void runTestCases(List<String> testCaseFiles, Function apiFunction) async {
for (var testCount = 0; testCount < testCaseFiles.length; testCount++) {
final file = await File(testCaseFiles[testCount]).readAsString();
final testCase = jsonDecode(file);
await test('Test ${testCount}', () async {
final result = await apiFunction(testCase['query']);
expect(jsonEncode(result), jsonEncode(testCase['expectedResult']));
});
}
}
void main() async {
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);
}