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.
Ensure Try-Dart example sources are analyzed and tested (dart-lang#2124)
* Ensure Try-Dart example sources are analyzed and tested * Remove dartpad_picker_main dev help files * Drop demo /try-dart page * Undo example code changes Per @mit-mit's request. Will be addressed in a followup PR. * Drop nullity annotation (in a comment)
- Loading branch information
Showing
15 changed files
with
1,603 additions
and
1,405 deletions.
There are no files selected for viewing
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,41 @@ | ||
// ignore_for_file: annotate_overrides, sort_constructors_first, type_annotate_public_apis | ||
// Abstract classes can't be instantiated. | ||
// ignore: one_member_abstracts | ||
abstract class Item { | ||
use(); | ||
} | ||
|
||
// Classes can implement other classes. | ||
class Chest<T> implements Item { | ||
List<T> contents; | ||
|
||
// Constructors can assign arguments to instance variables using `this`. | ||
Chest(this.contents); | ||
|
||
use() => print("$this has ${contents.length} items."); | ||
} | ||
|
||
class Sword implements Item { | ||
int damage = 5; | ||
|
||
use() => print("$this dealt $damage damage."); | ||
} | ||
|
||
// Classes can extend other classes. | ||
class DiamondSword extends Sword { | ||
int damage = 50; // ignore: overridden_fields | ||
} | ||
|
||
main() { | ||
// The 'new' keyword is optional. | ||
var chest = Chest<Item>([ | ||
DiamondSword(), | ||
Sword(), | ||
]); | ||
|
||
chest.use(); | ||
|
||
for (var item in chest.contents) { | ||
item.use(); | ||
} | ||
} |
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,23 @@ | ||
// ignore_for_file: type_annotate_public_apis | ||
// A list literal. | ||
var lostNumbers = [4, 8, 15, 16, 23, 42]; | ||
|
||
// A map literal. | ||
var nobleGases = { | ||
'He': 'Helium', | ||
'Ne': 'Neon', | ||
'Ar': 'Argon', | ||
}; | ||
|
||
// A set literal. | ||
var frogs = { | ||
'Tree', | ||
'Poison dart', | ||
'Glass', | ||
}; | ||
|
||
main() { | ||
print(lostNumbers.last); | ||
print(nobleGases['Ne']); | ||
print(frogs.difference({'Poison 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
bool isEven(int x) { | ||
// An if-else statement. | ||
if (x % 2 == 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
List<int> getEvenNumbers(Iterable<int> numbers) { | ||
var evenNumbers = <int>[]; | ||
|
||
// A for-in loop. | ||
for (var i in numbers) { | ||
// A single-line if statement. | ||
if (isEven(i)) evenNumbers.add(i); | ||
} | ||
|
||
return evenNumbers; | ||
} | ||
|
||
// ignore: type_annotate_public_apis | ||
main() { | ||
var numbers = List.generate(10, (i) => i); | ||
print(getEvenNumbers(numbers)); | ||
} |
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,22 @@ | ||
// A function declaration. | ||
int timesTwo(int x) { | ||
return x * 2; | ||
} | ||
|
||
// Arrow syntax is shorthand for `{ return expr; }`. | ||
int timesFour(int x) => timesTwo(timesTwo(x)); | ||
|
||
// Functions are objects. | ||
int runTwice(int x, Function f) { | ||
for (var i = 0; i < 2; i++) { | ||
x = f(x); // ignore: invalid_assignment | ||
} | ||
return x; | ||
} | ||
|
||
// ignore: type_annotate_public_apis | ||
main() { | ||
print("4 times two is ${timesTwo(4)}"); | ||
print("4 times four is ${timesFour(4)}"); | ||
print("2 x 2 x 2 is ${runTwice(2, timesTwo)}"); | ||
} |
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,17 @@ | ||
// ignore: type_annotate_public_apis | ||
main() { | ||
print('a single quoted string'); | ||
print("a double quoted string"); | ||
|
||
// Strings can be combined with the + operator. | ||
print("cat" + "dog"); | ||
|
||
// Triple quotes define a multi-line string. | ||
print('''triple quoted strings | ||
are for multiple lines'''); | ||
|
||
// Dart supports string interpolation. | ||
var pi = 3.14; | ||
print('pi is $pi'); | ||
print('tau is ${2 * pi}'); | ||
} |
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,36 @@ | ||
import 'package:test/test.dart'; | ||
|
||
import '../bin/try_dart/functions.dart' as functions; | ||
import '../bin/try_dart/classes.dart' as classes; | ||
import '../bin/try_dart/collection_literals.dart' as collection_literals; | ||
import '../bin/try_dart/control_flow.dart' as control_flow; | ||
import '../bin/try_dart/strings.dart' as strings; | ||
|
||
void main() { | ||
test('functions', () { | ||
expect(functions.main, prints(endsWith('2 x 2 x 2 is 8\n'))); | ||
}); | ||
|
||
test('control_flow', () { | ||
expect(control_flow.main, prints('[0, 2, 4, 6, 8]\n')); | ||
}); | ||
|
||
test('strings', () { | ||
expect(strings.main, prints(endsWith('tau is 6.28\n'))); | ||
}); | ||
|
||
test('collection_literals', () { | ||
expect(collection_literals.main, prints('42\nNeon\n{Tree, Glass}\n')); | ||
}); | ||
|
||
test('classes', () { | ||
final output = ''' | ||
Instance of 'Chest<Item>' has 2 items. | ||
Instance of 'DiamondSword' dealt 50 damage. | ||
Instance of 'Sword' dealt 5 damage. | ||
''' | ||
.trimLeft() | ||
.replaceAll(RegExp(r'\n\s*'), '\n'); | ||
expect(classes.main, prints(output)); | ||
}); | ||
} |
Oops, something went wrong.