OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Test correct source positions in stack trace with optimized functions. | 4 // Test correct source positions in stack trace with optimized functions. |
5 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 // (1) Test normal exception. | 7 // (1) Test normal exception. |
8 foo(x) => bar(x); | 8 foo(x) => bar(x); |
9 | 9 |
10 bar(x) { | 10 bar(x) { |
11 if (x == null) throw 42; // throw at position 11:18 | 11 if (x == null) throw 42; // throw at position 11:18 |
12 return x + 1; | 12 return x + 1; |
13 } | 13 } |
14 | 14 |
15 test1() { | 15 test1() { |
16 // First unoptimized. | 16 // First unoptimized. |
17 try { | 17 try { |
18 foo(null); | 18 foo(null); |
19 Expect.fail("Unreachable"); | 19 Expect.fail("Unreachable"); |
20 } catch (e, stacktrace) { | 20 } catch (e, stacktrace) { |
21 String s = stacktrace.toString(); | 21 String s = stacktrace.toString(); |
22 print(s); | 22 print(s); |
23 Expect.isFalse(s.contains("-1:-1"), "A"); | 23 Expect.isFalse(s.contains("-1:-1"), "A"); |
24 Expect.isTrue( | 24 Expect.isTrue(s.contains("optimized_stacktrace_line_and_column_test.dart:11:
18"), "B"); |
25 s.contains("optimized_stacktrace_line_and_column_test.dart:11:18"), | |
26 "B"); | |
27 } | 25 } |
28 | 26 |
29 // Optimized. | 27 // Optimized. |
30 for (var i = 0; i < 10000; i++) foo(42); | 28 for (var i=0; i<10000; i++) foo(42); |
31 try { | 29 try { |
32 foo(null); | 30 foo(null); |
33 Expect.fail("Unreachable"); | 31 Expect.fail("Unreachable"); |
34 } catch (e, stacktrace) { | 32 } catch (e, stacktrace) { |
35 String s = stacktrace.toString(); | 33 String s = stacktrace.toString(); |
36 print(s); | 34 print(s); |
37 Expect.isFalse(s.contains("-1:-1"), "C"); | 35 Expect.isFalse(s.contains("-1:-1"), "C"); |
38 Expect.isTrue( | 36 Expect.isTrue(s.contains("optimized_stacktrace_line_and_column_test.dart:11:
18"), "D"); |
39 s.contains("optimized_stacktrace_line_and_column_test.dart:11:18"), | |
40 "D"); | |
41 } | 37 } |
42 } | 38 } |
43 | 39 |
| 40 |
44 // (2) Test checked mode exceptions. | 41 // (2) Test checked mode exceptions. |
45 maximus(x) => moritz(x); | 42 maximus(x) => moritz(x); |
46 | 43 |
47 moritz(x) { | 44 moritz(x) { |
48 if (x == 333) return 42 ? 0 : 1; // Throws in checked mode. | 45 if (x == 333) return 42 ? 0 : 1; // Throws in checked mode. |
49 if (x == 777) { | 46 if (x == 777) { |
50 bool b = x; // Throws in checked mode. | 47 bool b = x; // Throws in checked mode. |
51 return b; | 48 return b; |
52 } | 49 } |
53 | 50 |
54 return x + 1; | 51 return x + 1; |
55 } | 52 } |
56 | 53 |
57 test2() { | 54 test2() { |
58 for (var i = 0; i < 100000; i++) maximus(42); | 55 for (var i=0; i<100000; i++) maximus(42); |
59 try { | 56 try { |
60 maximus(333); | 57 maximus(333); |
61 } catch (e, stacktrace) { | 58 } catch (e, stacktrace) { |
62 String s = stacktrace.toString(); | 59 String s = stacktrace.toString(); |
63 print(s); | 60 print(s); |
64 Expect.isTrue(s.contains("maximus"), "E"); | 61 Expect.isTrue(s.contains("maximus"), "E"); |
65 Expect.isTrue(s.contains("moritz"), "F"); | 62 Expect.isTrue(s.contains("moritz"), "F"); |
66 Expect.isFalse(s.contains("-1:-1"), "G"); | 63 Expect.isFalse(s.contains("-1:-1"), "G"); |
67 } | 64 } |
68 | 65 |
69 try { | 66 try { |
70 maximus(777); | 67 maximus(777); |
71 } catch (e, stacktrace) { | 68 } catch (e, stacktrace) { |
72 String s = stacktrace.toString(); | 69 String s = stacktrace.toString(); |
73 print(s); | 70 print(s); |
74 Expect.isTrue(s.contains("maximus"), "H"); | 71 Expect.isTrue(s.contains("maximus"), "H"); |
75 Expect.isTrue(s.contains("moritz"), "I"); | 72 Expect.isTrue(s.contains("moritz"), "I"); |
76 Expect.isFalse(s.contains("-1:-1"), "J"); | 73 Expect.isFalse(s.contains("-1:-1"), "J"); |
77 } | 74 } |
78 } | 75 } |
79 | 76 |
80 main() { | 77 main() { |
81 test1(); | 78 test1(); |
82 test2(); | 79 test2(); |
83 } | 80 } |
OLD | NEW |