unofficial_jisho_api_dart/test/unofficial_jisho_api_test.dart

30 lines
1.1 KiB
Dart

import 'dart:io';
import 'package:path/path.dart' as path;
import 'dart:convert';
import 'package:unofficial_jisho_api/api.dart';
import 'package:test/test.dart';
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));
});
}
}
void main() async {
await runTestCases(getFilePaths('kanji_test_cases'), searchForKanji);
await runTestCases(getFilePaths('example_test_cases'), searchForExamples);
await runTestCases(getFilePaths('phrase_scrape_test_cases'), scrapeForPhrase);
}