Added a fake file writer (writes to string instead of file) (#1101)

Added fake filer writer which writes the output to a string instead of a file.

This will help writing tests for classes that use the file writer interface to write data.
master
Martin Schwörer 2018-04-27 13:45:54 +02:00 committed by Wally B. Feed
parent c46fe073b4
commit fe7aaf4d94
5 changed files with 266 additions and 4 deletions

View File

@ -48,6 +48,29 @@ cc_fix_config(
visibility = ["//visibility:private"],
)
TEST_LIBRARY_SRCS = glob([
"**/*test_helpers.cc",
"**/fake_*.cc",
"**/mock_*.cc",
])
TEST_LIBRARY_HDRS = glob([
"**/*test_helpers.h",
"**/fake_*.h",
"**/mock_*.h",
])
cc_library(
name = "cartographer_test_library",
testonly = 1,
srcs = TEST_LIBRARY_SRCS,
hdrs = TEST_LIBRARY_HDRS,
deps = [
":cartographer",
"@com_google_googletest//:gtest",
],
)
cc_library(
name = "cartographer",
srcs = glob(
@ -57,13 +80,16 @@ cc_library(
exclude = [
"**/*_main.cc",
"**/*_test.cc",
],
] + TEST_LIBRARY_SRCS,
),
hdrs = [
"common/config.h",
] + glob([
] + glob(
[
"**/*.h",
]),
],
exclude = TEST_LIBRARY_HDRS,
),
copts = ["-Wno-sign-compare"],
includes = ["."],
deps = [
@ -83,6 +109,7 @@ cc_library(
data = ["//:configuration_files"],
deps = [
":cartographer",
":cartographer_test_library",
"@com_google_googletest//:gtest_main",
],
) for src in glob(

View File

@ -94,6 +94,7 @@ cc_binary(
tags = ["exclusive"],
deps = [
":cartographer_grpc",
"//cartographer:cartographer_test_library",
"@com_google_googletest//:gtest_main",
],
) for src in glob(

View File

@ -0,0 +1,55 @@
/*
* Copyright 2018 The Cartographer Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cartographer/io/fake_file_writer.h"
#include "glog/logging.h"
#include "gtest/gtest.h"
namespace cartographer {
namespace io {
FakeFileWriter::FakeFileWriter(const std::string& filename,
std::shared_ptr<std::vector<char>> content)
: is_closed_(false), content_(content), filename_(filename) {
CHECK(content != nullptr);
}
bool FakeFileWriter::Write(const char* const data, const size_t len) {
EXPECT_FALSE(is_closed_);
content_->insert(content_->end(), data, data + len);
return true;
}
bool FakeFileWriter::Close() {
EXPECT_FALSE(is_closed_);
is_closed_ = true;
return true;
}
bool FakeFileWriter::WriteHeader(const char* const data, const size_t len) {
EXPECT_FALSE(is_closed_);
if (content_->size() < len) {
content_->resize(len);
}
std::copy(data, data + len, content_->begin());
return true;
}
std::string FakeFileWriter::GetFilename() { return filename_; }
} // namespace io
} // namespace cartographer

View File

@ -0,0 +1,50 @@
/*
* Copyright 2018 The Cartographer Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CARTOGRAPHER_IO_FAKE_FILE_WRITER_H_
#define CARTOGRAPHER_IO_FAKE_FILE_WRITER_H_
#include <memory>
#include <string>
#include <vector>
#include "cartographer/io/file_writer.h"
namespace cartographer {
namespace io {
// Fakes a FileWriter by just writing the data to a std::vector<char>.
class FakeFileWriter : public FileWriter {
public:
FakeFileWriter(const std::string& filename,
std::shared_ptr<std::vector<char>> content);
~FakeFileWriter() override = default;
bool WriteHeader(const char* data, size_t len) override;
bool Write(const char* data, size_t len) override;
bool Close() override;
std::string GetFilename() override;
private:
bool is_closed_;
std::shared_ptr<std::vector<char>> content_;
std::string filename_;
};
} // namespace io
} // namespace cartographer
#endif // CARTOGRAPHER_IO_FAKE_FILE_WRITER_H_

View File

@ -0,0 +1,129 @@
/*
* Copyright 2018 The Cartographer Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <vector>
#include "cartographer/io/fake_file_writer.h"
#include "glog/logging.h"
#include "gtest/gtest.h"
namespace cartographer {
namespace io {
namespace {
std::string toString(const std::vector<char>& data) {
return std::string(data.data(), data.size());
}
TEST(FakeFileWriter, Filename) {
auto content = std::make_shared<std::vector<char>>();
FakeFileWriter writer("file", content);
EXPECT_EQ("file", writer.GetFilename());
}
TEST(FakeFileWriter, CloseStream) {
auto content = std::make_shared<std::vector<char>>();
FakeFileWriter writer("file", content);
EXPECT_TRUE(writer.Close());
EXPECT_EQ("", toString(*content));
}
TEST(FakeFileWriter, WriteHeader) {
auto content = std::make_shared<std::vector<char>>();
const std::string header("dummy header");
const std::string header_2("dummy header 2");
FakeFileWriter writer("file", content);
EXPECT_TRUE(writer.WriteHeader(header.c_str(), header.size()));
EXPECT_EQ("dummy header", toString(*content));
EXPECT_TRUE(writer.WriteHeader(header_2.c_str(), header_2.size()));
EXPECT_EQ("dummy header 2", toString(*content));
EXPECT_TRUE(writer.Close());
EXPECT_EQ("dummy header 2", toString(*content));
}
TEST(FakeFileWriter, Write) {
auto content = std::make_shared<std::vector<char>>();
const std::vector<std::string> data_stream = {"data 1", "data 2"};
FakeFileWriter writer("file", content);
for (const auto& data : data_stream) {
EXPECT_TRUE(writer.Write(data.c_str(), data.size()));
}
EXPECT_EQ("data 1data 2", toString(*content));
EXPECT_TRUE(writer.Close());
EXPECT_EQ("data 1data 2", toString(*content));
}
TEST(FakeFileWriter, HeaderAndWrite) {
auto content = std::make_shared<std::vector<char>>();
const std::string header("dummy header");
const std::vector<std::string> data_stream = {"data 1", "data 2"};
FakeFileWriter writer("file", content);
EXPECT_TRUE(writer.WriteHeader(header.c_str(), header.size()));
EXPECT_EQ("dummy header", toString(*content));
for (const auto& data : data_stream) {
EXPECT_TRUE(writer.Write(data.c_str(), data.size()));
}
EXPECT_TRUE(writer.Close());
EXPECT_EQ("dummy headerdata 1data 2", toString(*content));
}
TEST(FakeFileWriter, WriteTerminatedString) {
auto content = std::make_shared<std::vector<char>>();
std::vector<char> data_stream = {'d', 'a', 't', 'a', '\0', ' ', '1'};
FakeFileWriter writer("file", content);
EXPECT_TRUE(writer.Write(data_stream.data(), data_stream.size()));
EXPECT_EQ(data_stream, *content);
}
TEST(FakeFileWriter, WriteTerminatedHeaderString) {
auto content = std::make_shared<std::vector<char>>();
std::vector<char> header = {'h', 'e', 'a', 'd', '\0', ' ', 'e', 'r'};
FakeFileWriter writer("file", content);
EXPECT_TRUE(writer.WriteHeader(header.data(), header.size()));
EXPECT_EQ(header, *content);
}
TEST(FakeFileWriter, HeaderAndWriteTerminatedString) {
auto content = std::make_shared<std::vector<char>>();
std::vector<char> header = {'d', 'a', 't', 'a', '\0', ' ', '1'};
std::vector<char> data = {'h', 'e', 'a', 'd', '\0', ' ', 'e', 'r'};
FakeFileWriter writer("file", content);
EXPECT_TRUE(writer.WriteHeader(header.data(), header.size()));
EXPECT_EQ(header, *content);
EXPECT_TRUE(writer.Write(data.data(), data.size()));
std::vector<char> expected_output = header;
expected_output.insert(expected_output.end(), data.begin(), data.end());
EXPECT_EQ(expected_output, *content);
EXPECT_TRUE(writer.Close());
EXPECT_EQ(expected_output, *content);
}
} // namespace
} // namespace io
} // namespace cartographer