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