diff --git a/lib/src/objects.dart b/lib/src/objects.dart index 0592d97..0835307 100644 --- a/lib/src/objects.dart +++ b/lib/src/objects.dart @@ -69,11 +69,20 @@ class YomiExample { String reading; String meaning; - YomiExample({String example, String reading, String meaning}){ + YomiExample({String example, String reading, String meaning}) + { this.example = example; this.reading = reading; this.meaning = meaning; } + + Map toJson() => + { + 'example': example, + 'reading': reading, + 'meaning': meaning + }; + } class Radical { @@ -86,6 +95,14 @@ class Radical { this.forms = forms; this.meaning = meaning; } + + Map toJson() => + { + 'symbol': symbol, + 'forms': forms, + 'meaning': meaning + }; + } class KanjiResult { diff --git a/lib/src/unofficial_jisho_api_base.dart b/lib/src/unofficial_jisho_api_base.dart index a27a79b..cd9a1a0 100644 --- a/lib/src/unofficial_jisho_api_base.dart +++ b/lib/src/unofficial_jisho_api_base.dart @@ -90,7 +90,7 @@ List getYomiExamples(String pageHtml, String yomiLocatorSymbol) { return null; } - final regex = RegExp(r'/
  • (.*?)<\/li>'); + final regex = RegExp(r'
  • (.*?)<\/li>', dotAll: true); final regexResults = getAllGlobalGroupMatches(exampleSection, regex).map((s) => s.trim()); final examples = regexResults.map((regexResult) { @@ -182,9 +182,9 @@ List getParts(String pageHtml) { } String getSvgUri(String pageHtml) { - var svgRegex = RegExp('\/\/.*?.cloudfront.net\/.*?.svg/'); - final regexResult = svgRegex.firstMatch(pageHtml).toString(); - return regexResult ?? 'http:${regexResult}'; + var svgRegex = RegExp('\/\/.*?.cloudfront.net\/.*?.svg'); + final regexResult = svgRegex.firstMatch(pageHtml).group(0).toString(); + return regexResult.isNotEmpty ? 'http:${regexResult}' : null; } String getGifUri(String kanji) { diff --git a/test/local_function_test_cases.dart b/test/local_function_test_cases.dart index 065807f..bc331ff 100644 --- a/test/local_function_test_cases.dart +++ b/test/local_function_test_cases.dart @@ -1,6 +1,8 @@ +import 'package:unofficial_jisho_api/src/objects.dart'; import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; -import 'package:test/test.dart'; +import 'package:test/test.dart'; +import 'dart:convert'; import 'package:http/http.dart' as http; void test_local_functions() async { @@ -56,7 +58,24 @@ void test_local_functions() async { }); test('parseAnchorsToArray', () { + const htmlCode = + ''' +
    +

    + Hello +

    + Hi + +

    + How are you doing +

    +
    +
    + '''; + final result = parseAnchorsToArray(htmlCode); + expect(result, [ + 'Hello', 'Hi', 'How are you doing']); }); test('getYomi', () { @@ -76,23 +95,101 @@ void test_local_functions() async { }); test('getYomiExamples', () { - final result = getYomiExamples(kanjiPage, 'Kun'); - expect(result, ['ジ']); //FIX + final result = getYomiExamples(kanjiPage, 'On'); + expect( + json.encode(result), + json.encode([ + YomiExample( + example: '時', + reading: 'ジ', + meaning: '''hour, o'clock, (specified) time, when ..., during ...''' + ), + YomiExample( + example: '時価', + reading: 'ジカ', + meaning: 'current value, price, market value' + ), + YomiExample( + example: '零時', + reading: 'レイジ', + meaning: '''twelve o'clock, midnight, noon''' + ), + YomiExample( + example: '平時', + reading: 'ヘイジ', + meaning: 'peacetime, time of peace, ordinary times, normal times' + ), + ]) + ); }); - test('getOnomiExamples', () { + test('getOnyomiExamples', () { final result = getOnyomiExamples(kanjiPage); - expect(result, ['ジ']); //FIX + expect( + json.encode(result), + json.encode([ + YomiExample( + example: '時', + reading: 'ジ', + meaning: '''hour, o'clock, (specified) time, when ..., during ...''' + ), + YomiExample( + example: '時価', + reading: 'ジカ', + meaning: 'current value, price, market value' + ), + YomiExample( + example: '零時', + reading: 'レイジ', + meaning: '''twelve o'clock, midnight, noon''' + ), + YomiExample( + example: '平時', + reading: 'ヘイジ', + meaning: 'peacetime, time of peace, ordinary times, normal times' + ), + ]) + ); }); test('getKunyomiExamples', () { final result = getKunyomiExamples(kanjiPage); - expect(result, ['ジ']); //FIX + expect( + json.encode(result), + json.encode([ + YomiExample( + example: '時', + reading: 'とき', + meaning: 'time, hour, moment, occasion, case, chance, opportunity, season, the times, the age, the day, tense' + ), + YomiExample( + example: '時折', + reading: 'ときおり', + meaning: 'sometimes' + ), + YomiExample( + example: '切り替え時', + reading: 'きりかえとき', + meaning: 'time to switch over, response time' + ), + YomiExample( + example: '逢魔が時', + reading: 'おうまがとき', + meaning: '''twilight, time for disasters (similar to 'the witching hour' but not midnight)''' + ), + ]) + ); }); test('getRadical', () { final result = getRadical(kanjiPage); - expect(result, ['ジ']); //FIX + expect( + json.encode(result), + json.encode(Radical( + symbol: '日', + meaning: 'sun, day' + )) + ); }); test('getParts', () { @@ -112,11 +209,83 @@ void test_local_functions() async { test('getNewspaperFrequencyRank', () { final result = getNewspaperFrequencyRank(kanjiPage); - expect(result, 16); //This might change + expect(result, 16); }); test('parseKanjiPageData', () { + final result = parseKanjiPageData(kanjiPage, '時'); + final expectedResult = KanjiResult(); + expectedResult.query = '時'; + expectedResult.found = true; + expectedResult.taughtIn = 'grade 2'; + expectedResult.jlptLevel = 'N5'; + expectedResult.newspaperFrequencyRank = 16; + expectedResult.strokeCount = 10; + expectedResult.meaning = 'time, hour'; + expectedResult.kunyomi = ['とき', '-どき']; + expectedResult.onyomi = ['ジ']; + expectedResult.onyomiExamples = + [ + YomiExample( + example: '時', + reading: 'ジ', + meaning: '''hour, o'clock, (specified) time, when ..., during ...''' + ), + YomiExample( + example: '時価', + reading: 'ジカ', + meaning: 'current value, price, market value' + ), + YomiExample( + example: '零時', + reading: 'レイジ', + meaning: '''twelve o'clock, midnight, noon''' + ), + YomiExample( + example: '平時', + reading: 'ヘイジ', + meaning: 'peacetime, time of peace, ordinary times, normal times' + ), + ]; + expectedResult.kunyomiExamples = + [ + YomiExample( + example: '時', + reading: 'とき', + meaning: 'time, hour, moment, occasion, case, chance, opportunity, season, the times, the age, the day, tense' + ), + YomiExample( + example: '時折', + reading: 'ときおり', + meaning: 'sometimes' + ), + YomiExample( + example: '切り替え時', + reading: 'きりかえとき', + meaning: 'time to switch over, response time' + ), + YomiExample( + example: '逢魔が時', + reading: 'おうまがとき', + meaning: '''twilight, time for disasters (similar to 'the witching hour' but not midnight)''' + ), + ]; + expectedResult.radical = + Radical( + symbol: '日', + meaning: 'sun, day' + ); + expectedResult.parts = ['土', '寸', '日']; + expectedResult.strokeOrderDiagramUri = 'https://classic.jisho.org/static/images/stroke_diagrams/26178_frames.png'; + expectedResult.strokeOrderSvgUri = 'http://d1w6u4xc3l95km.cloudfront.net/kanji-2015-03/06642.svg'; + expectedResult.strokeOrderGifUri = 'https://raw.githubusercontent.com/mistval/kanji_images/master/gifs/3c.gif'; + expectedResult.uri = 'https://jisho.org/search/%E6%99%82%23kanji'; + + expect( + json.encode(result), + json.encode(expectedResult) + ); }); /* KANJI SEARCH FUNCTION TESTS END */