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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 library incrementally_building;
2
3 import 'package:unittest/unittest.dart';
4
5 var data = [{'scheme': 'https', 'domain': 'news.ycombinator.com'},
6 {'domain': 'www.google.com'},
7 {'domain': 'reddit.com', 'path': 'search', 'params': 'q=dart'}
8 ];
9
10 String assembleUrlsUsingStringBuffer(data) {
11 StringBuffer sb = new StringBuffer();
12 for (final item in data) {
13 sb.write(item['scheme'] != null ? item['scheme'] : 'http');
14 sb.write("://");
15 sb.write(item['domain']);
16 sb.write('/');
17 sb.write(item['path'] != null ? item['path'] : '');
18 if (item['params'] != null) {
19 sb.write('?');
20 sb.write(item['params']);
21 }
22 sb.write('\n');
23 }
24 return sb.toString();
25 }
26
27 String assembleUrlsUsingConcat(data) {
28 var urls = '';
29 for (final item in data) {
30 urls = urls.concat(item['scheme'] != null ? item['scheme'] : 'http');
31 urls = urls.concat("://");
32 urls = urls.concat(item['domain']);
33 urls = urls.concat('/');
34 urls = urls.concat(item['path'] != null ? item['path'] : '');
35 if (item['params'] != null) {
36 urls = urls.concat('?');
37 urls = urls.concat(item['params']);
38 }
39 urls = urls.concat('\n');
40 }
41 return urls;
42 }
43
44 void main() {
45 group('incrementally building a string', () {
46 group('using a StringBuffer', () {
47 test('using write()', () {
48 expect(assembleUrlsUsingStringBuffer(data), equals('''https://news.ycomb inator.com/
49 http://www.google.com/
50 http://reddit.com/search?q=dart
51 '''));
52 });
53 test('using several methods', () {
54 var sb = new StringBuffer();
55 sb.writeln('The Beatles:');
56 sb.writeAll(['John, ', 'Paul, ', 'George, and Ringo']);
57 sb.writeCharCode(33); // charCode for '!'.
58 expect(sb.toString(), equals('The Beatles:\nJohn, Paul, George, and Ring o!'));
59 });
60 });
61
62 group('using concat()', () {
63 test('', () {
64 expect(assembleUrlsUsingConcat(data), equals('''https://news.ycombinator .com/
65 http://www.google.com/
66 http://reddit.com/search?q=dart
67 '''));
68 });
69 });
70 });
71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698