Skip to content

Commit 3f08ad2

Browse files
sharder996ricab
authored andcommitted
[format utils] create singleton format utils class in order to mock functions
1 parent a179749 commit 3f08ad2

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

Diff for: include/multipass/cli/format_utils.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <algorithm>
2929
#include <string>
3030

31+
#define MP_FORMAT_UTILS multipass::FormatUtils::instance()
32+
3133
namespace multipass
3234
{
3335
class Formatter;
@@ -43,8 +45,6 @@ Formatter* formatter_for(const std::string& format);
4345
template <typename Container>
4446
Container sorted(const Container& items);
4547

46-
std::string convert_to_localtime(const google::protobuf::Timestamp& timestamp);
47-
4848
void filter_aliases(google::protobuf::RepeatedPtrField<multipass::FindReply_AliasInfo>& aliases);
4949

5050
// Computes the column width needed to display all the elements of a range [begin, end). get_width is a function
@@ -60,6 +60,14 @@ static constexpr auto column_width =
6060
return std::max({get_width(*max_width) + col_buffer, header_width + col_buffer, minimum_width});
6161
};
6262
} // namespace format
63+
64+
class FormatUtils : public Singleton<FormatUtils>
65+
{
66+
public:
67+
FormatUtils(const Singleton<FormatUtils>::PrivatePass&) noexcept;
68+
69+
virtual std::string convert_to_localtime(const google::protobuf::Timestamp& timestamp) const;
70+
};
6371
} // namespace multipass
6472

6573
template <typename Container>

Diff for: src/client/cli/formatter/csv_formatter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ std::string generate_snapshot_details(const mp::InfoReply reply)
8585

8686
fmt::format_to(std::back_inserter(buf),
8787
",{},{},{},\"{}\"\n",
88-
mp::format::convert_to_localtime(fundamentals.creation_timestamp()),
88+
MP_FORMAT_UTILS.convert_to_localtime(fundamentals.creation_timestamp()),
8989
fundamentals.parent(),
9090
fmt::join(info.snapshot_info().children(), ";"),
9191
fundamentals.comment());

Diff for: src/client/cli/formatter/format_utils.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ void mp::format::filter_aliases(google::protobuf::RepeatedPtrField<multipass::Fi
111111
}
112112
}
113113

114-
std::string mp::format::convert_to_localtime(const google::protobuf::Timestamp& timestamp) const
114+
mp::FormatUtils::FormatUtils(const Singleton<FormatUtils>::PrivatePass& pass) noexcept
115+
: Singleton<FormatUtils>::Singleton{pass}
116+
{
117+
}
118+
119+
std::string mp::FormatUtils::convert_to_localtime(const google::protobuf::Timestamp& timestamp) const
115120
{
116121
std::ostringstream oss;
117122
std::time_t t = google::protobuf::util::TimeUtil::TimestampToTimeT(timestamp);

Diff for: src/client/cli/formatter/json_formatter.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ QJsonObject generate_snapshot_details(const mp::DetailedInfoItem& item)
7676
}
7777
snapshot_info.insert("mounts", mounts);
7878

79-
snapshot_info.insert("created",
80-
QString::fromStdString(mp::format::convert_to_localtime(fundamentals.creation_timestamp())));
79+
snapshot_info.insert(
80+
"created",
81+
QString::fromStdString(MP_FORMAT_UTILS.convert_to_localtime(fundamentals.creation_timestamp())));
8182
snapshot_info.insert("parent", QString::fromStdString(fundamentals.parent()));
8283

8384
QJsonArray children;

Diff for: src/client/cli/formatter/table_formatter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ std::string generate_snapshot_details(const mp::DetailedInfoItem& item)
8888
fmt::format_to(std::back_inserter(buf),
8989
"{:<16}{}\n",
9090
"Created:",
91-
mp::format::convert_to_localtime(fundamentals.creation_timestamp()));
91+
MP_FORMAT_UTILS.convert_to_localtime(fundamentals.creation_timestamp()));
9292
fmt::format_to(std::back_inserter(buf),
9393
"{:<16}{}\n",
9494
"Parent:",

Diff for: src/client/cli/formatter/yaml_formatter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ YAML::Node generate_snapshot_details(const mp::DetailedInfoItem& item)
7878
}
7979
snapshot_node["mounts"] = mounts;
8080

81-
snapshot_node["created"] = mp::format::convert_to_localtime(fundamentals.creation_timestamp());
81+
snapshot_node["created"] = MP_FORMAT_UTILS.convert_to_localtime(fundamentals.creation_timestamp());
8282
snapshot_node["parent"] = fundamentals.parent().empty() ? YAML::Node() : YAML::Node(fundamentals.parent());
8383

8484
snapshot_node["children"] = YAML::Node(YAML::NodeType::Sequence);

0 commit comments

Comments
 (0)