Skip to content

Commit

Permalink
feat: Adding features and test. #96
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed May 17, 2023
1 parent 576ec1c commit de6e0ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
27 changes: 26 additions & 1 deletion lib/quotesy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ library quotesy;

import 'dart:convert';
import 'dart:io';
import 'dart:math';

class QuoteInfo {
final String author;
Expand All @@ -21,10 +22,34 @@ class QuoteInfo {
}

class Quotes {
static Future<List<QuoteInfo>> loadQuotes() async {
static Future<List<QuoteInfo>> _quotesList() async {
String jsonString = await File('quotes.json').readAsString();
List<dynamic> jsonList = json.decode(jsonString);
List<QuoteInfo> quotes = jsonList.map((json) => QuoteInfo.fromJson(json)).toList();
return quotes;
}

static list() async {
return await _quotesList();
}

static Future<QuoteInfo> random() async {
final random = Random();
final list = await _quotesList();

return list[random.nextInt(list.length)];
}

static Future<List<QuoteInfo>> byAuthor(String author) async {
final list = await _quotesList();
return list.where((quote) => quote.author == author).toList();
}

static Future<QuoteInfo> singleRandomByAuthor(String author) async {
final list = await _quotesList();
final listByAuthor = list.where((quote) => quote.author == author).toList();
final random = Random();

return listByAuthor[random.nextInt(listByAuthor.length)];
}
}
20 changes: 18 additions & 2 deletions test/quotesy_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@ import 'package:test/test.dart';

void main() {
group('A group of tests', () {
test('Fetching first element from quotes list', () async {
final quotesArray = await Quotes.loadQuotes();
test('Fetching quotes list', () async {
final quotesArray = await Quotes.list();
expect(quotesArray.length, greaterThan(0));
expect(quotesArray.first.author, "Abraham Lincoln");
});

test('Fetching random element', () async {
final randomQuote = await Quotes.random();
expect(randomQuote, isNotNull);
});

test('Fetching by author', () async {
final quoteList = await Quotes.byAuthor("Abraham Lincoln");
expect(quoteList.length, greaterThan(0));
});

test('Fetching random quote by author', () async {
final quoteList = await Quotes.singleRandomByAuthor("Abraham Lincoln");
expect(quoteList.author, "Abraham Lincoln");
});
});
}

0 comments on commit de6e0ee

Please sign in to comment.