Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c8cf82b

Browse files
sharder996ricab
authored andcommittedOct 16, 2023
[format utils] create singleton format utils class in order to mock functions
1 parent 59691c9 commit c8cf82b

File tree

6 files changed

+21
-8
lines changed

6 files changed

+21
-8
lines changed
 

‎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
@@ -59,6 +59,14 @@ static constexpr auto column_width = [](const auto begin, const auto end, const
5959
return std::max({get_width(*max_width) + col_buffer, header_width + col_buffer, minimum_width});
6060
};
6161
} // namespace format
62+
63+
class FormatUtils : public Singleton<FormatUtils>
64+
{
65+
public:
66+
FormatUtils(const Singleton<FormatUtils>::PrivatePass&) noexcept;
67+
68+
virtual std::string convert_to_localtime(const google::protobuf::Timestamp& timestamp) const;
69+
};
6270
} // namespace multipass
6371

6472
template <typename Container>

‎src/client/cli/formatter/csv_formatter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ std::string generate_snapshot_details(const mp::InfoReply reply)
7979
fmt::format_to(std::back_inserter(buf), format_mounts(info.mount_info()));
8080

8181
fmt::format_to(std::back_inserter(buf), ",{},{},{},\"{}\"\n",
82-
mp::format::convert_to_localtime(fundamentals.creation_timestamp()), fundamentals.parent(),
82+
MP_FORMAT_UTILS.convert_to_localtime(fundamentals.creation_timestamp()), fundamentals.parent(),
8383
fmt::join(info.snapshot_info().children(), ";"), fundamentals.comment());
8484
}
8585

‎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);

‎src/client/cli/formatter/json_formatter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ 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", QString::fromStdString(MP_FORMAT_UTILS.convert_to_localtime(fundamentals.creation_timestamp())));
8181
snapshot_info.insert("parent", QString::fromStdString(fundamentals.parent()));
8282

8383
QJsonArray children;

‎src/client/cli/formatter/table_formatter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ std::string generate_snapshot_details(const mp::DetailedInfoItem& item)
8383
}
8484

8585
fmt::format_to(std::back_inserter(buf), "{:<16}{}\n",
86-
"Created:", mp::format::convert_to_localtime(fundamentals.creation_timestamp()));
86+
"Created:", MP_FORMAT_UTILS.convert_to_localtime(fundamentals.creation_timestamp()));
8787
fmt::format_to(std::back_inserter(buf), "{:<16}{}\n",
8888
"Parent:", fundamentals.parent().empty() ? "--" : fundamentals.parent());
8989

‎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)