@@ -5,6 +5,7 @@ import 'package:angular2/test_lib.dart';
55import 'package:angular2/src/facade/async.dart' ;
66
77class MockException implements Error { var message; var stackTrace; }
8+ class NonError { var message; }
89
910void functionThatThrows () {
1011 try { throw new MockException (); }
@@ -18,7 +19,13 @@ void functionThatThrows() {
1819}
1920
2021void functionThatThrowsNonError () {
21- throw 'this is an error' ;
22+ try { throw new NonError (); }
23+ catch (e, stack) {
24+ // If we lose the stack trace the message will no longer match
25+ // the first line in the stack
26+ e.message = stack.toString ().split ('\n ' )[0 ];
27+ rethrow ;
28+ }
2229}
2330
2431void expectFunctionThatThrowsWithStackTrace (
@@ -29,72 +36,65 @@ void expectFunctionThatThrowsWithStackTrace(
2936 });
3037}
3138
32- void expectFunctionThatThrowsWithoutStackTrace (Future future,
33- AsyncTestCompleter async ) {
34- PromiseWrapper .catchError (future, (err, StackTrace stack) {
35- expect (stack).toBe (null );
36- async .done ();
37- });
38- }
39-
4039main () {
41- describe ('Completer' , () {
42-
43- it ('should preserve error stack traces' ,
44- inject ([AsyncTestCompleter ], (async ) {
45- var c = PromiseWrapper .completer ();
46-
47- expectFunctionThatThrowsWithStackTrace (c.promise, async );
48-
49- try {
50- functionThatThrows ();
51- } catch (e) {
52- c.reject (e);
53- }
54- }));
40+ describe ('async facade' , () {
41+ describe ('Completer' , () {
5542
56- // TODO: We might fix this one day; for now testing it to be explicit
57- it ('CANNOT preserve error stack traces for non-Errors' ,
58- inject ([AsyncTestCompleter ], (async ) {
59- var c = PromiseWrapper .completer ();
60-
61- expectFunctionThatThrowsWithoutStackTrace (c.promise, async );
62-
63- try {
64- functionThatThrowsNonError ();
65- } catch (e) {
66- c.reject (e);
67- }
68- }));
69-
70- });
71-
72- describe ('PromiseWrapper' , () {
43+ it ('should preserve Error stack traces' ,
44+ inject ([AsyncTestCompleter ], (async ) {
45+ var c = PromiseWrapper .completer ();
7346
74- describe ( 'reject' , () {
47+ expectFunctionThatThrowsWithStackTrace (c.promise, async );
7548
76- it ('should preserve error stack traces' ,
77- inject ([AsyncTestCompleter ], (async ) {
7849 try {
7950 functionThatThrows ();
8051 } catch (e) {
81- var rejectedFuture = PromiseWrapper .reject (e);
82- expectFunctionThatThrowsWithStackTrace (rejectedFuture, async );
52+ c.reject (e, null );
8353 }
8454 }));
8555
86- // TODO: We might fix this one day; for now testing it to be explicit
87- it ('CANNOT preserve stack traces for non-Errors' ,
56+ it ('should preserve error stack traces for non-Errors' ,
8857 inject ([AsyncTestCompleter ], (async ) {
58+ var c = PromiseWrapper .completer ();
59+
60+ expectFunctionThatThrowsWithStackTrace (c.promise, async );
61+
8962 try {
9063 functionThatThrowsNonError ();
91- } catch (e) {
92- var rejectedFuture = PromiseWrapper .reject (e);
93- expectFunctionThatThrowsWithoutStackTrace (rejectedFuture, async );
64+ } catch (e, s) {
65+ c.reject (e, s);
9466 }
9567 }));
9668
9769 });
9870
71+ describe ('PromiseWrapper' , () {
72+
73+ describe ('reject' , () {
74+
75+ it ('should preserve Error stack traces' ,
76+ inject ([AsyncTestCompleter ], (async ) {
77+ try {
78+ functionThatThrows ();
79+ } catch (e) {
80+ var rejectedFuture = PromiseWrapper .reject (e, null );
81+ expectFunctionThatThrowsWithStackTrace (rejectedFuture, async );
82+ }
83+ }));
84+
85+ it ('should preserve stack traces for non-Errors' ,
86+ inject ([AsyncTestCompleter ], (async ) {
87+ try {
88+ functionThatThrowsNonError ();
89+ } catch (e, s) {
90+ var rejectedFuture = PromiseWrapper .reject (e, s);
91+ expectFunctionThatThrowsWithStackTrace (rejectedFuture, async );
92+ }
93+ }));
94+
95+ });
96+
97+ });
98+
9999 });
100100}
0 commit comments