OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 #include "vm/coverage.h" | 5 #include "vm/coverage.h" |
6 #include "vm/dart_api_impl.h" | 6 #include "vm/dart_api_impl.h" |
7 #include "vm/unit_test.h" | 7 #include "vm/unit_test.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
11 static RawObject* ExecuteScript(const char* script) { | 11 static RawObject* ExecuteScript(const char* script) { |
| 12 TransitionVMToNative transition(Thread::Current()); |
12 Dart_Handle h_lib = TestCase::LoadTestScript(script, NULL); | 13 Dart_Handle h_lib = TestCase::LoadTestScript(script, NULL); |
13 EXPECT_VALID(h_lib); | 14 EXPECT_VALID(h_lib); |
14 Library& lib = Library::Handle(); | 15 Library& lib = Library::Handle(); |
15 lib ^= Api::UnwrapHandle(h_lib); | 16 lib ^= Api::UnwrapHandle(h_lib); |
16 EXPECT(!lib.IsNull()); | 17 EXPECT(!lib.IsNull()); |
17 Dart_Handle result = Dart_Invoke(h_lib, NewString("main"), 0, NULL); | 18 Dart_Handle result = Dart_Invoke(h_lib, NewString("main"), 0, NULL); |
18 EXPECT_VALID(result); | 19 EXPECT_VALID(result); |
19 return Api::UnwrapHandle(h_lib); | 20 return Api::UnwrapHandle(h_lib); |
20 } | 21 } |
21 | 22 |
22 | 23 |
23 class FunctionCoverageFilter : public CoverageFilter { | 24 class FunctionCoverageFilter : public CoverageFilter { |
24 public: | 25 public: |
25 explicit FunctionCoverageFilter(const Function& func) : func_(func) {} | 26 explicit FunctionCoverageFilter(const Function& func) : func_(func) {} |
26 bool ShouldOutputCoverageFor(const Library& lib, | 27 bool ShouldOutputCoverageFor(const Library& lib, |
27 const Script& script, | 28 const Script& script, |
28 const Class& cls, | 29 const Class& cls, |
29 const Function& func) const { | 30 const Function& func) const { |
30 return func.raw() == func_.raw(); | 31 return func.raw() == func_.raw(); |
31 } | 32 } |
32 private: | 33 private: |
33 const Function& func_; | 34 const Function& func_; |
34 }; | 35 }; |
35 | 36 |
36 | 37 |
37 TEST_CASE(Coverage_Empty) { | 38 VM_TEST_CASE(Coverage_Empty) { |
38 const char* kScript = | 39 const char* kScript = |
39 "main() {\n" | 40 "main() {\n" |
40 "}"; | 41 "}"; |
41 | 42 |
42 Library& lib = Library::Handle(); | 43 Library& lib = Library::Handle(); |
43 lib ^= ExecuteScript(kScript); | 44 lib ^= ExecuteScript(kScript); |
44 ASSERT(!lib.IsNull()); | 45 ASSERT(!lib.IsNull()); |
45 | 46 |
46 JSONStream js; | 47 JSONStream js; |
47 CodeCoverage::PrintJSON(thread, &js, NULL, false); | 48 CodeCoverage::PrintJSON(thread, &js, NULL, false); |
48 | 49 |
49 char buf[1024]; | 50 char buf[1024]; |
50 OS::SNPrint(buf, sizeof(buf), | 51 OS::SNPrint(buf, sizeof(buf), |
51 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," | 52 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," |
52 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," | 53 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," |
53 "\"uri\":\"test-lib\"," | 54 "\"uri\":\"test-lib\"," |
54 "\"_kind\":\"script\"},\"hits\":[]}", lib.index()); | 55 "\"_kind\":\"script\"},\"hits\":[]}", lib.index()); |
55 EXPECT_SUBSTRING(buf, js.ToCString()); | 56 EXPECT_SUBSTRING(buf, js.ToCString()); |
56 } | 57 } |
57 | 58 |
58 | 59 |
59 TEST_CASE(Coverage_MainWithClass) { | 60 VM_TEST_CASE(Coverage_MainWithClass) { |
60 const char* kScript = | 61 const char* kScript = |
61 "class Foo {\n" | 62 "class Foo {\n" |
62 " var x;\n" | 63 " var x;\n" |
63 " Foo(this.x);\n" | 64 " Foo(this.x);\n" |
64 " bar() {\n" | 65 " bar() {\n" |
65 " x = x * x;\n" | 66 " x = x * x;\n" |
66 " x = x / 13;\n" | 67 " x = x / 13;\n" |
67 " }\n" | 68 " }\n" |
68 "}\n" | 69 "}\n" |
69 "main() {\n" | 70 "main() {\n" |
(...skipping 23 matching lines...) Expand all Loading... |
93 // Data for the fake class containing main(). | 94 // Data for the fake class containing main(). |
94 OS::SNPrint(buf, sizeof(buf), | 95 OS::SNPrint(buf, sizeof(buf), |
95 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," | 96 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," |
96 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," | 97 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," |
97 "\"uri\":\"test-lib\"," | 98 "\"uri\":\"test-lib\"," |
98 "\"_kind\":\"script\"},\"hits\":[10,1,11,1]}", lib.index()); | 99 "\"_kind\":\"script\"},\"hits\":[10,1,11,1]}", lib.index()); |
99 EXPECT_SUBSTRING(buf, js.ToCString()); | 100 EXPECT_SUBSTRING(buf, js.ToCString()); |
100 } | 101 } |
101 | 102 |
102 | 103 |
103 TEST_CASE(Coverage_FilterFunction) { | 104 VM_TEST_CASE(Coverage_FilterFunction) { |
104 const char* kScript = | 105 const char* kScript = |
105 "class Foo {\n" | 106 "class Foo {\n" |
106 " var x;\n" | 107 " var x;\n" |
107 " var y;\n" | 108 " var y;\n" |
108 " Foo(this.x);\n" | 109 " Foo(this.x);\n" |
109 " Foo.other(this.x, this.y);\n" | 110 " Foo.other(this.x, this.y);\n" |
110 " Foo.yetAnother();\n" | 111 " Foo.yetAnother();\n" |
111 "}\n" | 112 "}\n" |
112 "main() {\n" | 113 "main() {\n" |
113 " var foo = new Foo(7);\n" | 114 " var foo = new Foo(7);\n" |
(...skipping 16 matching lines...) Expand all Loading... |
130 char buf[1024]; | 131 char buf[1024]; |
131 OS::SNPrint(buf, sizeof(buf), | 132 OS::SNPrint(buf, sizeof(buf), |
132 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," | 133 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," |
133 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," | 134 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," |
134 "\"uri\":\"test-lib\"," | 135 "\"uri\":\"test-lib\"," |
135 "\"_kind\":\"script\"},\"hits\":[6,0]}", lib.index()); | 136 "\"_kind\":\"script\"},\"hits\":[6,0]}", lib.index()); |
136 EXPECT_SUBSTRING(buf, js.ToCString()); | 137 EXPECT_SUBSTRING(buf, js.ToCString()); |
137 } | 138 } |
138 | 139 |
139 } // namespace dart | 140 } // namespace dart |
OLD | NEW |