forked from dart-lang/site-www
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Analyze and test more cheatsheet snippets
Fixes dart-lang#2295
- Loading branch information
Showing
15 changed files
with
293 additions
and
141 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
} |
14 changes: 9 additions & 5 deletions
14
.../misc/bin/cheatsheet/arrow_functions.dart → ...test/cheatsheet/arrow_functions_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.