OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Test to ensure that StringBuffer and string interpolation behaves | 5 // Test to ensure that StringBuffer and string interpolation behaves |
6 // the same and fail fast. | 6 // the same and fail fast. |
7 | 7 |
8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
9 | 9 |
10 class ToStringWrapper { | 10 class ToStringWrapper { |
11 final value; | 11 final value; |
12 | 12 |
13 ToStringWrapper(this.value); | 13 ToStringWrapper(this.value); |
14 | 14 |
15 toString() => value; | 15 toString() => value; |
16 } | 16 } |
17 | 17 |
18 wrap(value) => new ToStringWrapper(value); | 18 wrap(value) => new ToStringWrapper(value); |
19 | 19 |
20 main() { | 20 main() { |
21 bool checkedMode = false; | 21 bool checkedMode = false; |
22 assert(checkedMode = true); | 22 assert(checkedMode = true); |
23 interpolate(object) { | 23 interpolate(object) { |
24 var result; | 24 var result; |
25 if (checkedMode && object != null) { | 25 if (checkedMode && object != null) { |
26 try { | 26 try { |
27 result = '${wrap(object)}'; | 27 result = '${wrap(object)}'; |
28 } on TypeError { | 28 } on TypeError { |
29 return 'Error'; | 29 return 'Error'; |
| 30 } on ArgumentError { |
| 31 return 'Error'; // Checked mode. |
30 } | 32 } |
31 } else { | 33 } else { |
32 try { | 34 try { |
33 result = '${wrap(object)}'; | 35 result = '${wrap(object)}'; |
34 } on ArgumentError { | 36 } on ArgumentError { |
35 return 'Error'; | 37 return 'Error'; |
36 } | 38 } |
37 } | 39 } |
38 Expect.isTrue(result is String); | 40 Expect.isTrue(result is String); |
39 return 'Success'; | 41 return 'Success'; |
40 } | 42 } |
41 | 43 |
42 buffer(object) { | 44 buffer(object) { |
43 var sb; | 45 var sb; |
44 if (checkedMode && object != null) { | 46 if (checkedMode && object != null) { |
45 try { | 47 try { |
46 sb = new StringBuffer()..write(wrap(object)); | 48 sb = new StringBuffer()..write(wrap(object)); |
47 } on TypeError { | 49 } on TypeError { |
48 return 'Error'; | 50 return 'Error'; |
| 51 } on ArgumentError { |
| 52 return 'Error'; // Checked mode. |
49 } | 53 } |
50 } else { | 54 } else { |
51 try { | 55 try { |
52 sb = new StringBuffer()..write(wrap(object)); | 56 sb = new StringBuffer()..write(wrap(object)); |
53 } on ArgumentError { | 57 } on ArgumentError { |
54 return 'Error'; | 58 return 'Error'; |
55 } | 59 } |
56 Expect.isTrue(sb.toString() is String); | 60 Expect.isTrue(sb.toString() is String); |
57 } | 61 } |
58 return 'Success'; | 62 return 'Success'; |
59 } | 63 } |
60 | 64 |
61 initBuffer(object) { | 65 initBuffer(object) { |
62 var sb; | 66 var sb; |
63 if (checkedMode && object != null) { | 67 if (checkedMode && object != null) { |
64 try { | 68 try { |
65 sb = new StringBuffer(wrap(object)); | 69 sb = new StringBuffer(wrap(object)); |
66 } on TypeError { | 70 } on TypeError { |
67 return 'Error'; | 71 return 'Error'; |
| 72 } on ArgumentError { |
| 73 return 'Error'; // Checked mode. |
68 } | 74 } |
69 } else { | 75 } else { |
70 try { | 76 try { |
71 sb = new StringBuffer(wrap(object)); | 77 sb = new StringBuffer(wrap(object)); |
72 } on ArgumentError { | 78 } on ArgumentError { |
73 return 'Error'; | 79 return 'Error'; |
74 } | 80 } |
75 Expect.isTrue(sb.toString() is String); | 81 Expect.isTrue(sb.toString() is String); |
76 } | 82 } |
77 return 'Success'; | 83 return 'Success'; |
(...skipping 13 matching lines...) Expand all Loading... |
91 Expect.equals('Error', buffer([1])); | 97 Expect.equals('Error', buffer([1])); |
92 Expect.equals('Error', buffer(new Object())); | 98 Expect.equals('Error', buffer(new Object())); |
93 | 99 |
94 Expect.equals('Error', initBuffer(null)); | 100 Expect.equals('Error', initBuffer(null)); |
95 Expect.equals('Success', initBuffer("")); | 101 Expect.equals('Success', initBuffer("")); |
96 Expect.equals('Success', initBuffer("string")); | 102 Expect.equals('Success', initBuffer("string")); |
97 Expect.equals('Error', initBuffer([])); | 103 Expect.equals('Error', initBuffer([])); |
98 Expect.equals('Error', initBuffer([1])); | 104 Expect.equals('Error', initBuffer([1])); |
99 Expect.equals('Error', initBuffer(new Object())); | 105 Expect.equals('Error', initBuffer(new Object())); |
100 } | 106 } |
OLD | NEW |