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; return result;
} }
class API { class JishoApi {
/// Query the official Jisho API for a word or phrase /// 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 'package:path/path.dart' as path;
import 'dart:convert'; import 'dart:convert';
import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; import 'package:unofficial_jisho_api/unofficial_jisho_api.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
final jisho = API(); final jisho = JishoApi();
List<String> getFilePaths(String dirname) { List<String> getFilePaths(String dirname) {
final currentdir = io.Directory.current.path; final currentdir = Directory.current.path;
final filenames = io.Directory(path.join(currentdir, 'test', dirname)).listSync(); final filenames = Directory(path.join(currentdir, 'test', dirname)).listSync();
return filenames.map((filename) => path.join(currentdir, 'test', dirname, filename.path)).toList(); 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++) { 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); final testCase = jsonDecode(file);
test('Test ${testCount}', () async { await test('Test ${testCount}', () async {
switch(apiFunction) { switch(apiFunction) {
case 'searchForKanji': { case 'searchForKanji': {
final result = await jisho.searchForKanji(testCase.query); final result = await jisho.searchForKanji(testCase['query']);
expect(result, testCase.expectedResult); expect(result, testCase['expectedResult']);
break; break;
} }
case 'searchForExamples': { case 'searchForExamples': {
final result = await jisho.searchForExamples(testCase.query); final result = await jisho.searchForExamples(testCase['query']);
expect(result, testCase.expectedResult); expect(result, testCase['expectedResult']);
break; break;
} }
case 'scrapeForPhrase': { case 'scrapeForPhrase': {
final result = await jisho.scrapeForPhrase(testCase.query); final result = await jisho.scrapeForPhrase(testCase['query']);
expect(result, testCase.expectedResult); expect(result, testCase['expectedResult']);
break; break;
} }
throw 'No API function provided'; throw 'No API function provided';
@ -40,21 +40,12 @@ void runTestCases(testCaseFiles, apiFunction) async {
} }
} }
void main() { void main() async {
group('Kanji test cases', () { await runTestCases(getFilePaths('kanji_test_cases'), 'searchForKanji');
final filePaths = getFilePaths('kanji_test_cases');
runTestCases(filePaths, 'searchForKanji');
});
group('Example test cases', () { await runTestCases(getFilePaths('example_test_cases'), 'searchForExamples');
final filePaths = getFilePaths('example_test_cases');
runTestCases(filePaths, 'searchForExamples');
});
group('Phrase scrape test cases', () { await runTestCases(getFilePaths('phrase_scrape_test_cases'), 'scrapeForPhrase');
final filePaths = getFilePaths('phrase_scrape_test_cases');
runTestCases(filePaths, 'scrapeForPhrase');
});
} }