1 : // Copyright 2012 Google Inc.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/grinder/lcov_writer.h"
16 :
17 : #include "base/file_util.h"
18 : #include "gmock/gmock.h"
19 : #include "gtest/gtest.h"
20 :
21 : namespace grinder {
22 :
23 : namespace {
24 :
25 : using testing::ContainerEq;
26 :
27 : class TestLineInfo : public LineInfo {
28 : public:
29 : using LineInfo::source_files_;
30 : using LineInfo::source_lines_;
31 :
32 E : void InitDummyLineInfoFoo() {
33 : LineInfo::SourceFileSet::iterator file_it =
34 E : source_files_.insert("foo.cc").first;
35 E : const std::string* file_name = &(*file_it);
36 :
37 : source_lines_.push_back(LineInfo::SourceLine(
38 E : file_name, 1, core::RelativeAddress(0), 1));
39 : source_lines_.push_back(LineInfo::SourceLine(
40 E : file_name, 2, core::RelativeAddress(1), 1));
41 : source_lines_.push_back(LineInfo::SourceLine(
42 E : file_name, 3, core::RelativeAddress(2), 1));
43 :
44 : // Visits lines 1 and 2.
45 E : Visit(core::RelativeAddress(0), 2);
46 E : }
47 : };
48 :
49 : // A simple utility class for creating and cleaning up a temporary file.
50 : class ScopedTempFile {
51 : public:
52 E : ScopedTempFile() {
53 E : file_util::CreateTemporaryFile(&path_);
54 E : }
55 :
56 E : ~ScopedTempFile() {
57 E : file_util::Delete(path_, false);
58 E : }
59 :
60 E : const FilePath& path() const { return path_; }
61 :
62 : private:
63 : FilePath path_;
64 : };
65 :
66 : } // namespace
67 :
68 : // We provide this so that we can use ContainerEq. It must be outside of the
69 : // anonymous namespace for this to compile.
70 : bool operator==(const LcovWriter::CoverageInfo& lhs,
71 E : const LcovWriter::CoverageInfo& rhs) {
72 E : return lhs.line_execution_count_map == rhs.line_execution_count_map;
73 E : }
74 :
75 E : TEST(LcovWriterTest, Construct) {
76 E : LcovWriter lcov;
77 E : EXPECT_TRUE(lcov.source_file_coverage_info_map().empty());
78 E : }
79 :
80 E : TEST(LcovWriterTest, Add) {
81 E : TestLineInfo line_info;
82 E : ASSERT_NO_FATAL_FAILURE(line_info.InitDummyLineInfoFoo());
83 :
84 E : LcovWriter lcov;
85 E : EXPECT_TRUE(lcov.Add(line_info));
86 :
87 E : LcovWriter::SourceFileCoverageInfoMap expected_coverage_info_map;
88 : LcovWriter::LineExecutionCountMap& expected_line_exec =
89 E : expected_coverage_info_map["foo.cc"].line_execution_count_map;
90 E : expected_line_exec[1] = 1;
91 E : expected_line_exec[2] = 1;
92 E : expected_line_exec[3] = 0;
93 :
94 : EXPECT_THAT(expected_coverage_info_map,
95 E : ContainerEq(lcov.source_file_coverage_info_map()));
96 E : }
97 :
98 E : TEST(LcovWriterTest, Write) {
99 E : TestLineInfo line_info;
100 E : ASSERT_NO_FATAL_FAILURE(line_info.InitDummyLineInfoFoo());
101 :
102 E : LcovWriter lcov;
103 E : EXPECT_TRUE(lcov.Add(line_info));
104 :
105 E : ScopedTempFile temp;
106 E : EXPECT_TRUE(lcov.Write(temp.path()));
107 :
108 E : std::string actual_contents;
109 E : EXPECT_TRUE(file_util::ReadFileToString(temp.path(), &actual_contents));
110 :
111 : std::string expected_contents =
112 : "SF:foo.cc\n"
113 : "DA:1,1\n"
114 : "DA:2,1\n"
115 : "DA:3,0\n"
116 : "LH:2\n"
117 : "LF:3\n"
118 E : "end_of_record\n";
119 :
120 E : EXPECT_EQ(expected_contents, actual_contents);
121 E : }
122 :
123 : } // namespace grinder
|