Skip to content

Commit

Permalink
Analyze and test more cheatsheet snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough committed Aug 10, 2021
1 parent a8cb01b commit d048679
Show file tree
Hide file tree
Showing 15 changed files with 293 additions and 141 deletions.
27 changes: 0 additions & 27 deletions examples/misc/bin/cheatsheet/collection_literals.dart

This file was deleted.

53 changes: 0 additions & 53 deletions examples/misc/bin/cheatsheet/exceptions.dart

This file was deleted.

17 changes: 0 additions & 17 deletions examples/misc/bin/null_aware_operators.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,3 @@ class MyClass {
}
}
// #enddocregion

void main() {
MyClass _class = MyClass();
_class.addValue(5);
_class.addValue(7);
print(_class.count.toString());
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,3 @@ class MyClass {
}
}
// #enddocregion

void main() {
MyClass _class = MyClass();
_class.aProperty = 5;
print(_class.aProperty);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ void printName(String firstName, String lastName, {String? suffix}) {
}
// #enddocregion

void main() {
void printNameTest() {
// #docregion
printName('Avinash', 'Gupta');
printName('Poshmeister', 'Moneybuckets', suffix: 'IV');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int sumUp(int a, int b, int c) {
}
// #enddocregion optional-positional-args

void main() {
void mainTest() {
// #docregion optional-positional-args
int total = sumUp(1, 2, 3);
// #enddocregion optional-positional-args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ int sumUpToFive(int a, [int b = 2, int c = 3, int d = 4, int e = 5]) {
}
// #enddocregion

void main() {
void mainTest() {
// #docregion
int newTotal = sumUpToFive(1);
print(newTotal); // <-- prints 15
Expand Down
44 changes: 44 additions & 0 deletions examples/misc/test/cheatsheet/arguments_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:examples/cheatsheet/optional_named_params.dart';
import 'package:examples/cheatsheet/optional_positional_args.dart' as optionalArgs;
import 'package:examples/cheatsheet/optional_positional_args2.dart' as defaultedArgs;
import 'package:test/test.dart';

void main() {
test('print_name', () {
expect(() {
printNameTest();
}, prints('Avinash Gupta \nPoshmeister Moneybuckets IV\n'));
});

test('print_name_default', () {
// #docregion defaulted-suffix
void printName(String firstName, String lastName, {String suffix = ''}) {
print('$firstName $lastName $suffix');
}
// #enddocregion defaulted-suffix

expect(() {
printName('Dash', 'Dartisan', suffix: 'III');
}, prints('Dash Dartisan III\n'));

expect(() {
printName('Dash', 'Dartisan');
}, prints('Dash Dartisan \n'));
});

test('optional_positional_args', () {
expect(() {
optionalArgs.mainTest();
}, prints('6\n3\n15\n'));

expect(optionalArgs.sumUpToFive(1, 4, 7), equals(12));
});

test('optional_positional_args_defaulted', () {
expect(() {
defaultedArgs.mainTest();
}, prints('15\n'));

expect(defaultedArgs.sumUpToFive(1, 1), equals(14));
});
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// ignore_for_file: unused_local_variable
import 'package:test/test.dart';

void main() {
const aListOfStrings = ['a', 'b', 'c'];

{
test('has_empty_long', () {
// #docregion has-empty-long
bool hasEmpty = aListOfStrings.any((s) {
return s.isEmpty;
});
// #enddocregion has-empty-long
}

{
expect(hasEmpty, isFalse);
});

test('has_empty_short', () {
// #docregion has-empty-short
bool hasEmpty = aListOfStrings.any((s) => s.isEmpty);
// #enddocregion has-empty-short
}

expect(hasEmpty, isFalse);
});
}
53 changes: 53 additions & 0 deletions examples/misc/test/cheatsheet/collections_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:test/test.dart';

void main() {
test('collection_literals_inferred', () {
// #docregion collection-literals-inferred
final aListOfStrings = ['one', 'two', 'three'];
final aSetOfStrings = {'one', 'two', 'three'};
final aMapOfStringsToInts = {
'one': 1,
'two': 2,
'three': 3,
};
// #enddocregion collection-literals-inferred

expect(aListOfStrings, isA<List<String>>());
expect(aListOfStrings[1], equals('two'));

expect(aSetOfStrings, isA<Set<String>>());
expect(aSetOfStrings, contains('two'));

expect(aMapOfStringsToInts, isA<Map<String, int>>());
expect(aMapOfStringsToInts['two'], equals(2));
});

test('collection_literals_specified', () {
// #docregion collection-literals-specified
final aListOfInts = <int>[];
final aSetOfInts = <int>{};
final aMapOfIntToDouble = <int, double>{};
// #enddocregion collection-literals-specified

expect(aListOfInts, isA<List<int>>());

expect(aSetOfInts, isA<Set<int>>());

expect(aMapOfIntToDouble, isA<Map<int, double>>());
});

test('collection_literals_subtypes', () {
// #docregion collection-literals-subtypes
final aListOfBaseType = <BaseType>[SubType(), SubType()];
// #enddocregion collection-literals-subtypes

expect(aListOfBaseType, isA<List<BaseType>>());

expect(
aListOfBaseType, containsAllInOrder([isA<SubType>(), isA<SubType>()]));
});
}

class BaseType {}

class SubType extends BaseType {}
86 changes: 86 additions & 0 deletions examples/misc/test/cheatsheet/exceptions_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// ignore_for_file: dead_code

import 'package:test/test.dart';

void main() {
test('try_on_catch', () {
expect(() {
// #docregion try-on-catch
try {
breedMoreLlamas();
} on OutOfLlamasException {
// A specific exception
buyMoreLlamas();
} on Exception catch (e) {
// Anything else that is an exception
print('Unknown exception: $e');
} catch (e) {
// No specified type, handles all
print('Something really unknown: $e');
}
// #enddocregion try-on-catch
}, prints('Bought more llamas.\n'));
});

test('simple_throws', () {
void throwErrors() {
// #docregion simple-throws
throw Exception('Something bad happened.');
throw 'Waaaaaaah!';
// #enddocregion simple-throws
}

expect(throwErrors, throwsException);
});

test('try_catch', () {
expect(() {
expect(() {
// #docregion try-catch
try {
breedMoreLlamas();
} catch (e) {
print('I was just trying to breed llamas!');
rethrow;
}
// #enddocregion try-catch
}, throwsA(isA<OutOfLlamasException>()));
}, prints('I was just trying to breed llamas!\n'));
});

test('try_catch_finally', () {
expect(() {
// #docregion try-catch-finally
try {
breedMoreLlamas();
} catch (e) {
// ... handle exception ...
} finally {
// Always clean up, even if an exception is thrown.
cleanLlamaStalls();
}
// #enddocregion try-catch-finally
}, prints('Cleaned llama stalls.\n'));
});
}

void cleanLlamaStalls() {
print('Cleaned llama stalls.');
}

void breedMoreLlamas() {
throw OutOfLlamasException();
}

void buyMoreLlamas() {
print('Bought more llamas.');
}

class OutOfLlamasException implements Exception {}

void throwErrors() {
// #docregion simple-throws
throw Exception('Something bad happened.');
throw 'Waaaaaaah!';
// #enddocregion simple-throws
}
Loading

0 comments on commit d048679

Please sign in to comment.