fix tests

This commit is contained in:
Oystein Kristoffer Tveit 2020-03-12 10:56:52 +01:00
parent 08b3f990fa
commit 7157bd1d73
2 changed files with 18 additions and 27 deletions

View File

@ -433,7 +433,7 @@ PhrasePageScrapeResult parsePhrasePageData(pageHtml, query) {
return result;
}
class API {
class JishoApi {
/// Query the official Jisho API for a word or phrase
///

View File

@ -1,37 +1,37 @@
import 'dart:io' as io;
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 = API();
final jisho = JishoApi();
List<String> getFilePaths(String dirname) {
final currentdir = io.Directory.current.path;
final filenames = io.Directory(path.join(currentdir, 'test', dirname)).listSync();
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(testCaseFiles, apiFunction) async {
void runTestCases(List<String> testCaseFiles, String apiFunction) async {
for (var testCount = 0; testCount < testCaseFiles.length; testCount++) {
final file = await io.File(testCaseFiles[testCount]).readAsString();
final file = await File(testCaseFiles[testCount]).readAsString();
final testCase = jsonDecode(file);
test('Test ${testCount}', () async {
await test('Test ${testCount}', () async {
switch(apiFunction) {
case 'searchForKanji': {
final result = await jisho.searchForKanji(testCase.query);
expect(result, testCase.expectedResult);
final result = await jisho.searchForKanji(testCase['query']);
expect(result, testCase['expectedResult']);
break;
}
case 'searchForExamples': {
final result = await jisho.searchForExamples(testCase.query);
expect(result, testCase.expectedResult);
final result = await jisho.searchForExamples(testCase['query']);
expect(result, testCase['expectedResult']);
break;
}
case 'scrapeForPhrase': {
final result = await jisho.scrapeForPhrase(testCase.query);
expect(result, testCase.expectedResult);
final result = await jisho.scrapeForPhrase(testCase['query']);
expect(result, testCase['expectedResult']);
break;
}
throw 'No API function provided';
@ -40,21 +40,12 @@ void runTestCases(testCaseFiles, apiFunction) async {
}
}
void main() {
void main() async {
group('Kanji test cases', () {
final filePaths = getFilePaths('kanji_test_cases');
runTestCases(filePaths, 'searchForKanji');
});
await runTestCases(getFilePaths('kanji_test_cases'), 'searchForKanji');
group('Example test cases', () {
final filePaths = getFilePaths('example_test_cases');
runTestCases(filePaths, 'searchForExamples');
});
await runTestCases(getFilePaths('example_test_cases'), 'searchForExamples');
group('Phrase scrape test cases', () {
final filePaths = getFilePaths('phrase_scrape_test_cases');
runTestCases(filePaths, 'scrapeForPhrase');
});
await runTestCases(getFilePaths('phrase_scrape_test_cases'), 'scrapeForPhrase');
}