Skip to content

Commit

Permalink
Ensure Try-Dart example sources are analyzed and tested (dart-lang#2124)
Browse files Browse the repository at this point in the history
* 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
chalin authored Nov 25, 2019
1 parent 1e9ed05 commit b2acb0b
Show file tree
Hide file tree
Showing 15 changed files with 1,603 additions and 1,405 deletions.
41 changes: 41 additions & 0 deletions examples/misc/bin/try_dart/classes.dart
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();
}
}
23 changes: 23 additions & 0 deletions examples/misc/bin/try_dart/collection_literals.dart
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'}));
}
26 changes: 26 additions & 0 deletions examples/misc/bin/try_dart/control_flow.dart
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));
}
22 changes: 22 additions & 0 deletions examples/misc/bin/try_dart/functions.dart
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)}");
}
17 changes: 17 additions & 0 deletions examples/misc/bin/try_dart/strings.dart
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}');
}
3 changes: 3 additions & 0 deletions examples/misc/lib/pi_monte_carlo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
// e.g., using this Perl regexp: / ?\/\/!.*//g

import 'dart:html'; //!web-only
// #docregion try-dart
import 'dart:math' show Random; //!tip("dart:math") //!tip("import")

// #enddocregion try-dart
int numIterations = 500; //!web-only
//!web-only
// We changed a few lines of code to make this sample nicer //!web-only
Expand All @@ -20,6 +22,7 @@ int numIterations = 500; //!web-only
// after 500 iterations). //!web-only
//!web-only
//!tip("main()")
// #docregion try-dart
main() async {
print('Compute π using the Monte Carlo method.'); //!tip("π")
var output = querySelector("#value-of-pi"); //!web-only
Expand Down
5 changes: 2 additions & 3 deletions examples/misc/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ environment:

dev_dependencies:
args: ^1.5.0
test: ^1.0.0
examples_util: {path: ../util}
pedantic: ^1.0.0
examples_util:
path: ../util
test: ^1.0.0
36 changes: 36 additions & 0 deletions examples/misc/test/try_dart_test.dart
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));
});
}
Loading

0 comments on commit b2acb0b

Please sign in to comment.