unofficial_jisho_api_dart/test/unofficial_jisho_api_test.dart

80 lines
2.5 KiB
Dart
Raw Normal View History

2020-06-26 11:44:46 +02:00
import 'dart:convert';
2020-03-12 10:56:52 +01:00
import 'dart:io';
2020-03-08 21:11:32 +01:00
2021-07-25 22:57:02 +02:00
import 'package:path/path.dart' as path;
2020-03-08 21:11:32 +01:00
import 'package:test/test.dart';
2021-07-25 22:57:02 +02:00
import 'package:unofficial_jisho_api/api.dart';
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;
2020-06-26 11:44:46 +02:00
final testDir = path.join(currentdir, 'test', dirname);
final filenames = Directory(testDir).listSync();
2021-07-25 22:57:02 +02:00
return filenames
.map((filename) => path.join(testDir, filename.path))
.toList();
2020-03-08 21:11:32 +01:00
}
List<dynamic> getTestCases(String directoryName) {
2020-06-26 11:44:46 +02:00
final testCaseFiles = getFilePaths(directoryName);
final result = <dynamic>[];
2020-06-26 11:44:46 +02:00
2020-03-08 21:11:32 +01:00
for (var testCount = 0; testCount < testCaseFiles.length; testCount++) {
2020-06-26 11:44:46 +02:00
final file = File(testCaseFiles[testCount]).readAsStringSync();
result.add(jsonDecode(file));
2020-03-08 21:11:32 +01:00
}
2020-06-26 11:44:46 +02:00
return result;
2020-03-08 21:11:32 +01:00
}
2020-06-26 11:44:46 +02:00
void main() {
group('searchForKanji', () {
final testCases = getTestCases('kanji_test_cases')
.map((e) => KanjiResult.fromJson(e))
.toList();
2020-06-26 11:44:46 +02:00
for (var testCase in testCases) {
test('Query "${testCase.query}"', () async {
final result = await searchForKanji(testCase.query);
expect(result, testCase);
2020-06-26 11:44:46 +02:00
});
}
});
group('searchForExamples', () {
final testCases = getTestCases('example_test_cases')
.map((e) => ExampleResults.fromJson(e))
.toList();
2020-06-26 11:44:46 +02:00
for (var testCase in testCases) {
test('Query "${testCase.query}"', () async {
final result = await searchForExamples(testCase.query);
expect(result, testCase);
2020-06-26 11:44:46 +02:00
});
}
});
group('scrapeForPhrase', () {
final testCases = getTestCases('phrase_scrape_test_cases')
.map((e) => PhrasePageScrapeResult.fromJson(e))
.toList();
2020-06-26 11:44:46 +02:00
for (var testCase in testCases) {
test('Query "${testCase.query}"', () async {
final result = await scrapeForPhrase(testCase.query);
expect(result, testCase);
2020-06-26 11:44:46 +02:00
});
}
});
group('equatable', () {
// searchForExample returns random results, not stable for testing.
// I will assume it works if all the other works, and rather wait for
// reports in Github issues if something is broken.
final kanjiSearch = [searchForKanji(''), searchForKanji('')];
final phraseScrape = [scrapeForPhrase('関係'), scrapeForPhrase('関係')];
final phraseSearch = [searchForPhrase('関係'), searchForPhrase('関係')];
for (var testCase in [kanjiSearch, phraseScrape, phraseSearch]) {
test('Equate ${testCase[0].runtimeType}' , () async {
expect(await testCase[0], await testCase[1]);
});
}
});
2021-07-25 22:57:02 +02:00
}