From 1db585cc8b49b76ddec1a95952e4ae5f8b6a655d Mon Sep 17 00:00:00 2001 From: h7x4abk3g Date: Fri, 26 Jun 2020 11:44:46 +0200 Subject: [PATCH] Rewrite tests to use groups --- test/unofficial_jisho_api_test.dart | 57 +++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/test/unofficial_jisho_api_test.dart b/test/unofficial_jisho_api_test.dart index b909f93..7c70331 100644 --- a/test/unofficial_jisho_api_test.dart +++ b/test/unofficial_jisho_api_test.dart @@ -1,29 +1,56 @@ +import 'dart:convert'; 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 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(); + final testDir = path.join(currentdir, 'test', dirname); + final filenames = Directory(testDir).listSync(); + return filenames.map((filename) => path.join(testDir, filename.path)); } -void runTestCases(List testCaseFiles, Function apiFunction) async { +List getTestCases(String directoryName) { + final testCaseFiles = getFilePaths(directoryName); + var result = []; + 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)); - }); + final file = File(testCaseFiles[testCount]).readAsStringSync(); + result.add(jsonDecode(file)); } + return result; } -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); -} +void main() { + group('searchForKanji', () { + final testCases = getTestCases('kanji_test_cases'); + for (var testCase in testCases) { + test('Query "${testCase['query']}"', () async { + final result = await searchForKanji(testCase['query']); + expect(jsonEncode(result), jsonEncode(testCase)); + }); + } + }); + + group('searchForExamples', () { + final testCases = getTestCases('example_test_cases'); + for (var testCase in testCases) { + test('Query "${testCase['query']}"', () async { + final result = await searchForExamples(testCase['query']); + expect(jsonEncode(result), jsonEncode(testCase)); + }); + } + }); + + group('scrapeForPhrase', () { + final testCases = getTestCases('phrase_scrape_test_cases'); + for (var testCase in testCases) { + test('Query "${testCase['query']}"', () async { + final result = await scrapeForPhrase(testCase['query']); + expect(jsonEncode(result), jsonEncode(testCase)); + }); + } + }); +} \ No newline at end of file