Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Unified Diff: recipes/test/core/strings/incrementally_building_test.dart

Issue 12335109: Strings recipes for the Dart Cookbook (Closed) Base URL: https://github.com/dart-lang/cookbook.git@master
Patch Set: Made most changes requested my Kathy. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: recipes/test/core/strings/incrementally_building_test.dart
diff --git a/recipes/test/core/strings/incrementally_building_test.dart b/recipes/test/core/strings/incrementally_building_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6a2ace49412b17d2d8f0d4e9821a5e87b9caa40b
--- /dev/null
+++ b/recipes/test/core/strings/incrementally_building_test.dart
@@ -0,0 +1,71 @@
+library incrementally_building;
+
+import 'package:unittest/unittest.dart';
+
+var data = [{'scheme': 'https', 'domain': 'news.ycombinator.com'},
+ {'domain': 'www.google.com'},
+ {'domain': 'reddit.com', 'path': 'search', 'params': 'q=dart'}
+ ];
+
+String assembleUrlsUsingStringBuffer(data) {
+ StringBuffer sb = new StringBuffer();
+ for (final item in data) {
+ sb.write(item['scheme'] != null ? item['scheme'] : 'http');
+ sb.write("://");
+ sb.write(item['domain']);
+ sb.write('/');
+ sb.write(item['path'] != null ? item['path'] : '');
+ if (item['params'] != null) {
+ sb.write('?');
+ sb.write(item['params']);
+ }
+ sb.write('\n');
+ }
+ return sb.toString();
+}
+
+String assembleUrlsUsingConcat(data) {
+ var urls = '';
+ for (final item in data) {
+ urls = urls.concat(item['scheme'] != null ? item['scheme'] : 'http');
+ urls = urls.concat("://");
+ urls = urls.concat(item['domain']);
+ urls = urls.concat('/');
+ urls = urls.concat(item['path'] != null ? item['path'] : '');
+ if (item['params'] != null) {
+ urls = urls.concat('?');
+ urls = urls.concat(item['params']);
+ }
+ urls = urls.concat('\n');
+ }
+ return urls;
+}
+
+void main() {
+ group('incrementally building a string', () {
+ group('using a StringBuffer', () {
+ test('using write()', () {
+ expect(assembleUrlsUsingStringBuffer(data), equals('''https://news.ycombinator.com/
+http://www.google.com/
+http://reddit.com/search?q=dart
+'''));
+ });
+ test('using several methods', () {
+ var sb = new StringBuffer();
+ sb.writeln('The Beatles:');
+ sb.writeAll(['John, ', 'Paul, ', 'George, and Ringo']);
+ sb.writeCharCode(33); // charCode for '!'.
+ expect(sb.toString(), equals('The Beatles:\nJohn, Paul, George, and Ringo!'));
+ });
+ });
+
+ group('using concat()', () {
+ test('', () {
+ expect(assembleUrlsUsingConcat(data), equals('''https://news.ycombinator.com/
+http://www.google.com/
+http://reddit.com/search?q=dart
+'''));
+ });
+ });
+ });
+}

Powered by Google App Engine
This is Rietveld 408576698