OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #include "vm/coverage.h" | |
6 #include "vm/dart_api_impl.h" | |
7 #include "vm/unit_test.h" | |
8 | |
9 namespace dart { | |
10 | |
11 #ifndef PRODUCT | |
12 | |
13 static RawObject* ExecuteScript(const char* script) { | |
14 TransitionVMToNative transition(Thread::Current()); | |
15 Dart_Handle h_lib = TestCase::LoadTestScript(script, NULL); | |
16 EXPECT_VALID(h_lib); | |
17 Library& lib = Library::Handle(); | |
18 lib ^= Api::UnwrapHandle(h_lib); | |
19 EXPECT(!lib.IsNull()); | |
20 Dart_Handle result = Dart_Invoke(h_lib, NewString("main"), 0, NULL); | |
21 EXPECT_VALID(result); | |
22 return Api::UnwrapHandle(h_lib); | |
23 } | |
24 | |
25 | |
26 class FunctionCoverageFilter : public CoverageFilter { | |
27 public: | |
28 explicit FunctionCoverageFilter(const Function& func) : func_(func) {} | |
29 bool ShouldOutputCoverageFor(const Library& lib, | |
30 const Script& script, | |
31 const Class& cls, | |
32 const Function& func) const { | |
33 return func.raw() == func_.raw(); | |
34 } | |
35 private: | |
36 const Function& func_; | |
37 }; | |
38 | |
39 | |
40 VM_TEST_CASE(Coverage_Empty) { | |
41 const char* kScript = | |
42 "main() {\n" | |
43 "}"; | |
44 | |
45 Library& lib = Library::Handle(); | |
46 lib ^= ExecuteScript(kScript); | |
47 ASSERT(!lib.IsNull()); | |
48 | |
49 JSONStream js; | |
50 CodeCoverage::PrintJSON(thread, &js, NULL, false); | |
51 | |
52 char buf[1024]; | |
53 OS::SNPrint(buf, sizeof(buf), | |
54 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," | |
55 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," | |
56 "\"uri\":\"test-lib\"," | |
57 "\"_kind\":\"script\"},\"hits\":[]}", lib.index()); | |
58 EXPECT_SUBSTRING(buf, js.ToCString()); | |
59 } | |
60 | |
61 | |
62 VM_TEST_CASE(Coverage_MainWithClass) { | |
63 const char* kScript = | |
64 "class Foo {\n" | |
65 " var x;\n" | |
66 " Foo(this.x);\n" | |
67 " bar() {\n" | |
68 " x = x * x;\n" | |
69 " x = x / 13;\n" | |
70 " }\n" | |
71 "}\n" | |
72 "main() {\n" | |
73 " var foo = new Foo(7);\n" | |
74 " foo.bar();\n" | |
75 "}\n"; | |
76 | |
77 Library& lib = Library::Handle(); | |
78 lib ^= ExecuteScript(kScript); | |
79 ASSERT(!lib.IsNull()); | |
80 | |
81 JSONStream js; | |
82 CodeCoverage::PrintJSON(thread, &js, NULL, false); | |
83 | |
84 char buf[1024]; | |
85 // Coverage data is printed per class, i.e., there should be two sections | |
86 // for test-lib in the JSON data. | |
87 | |
88 // Data for the actual class Foo. | |
89 OS::SNPrint(buf, sizeof(buf), | |
90 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," | |
91 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," | |
92 "\"uri\":\"test-lib\"," | |
93 "\"_kind\":\"script\"},\"hits\":[3,1,5,4,6,3]}", lib.index()); | |
94 EXPECT_SUBSTRING(buf, js.ToCString()); | |
95 | |
96 // Data for the fake class containing main(). | |
97 OS::SNPrint(buf, sizeof(buf), | |
98 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," | |
99 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," | |
100 "\"uri\":\"test-lib\"," | |
101 "\"_kind\":\"script\"},\"hits\":[10,1,11,1]}", lib.index()); | |
102 EXPECT_SUBSTRING(buf, js.ToCString()); | |
103 } | |
104 | |
105 | |
106 VM_TEST_CASE(Coverage_FilterFunction) { | |
107 const char* kScript = | |
108 "class Foo {\n" | |
109 " var x;\n" | |
110 " var y;\n" | |
111 " Foo(this.x);\n" | |
112 " Foo.other(this.x, this.y);\n" | |
113 " Foo.yetAnother();\n" | |
114 "}\n" | |
115 "main() {\n" | |
116 " var foo = new Foo(7);\n" | |
117 "}\n"; | |
118 | |
119 Library& lib = Library::Handle(); | |
120 lib ^= ExecuteScript(kScript); | |
121 ASSERT(!lib.IsNull()); | |
122 const Class& cls = Class::Handle( | |
123 lib.LookupClass(String::Handle(String::New("Foo")))); | |
124 ASSERT(!cls.IsNull()); | |
125 const Function& func = Function::Handle( | |
126 cls.LookupFunction(String::Handle(String::New("Foo.yetAnother")))); | |
127 ASSERT(!func.IsNull()); | |
128 | |
129 JSONStream js; | |
130 FunctionCoverageFilter filter(func); | |
131 CodeCoverage::PrintJSON(thread, &js, &filter, false); | |
132 // Only expect coverage data for Foo.yetAnother() on line 6. | |
133 char buf[1024]; | |
134 OS::SNPrint(buf, sizeof(buf), | |
135 "{\"source\":\"test-lib\",\"script\":{\"type\":\"@Script\"," | |
136 "\"fixedId\":true,\"id\":\"libraries\\/%" Pd "\\/scripts\\/test-lib\"," | |
137 "\"uri\":\"test-lib\"," | |
138 "\"_kind\":\"script\"},\"hits\":[6,0]}", lib.index()); | |
139 EXPECT_SUBSTRING(buf, js.ToCString()); | |
140 } | |
141 | |
142 #endif // !PRODUCT | |
143 | |
144 } // namespace dart | |
OLD | NEW |