OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 // VMOptions=--compile-all --error_on_bad_type --error_on_bad_override --checked | |
5 | |
6 import 'package:observatory/service_io.dart'; | |
7 import 'package:unittest/unittest.dart'; | |
8 import 'test_helper.dart'; | |
9 import 'dart:async'; | |
10 | |
11 int globalVar = 100; | |
12 | |
13 class MyClass { | |
14 static void myFunction(int value) { | |
15 print(value); // line 14 | |
16 if (value < 0) { | |
17 print("negative"); | |
18 } else { | |
19 print("positive"); | |
20 } | |
21 } | |
22 | |
23 static void otherFunction(int value) { | |
24 if (value < 0) { | |
25 print("otherFunction <"); | |
26 } else { | |
27 print("otherFunction >="); | |
28 } | |
29 } | |
30 } | |
31 | |
32 void testFunction() { | |
33 MyClass.otherFunction(-100); | |
34 int i = 0; | |
35 while (true) { | |
36 if (++i % 100000000 == 0) { | |
37 MyClass.myFunction(10000); | |
38 } | |
39 } | |
40 } | |
41 | |
42 List normalize(List coverage) { | |
43 // The exact coverage numbers may vary based on how many times | |
44 // things run. Normalize the data to 0 or 1. | |
45 List normalized = []; | |
46 for (int i = 0; i < coverage.length; i += 2) { | |
47 normalized.add(coverage[i]); | |
48 normalized.add(coverage[i+1] == 0 ? 0 : 1); | |
49 } | |
50 return normalized; | |
51 } | |
52 | |
53 var tests = [ | |
54 | |
55 // Go to breakpoint at line 14. | |
56 (Isolate isolate) { | |
57 return isolate.rootLib.load().then((_) { | |
58 // Set up a listener to wait for breakpoint events. | |
59 Completer completer = new Completer(); | |
60 isolate.vm.events.stream.listen((ServiceEvent event) { | |
61 if (event.eventType == ServiceEvent.kPauseBreakpoint) { | |
62 print('Breakpoint reached'); | |
63 completer.complete(); | |
64 } | |
65 }); | |
66 | |
67 // Create a timer to set a breakpoint with a short delay. | |
68 new Timer(new Duration(milliseconds: 2000), () { | |
69 // Add the breakpoint. | |
70 print('Setting breakpoint.'); | |
71 var script = isolate.rootLib.scripts[0]; | |
72 var line = 14; | |
73 isolate.addBreakpoint(script, line); | |
74 }); | |
75 | |
76 return completer.future; | |
77 }); | |
78 }, | |
79 | |
80 // Get coverage for function, class, library, script, and isolate. | |
81 (Isolate isolate) { | |
82 return isolate.getStack().then((ServiceMap stack) { | |
83 // Make sure we are in the right place. | |
84 expect(stack.type, equals('Stack')); | |
85 expect(stack['frames'].length, greaterThanOrEqualTo(2)); | |
86 expect(stack['frames'][0]['function'].name, equals('myFunction')); | |
87 expect(stack['frames'][0]['function'].dartOwner.name, equals('MyClass')); | |
88 | |
89 var lib = isolate.rootLib; | |
90 var func = stack['frames'][0]['function']; | |
91 expect(func.name, equals('myFunction')); | |
92 var cls = stack['frames'][0]['function'].dartOwner; | |
93 expect(cls.name, equals('MyClass')); | |
94 | |
95 List tests = []; | |
96 // Function | |
97 tests.add(isolate.invokeRpcNoUpgrade('getCoverage', | |
98 { 'targetId': func.id }) | |
99 .then((Map coverage) { | |
100 expect(coverage['type'], equals('CodeCoverage')); | |
101 expect(coverage['coverage'].length, equals(1)); | |
102 expect(normalize(coverage['coverage'][0]['hits']), | |
103 equals([15, 1, 16, 1, 17, 0, 19, 1])); | |
104 })); | |
105 // Class | |
106 tests.add(isolate.invokeRpcNoUpgrade('getCoverage', | |
107 { 'targetId': cls.id }) | |
108 .then((Map coverage) { | |
109 expect(coverage['type'], equals('CodeCoverage')); | |
110 expect(coverage['coverage'].length, equals(1)); | |
111 expect(normalize(coverage['coverage'][0]['hits']), | |
112 equals([15, 1, 16, 1, 17, 0, 19, 1, | |
113 24, 1, 25, 1, 27, 0])); | |
114 })); | |
115 // Library | |
116 tests.add(isolate.invokeRpcNoUpgrade('getCoverage', | |
117 { 'targetId': lib.id }) | |
118 .then((Map coverage) { | |
119 expect(coverage['type'], equals('CodeCoverage')); | |
120 expect(coverage['coverage'].length, equals(3)); | |
121 expect(normalize(coverage['coverage'][0]['hits']), | |
122 equals([15, 1, 16, 1, 17, 0, 19, 1, | |
123 24, 1, 25, 1, 27, 0])); | |
124 expect(normalize(coverage['coverage'][1]['hits']).take(12), | |
125 equals([33, 1, 36, 1, 37, 0, 32, 1, 45, 0, 46, 0])); | |
126 })); | |
127 // Script | |
128 tests.add(cls.load().then((_) { | |
129 return isolate.invokeRpcNoUpgrade('getCoverage', | |
130 { 'targetId': cls.script.id }) | |
131 .then((Map coverage) { | |
132 expect(coverage['type'], equals('CodeCoverage')); | |
133 expect(coverage['coverage'].length, equals(3)); | |
134 expect(normalize(coverage['coverage'][0]['hits']), | |
135 equals([15, 1, 16, 1, 17, 0, 19, 1, | |
136 24, 1, 25, 1, 27, 0])); | |
137 expect(normalize(coverage['coverage'][1]['hits']).take(12), | |
138 equals([33, 1, 36, 1, 37, 0, 32, 1, 45, 0, 46, 0])); | |
139 }); | |
140 })); | |
141 // Isolate | |
142 tests.add(cls.load().then((_) { | |
143 return isolate.invokeRpcNoUpgrade('getCoverage', {}) | |
144 .then((Map coverage) { | |
145 expect(coverage['type'], equals('CodeCoverage')); | |
146 expect(coverage['coverage'].length, greaterThan(100)); | |
147 }); | |
148 })); | |
149 return Future.wait(tests); | |
150 }); | |
151 }, | |
152 | |
153 ]; | |
154 | |
155 main(args) => runIsolateTests(args, tests, testeeConcurrent: testFunction); | |
OLD | NEW |