| 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 /** Tests for the watcher library. */ | 5 /** Tests for the watcher library. */ | 
| 6 library watcher_test; | 6 library watcher_test; | 
| 7 | 7 | 
| 8 import 'dart:collection'; | 8 import 'dart:collection'; | 
| 9 import 'package:unittest/compact_vm_config.dart'; | 9 import 'package:unittest/compact_vm_config.dart'; | 
| 10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; | 
| 11 import 'package:web_ui/watcher.dart'; | 11 import 'package:web_ui/watcher.dart'; | 
|  | 12 import 'package:logging/logging.dart'; | 
| 12 | 13 | 
| 13 main() { | 14 main() { | 
| 14   useCompactVMConfiguration(); | 15   useCompactVMConfiguration(); | 
| 15 | 16 | 
| 16   group('core', () { | 17   group('core', () { | 
| 17     test('simple watcher ', () { | 18     test('simple watcher ', () { | 
| 18       int x = 0; | 19       int x = 0; | 
| 19       int valueSeen = null; | 20       int valueSeen = null; | 
| 20       var stop = watch(() => x, expectAsync1((_) { valueSeen = x; })); | 21       var stop = watch(() => x, expectAsync1((_) { valueSeen = x; })); | 
| 21       x = 22; | 22       x = 22; | 
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 97       x = 12; | 98       x = 12; | 
| 98       dispatch(); | 99       dispatch(); | 
| 99       expect(oldValue, 1); | 100       expect(oldValue, 1); | 
| 100       expect(newValue, 12); | 101       expect(newValue, 12); | 
| 101       x = 14; | 102       x = 14; | 
| 102       dispatch(); | 103       dispatch(); | 
| 103       expect(oldValue, 12); | 104       expect(oldValue, 12); | 
| 104       expect(newValue, 14); | 105       expect(newValue, 14); | 
| 105       stop(); | 106       stop(); | 
| 106     }); | 107     }); | 
|  | 108 | 
|  | 109     test('loop is detected', () { | 
|  | 110       int x = 0; | 
|  | 111       var oldValue; | 
|  | 112       var newValue; | 
|  | 113       var stop = watch(() => x, expectAsync1((e) { | 
|  | 114         x++; | 
|  | 115         oldValue = e.oldValue; | 
|  | 116         newValue = e.newValue; | 
|  | 117       }, count: maxNumIterations)); | 
|  | 118       var subscription = Logger.root.onRecord.listen(expectAsync1((record) { | 
|  | 119         expect(record.message, startsWith('Possible loop in watchers')); | 
|  | 120       })); | 
|  | 121       x = 1; | 
|  | 122       dispatch(); | 
|  | 123       expect(oldValue, maxNumIterations - 1); | 
|  | 124       expect(newValue, maxNumIterations); | 
|  | 125       stop(); | 
|  | 126       subscription.cancel(); | 
|  | 127     }); | 
|  | 128 | 
|  | 129     group('loops can be debugged', () { | 
|  | 130       var messages; | 
|  | 131       var subscription; | 
|  | 132       setUp(() { | 
|  | 133         verboseDebugMessages = true; | 
|  | 134         messages = []; | 
|  | 135         subscription = Logger.root.onRecord.listen((record) { | 
|  | 136           messages.add(record.message); | 
|  | 137         }); | 
|  | 138       }); | 
|  | 139 | 
|  | 140       tearDown(() { | 
|  | 141         subscription.cancel(); | 
|  | 142         verboseDebugMessages = false; | 
|  | 143       }); | 
|  | 144 | 
|  | 145       test('no debug name', () { | 
|  | 146         int x = 0; | 
|  | 147         var oldValue; | 
|  | 148         var newValue; | 
|  | 149         // Note: the next line number (150) is recorded in the debug messages | 
|  | 150         var stop = watch(() => x, expectAsync1((e) { | 
|  | 151           x++; | 
|  | 152           oldValue = e.oldValue; | 
|  | 153           newValue = e.newValue; | 
|  | 154         }, count: maxNumIterations)); | 
|  | 155         x = 1; | 
|  | 156         dispatch(); | 
|  | 157         expect(oldValue, maxNumIterations - 1); | 
|  | 158         expect(newValue, maxNumIterations); | 
|  | 159         expect(messages.length, maxNumIterations + 1); | 
|  | 160 | 
|  | 161         // First message contains details of the definition of a callback: | 
|  | 162         var first = messages.first; | 
|  | 163         expect(first, contains('defined at')); | 
|  | 164         expect(first, contains('watcher_test.dart:150:25')); | 
|  | 165 | 
|  | 166         // The id is based on some global numbering, read the id, and see that | 
|  | 167         // it is mentioned on every other message: | 
|  | 168         var regExp = new RegExp('(\\(id: #[0-9]*\\))'); | 
|  | 169         var match = regExp.firstMatch(first); | 
|  | 170         expect(match, isNotNull); | 
|  | 171         var id = match.group(1); | 
|  | 172         for (int i = 1; i < maxNumIterations - 1; i++) { | 
|  | 173           expect(messages[i], 'watcher updated: <unnamed> $id'); | 
|  | 174         } | 
|  | 175         expect(messages.last, startsWith('Possible loop in watchers')); | 
|  | 176         stop(); | 
|  | 177       }); | 
|  | 178 | 
|  | 179       test('debug name provided', () { | 
|  | 180         verboseDebugMessages = true; | 
|  | 181         var messages = []; | 
|  | 182         var subscription = Logger.root.onRecord.listen((record) { | 
|  | 183           messages.add(record.message); | 
|  | 184         }); | 
|  | 185         int x = 0; | 
|  | 186         var oldValue; | 
|  | 187         var newValue; | 
|  | 188         // Note: the next line number (189) is recorded in the debug messages | 
|  | 189         var stop = watch(() => x, expectAsync1((e) { | 
|  | 190           x++; | 
|  | 191           oldValue = e.oldValue; | 
|  | 192           newValue = e.newValue; | 
|  | 193         }, count: maxNumIterations), 'my-debug-name'); | 
|  | 194         x = 1; | 
|  | 195         dispatch(); | 
|  | 196         expect(oldValue, maxNumIterations - 1); | 
|  | 197         expect(newValue, maxNumIterations); | 
|  | 198         expect(messages.length, maxNumIterations + 1); | 
|  | 199 | 
|  | 200         // First message contains details of the definition of a callback: | 
|  | 201         var first = messages.first; | 
|  | 202         expect(first, contains('defined at')); | 
|  | 203         expect(first, contains('watcher_test.dart:189:25')); | 
|  | 204 | 
|  | 205         // The id is based on some global numbering, read the id, and see that | 
|  | 206         // it is mentioned on every other message: | 
|  | 207         var regExp = new RegExp('my-debug-name (\\(id: #[0-9]*\\))'); | 
|  | 208         var match = regExp.firstMatch(first); | 
|  | 209         expect(match, isNotNull); | 
|  | 210         var id = match.group(1); | 
|  | 211         for (int i = 1; i < maxNumIterations - 1; i++) { | 
|  | 212           expect(messages[i], 'watcher updated: my-debug-name $id'); | 
|  | 213         } | 
|  | 214         expect(messages.last, startsWith('Possible loop in watchers')); | 
|  | 215         stop(); | 
|  | 216         subscription.cancel(); | 
|  | 217         verboseDebugMessages = false; | 
|  | 218       }); | 
|  | 219     }); | 
| 107   }); | 220   }); | 
| 108 | 221 | 
| 109   group('fields', () { | 222   group('fields', () { | 
| 110     test('watch changes to shallow fields', () { | 223     test('watch changes to shallow fields', () { | 
| 111       B b = new B(3); | 224       B b = new B(3); | 
| 112       int value = null; | 225       int value = null; | 
| 113       var stop = watch(() => b.c, | 226       var stop = watch(() => b.c, | 
| 114         expectAsync1((_) { value = b.c; }, count: 2)); | 227         expectAsync1((_) { value = b.c; }, count: 2)); | 
| 115       b.c = 5; | 228       b.c = 5; | 
| 116       dispatch(); | 229       dispatch(); | 
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 357 } | 470 } | 
| 358 | 471 | 
| 359 class A { | 472 class A { | 
| 360   B b = new B(3); | 473   B b = new B(3); | 
| 361 } | 474 } | 
| 362 | 475 | 
| 363 class B { | 476 class B { | 
| 364   int c; | 477   int c; | 
| 365   B(this.c); | 478   B(this.c); | 
| 366 } | 479 } | 
| OLD | NEW | 
|---|