| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Test to ensure that StringBuffer and string interpolation behaves | |
| 6 // the same and fail fast. | |
| 7 | |
| 8 class ToStringWrapper { | |
| 9 final value; | |
| 10 | |
| 11 ToStringWrapper(this.value); | |
| 12 | |
| 13 toString() => value; | |
| 14 } | |
| 15 | |
| 16 wrap(value) => new ToStringWrapper(value); | |
| 17 | |
| 18 main() { | |
| 19 bool checkedMode = false; | |
| 20 assert(checkedMode = true); | |
| 21 interpolate(object) { | |
| 22 var result; | |
| 23 if (checkedMode && object != null) { | |
| 24 try { | |
| 25 result = '${wrap(object)}'; | |
| 26 } on TypeError { | |
| 27 return 'Error'; | |
| 28 } | |
| 29 } else { | |
| 30 try { | |
| 31 result = '${wrap(object)}'; | |
| 32 } on ArgumentError { | |
| 33 return 'Error'; | |
| 34 } | |
| 35 } | |
| 36 Expect.isTrue(result is String); | |
| 37 return 'Success'; | |
| 38 } | |
| 39 | |
| 40 buffer(object) { | |
| 41 var sb; | |
| 42 if (checkedMode && object != null) { | |
| 43 try { | |
| 44 sb = new StringBuffer()..write(wrap(object)); | |
| 45 } on TypeError { | |
| 46 return 'Error'; | |
| 47 } | |
| 48 } else { | |
| 49 try { | |
| 50 sb = new StringBuffer()..write(wrap(object)); | |
| 51 } on ArgumentError { | |
| 52 return 'Error'; | |
| 53 } | |
| 54 Expect.isTrue(sb.toString() is String); | |
| 55 } | |
| 56 return 'Success'; | |
| 57 } | |
| 58 | |
| 59 initBuffer(object) { | |
| 60 var sb; | |
| 61 if (checkedMode && object != null) { | |
| 62 try { | |
| 63 sb = new StringBuffer(wrap(object)); | |
| 64 } on TypeError { | |
| 65 return 'Error'; | |
| 66 } | |
| 67 } else { | |
| 68 try { | |
| 69 sb = new StringBuffer(wrap(object)); | |
| 70 } on ArgumentError { | |
| 71 return 'Error'; | |
| 72 } | |
| 73 Expect.isTrue(sb.toString() is String); | |
| 74 } | |
| 75 return 'Success'; | |
| 76 } | |
| 77 | |
| 78 Expect.equals('Error', interpolate(null)); | |
| 79 Expect.equals('Success', interpolate("")); | |
| 80 Expect.equals('Success', interpolate("string")); | |
| 81 Expect.equals('Error', interpolate([])); | |
| 82 Expect.equals('Error', interpolate([1])); | |
| 83 Expect.equals('Error', interpolate(new Object())); | |
| 84 | |
| 85 Expect.equals('Error', buffer(null)); | |
| 86 Expect.equals('Success', buffer("")); | |
| 87 Expect.equals('Success', buffer("string")); | |
| 88 Expect.equals('Error', buffer([])); | |
| 89 Expect.equals('Error', buffer([1])); | |
| 90 Expect.equals('Error', buffer(new Object())); | |
| 91 | |
| 92 Expect.equals('Error', initBuffer(null)); | |
| 93 Expect.equals('Success', initBuffer("")); | |
| 94 Expect.equals('Success', initBuffer("string")); | |
| 95 Expect.equals('Error', initBuffer([])); | |
| 96 Expect.equals('Error', initBuffer([1])); | |
| 97 Expect.equals('Error', initBuffer(new Object())); | |
| 98 } | |
| OLD | NEW |