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