Skip to content

Instantly share code, notes, and snippets.

@johnpryan
Forked from legalcodes/hint.txt
Created May 31, 2019 21:00
Show Gist options
  • Save johnpryan/e2092f4f3f486cd1df5023cb9d794a07 to your computer and use it in GitHub Desktop.
Save johnpryan/e2092f4f3f486cd1df5023cb9d794a07 to your computer and use it in GitHub Desktop.
Practice Using async and await
Did you remember to add the async keyword to the getChange function?
Did you remember to use the await keyword before invoking getDollarAmount?
Remember: 'getChange' needs to return a future!
Future<String> reportUserRole() async {
// your implementation here
}
// Implement reportLogins here
Future<String> reportUserRole() async {
var username = await getRole();
return 'User role: $username';
}
Future<String> reportLogins() async {
var logins = await getLoginAmount();
return 'Total number of logins: $logins';
}
const role = 'administrator';
const logins = 42;
const passed = 'PASSED';
const typoMessage = 'Test failed! Check for typos in your return value';
const oneSecond = Duration(seconds: 1);
List<String> messages = [];
Future<String> getRole() => Future.delayed(oneSecond, () => role);
Future<int> getLoginAmount() => Future.delayed(oneSecond, () => logins);
main() async {
try {
messages
..add(makeReadable(
testLabel: 'Part 1',
testResult: await asyncEquals(
expected: 'User role: administrator',
actual: await reportUserRole(),
typoKeyword: role
),
readableErrors: {
typoMessage: typoMessage,
'User role: Instance of \'Future<String>\'': 'Test failed! reportUserRole failed. Did you use the await keyword?',
'User role: Instance of \'_Future<String>\'': 'Test failed! reportUserRole failed. Did you use the await keyword?',
}))
..add(makeReadable(
testLabel: 'Part 2',
testResult: await asyncEquals(
expected: 'Total number of logins: 42',
actual: await reportLogins(),
typoKeyword: logins.toString()
),
readableErrors: {
typoMessage: typoMessage,
'Total number of logins: Instance of \'Future<int>\'': 'Test failed! reportLogins failed. Did you use the await keyword?',
'Total number of logins: Instance of \'_Future<int>\'': 'Test failed! reportLogins failed. Did you use the await keyword?',
}))
..removeWhere((m) => m.contains(passed))
..toList();
if (messages.isEmpty) {
_result(true);
} else {
_result(false, messages);
}
} catch (e) {
_result(false, ['Tried to run solution, but received an exception: $e']);
}
}
////////////////////////////////////////
///////////// Test Helpers /////////////
////////////////////////////////////////
String makeReadable({ String testResult, Map readableErrors, String testLabel }) {
if (readableErrors.containsKey(testResult)) {
var readable = readableErrors[testResult];
return '$testLabel $readable';
} else {
return '$testLabel $testResult';
}
}
///////////////////////////////////////
//////////// Assertions ///////////////
///////////////////////////////////////
Future<String> asyncEquals({expected, actual, String typoKeyword}) async {
var strActual = actual is String ? actual : actual.toString();
try {
if (expected == actual) {
return passed;
} else if (strActual.contains(typoKeyword)) {
return typoMessage;
} else {
return strActual;
}
} catch(e) {
return e;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment