forked from microsoft/proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_integration_tests.cpp
169 lines (144 loc) · 4.73 KB
/
proxy_integration_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <gtest/gtest.h>
#include <iomanip>
#include <memory>
#include <memory_resource>
#include <numbers>
#include <sstream>
#include <string>
#include <vector>
#include "proxy.h"
namespace {
namespace spec {
PRO_DEF_MEM_DISPATCH(MemDraw, Draw);
PRO_DEF_MEM_DISPATCH(MemArea, Area);
struct Drawable : pro::facade_builder
::add_convention<MemDraw, void(std::ostream&)>
::add_convention<MemArea, double() noexcept>
::build {};
PRO_DEF_MEM_DISPATCH(MemLog, Log);
struct Logger : pro::facade_builder
::add_convention<MemLog, void(const char*), void(const char*, const std::exception&)>
::build {};
} // namespace spec
class Rectangle {
public:
explicit Rectangle(double width, double height) : width_(width), height_(height) {}
Rectangle(const Rectangle&) = default;
void Draw(std::ostream& out) const
{ out << "{Rectangle: width = " << width_ << ", height = " << height_ << "}"; }
double Area() const noexcept { return width_ * height_; }
private:
double width_;
double height_;
};
class Circle {
public:
explicit Circle(double radius) : radius_(radius) {}
Circle(const Circle&) = default;
void Draw(std::ostream& out) const { out << "{Circle: radius = " << radius_ << "}"; }
double Area() const noexcept { return std::numbers::pi * radius_ * radius_; }
private:
double radius_;
};
class Point {
public:
void Draw(std::ostream& out) const { out << "{Point}"; }
constexpr double Area() const noexcept { return 0; }
};
std::string PrintDrawableToString(pro::proxy<spec::Drawable> p) {
std::stringstream result;
result << std::fixed << std::setprecision(5) << "shape = ";
p->Draw(result);
result << ", area = " << p->Area();
return std::move(result).str();
}
std::vector<std::string> ParseCommand(const std::string& s) {
std::vector<std::string> result(1u);
bool in_quote = false;
std::size_t last_valid = s.find_last_not_of(' ');
for (std::size_t i = 0u; i <= last_valid; ++i) {
if (s[i] == '`' && i < last_valid) {
result.back() += s[++i];
} else if (s[i] == '"') {
in_quote = !in_quote;
} else if (s[i] == ' ' && !in_quote) {
if (!result.back().empty()) {
result.emplace_back();
}
} else {
result.back() += s[i];
}
}
if (result.back().empty()) {
result.pop_back();
}
return result;
}
pro::proxy<spec::Drawable> MakeDrawableFromCommand(const std::string& s) {
std::vector<std::string> parsed = ParseCommand(s);
if (!parsed.empty()) {
if (parsed[0u] == "Rectangle") {
if (parsed.size() == 3u) {
static std::pmr::unsynchronized_pool_resource rectangle_memory_pool;
std::pmr::polymorphic_allocator<> alloc{&rectangle_memory_pool};
return pro::allocate_proxy<spec::Drawable, Rectangle>(alloc, std::stod(parsed[1u]), std::stod(parsed[2u]));
}
} else if (parsed[0u] == "Circle") {
if (parsed.size() == 2u) {
Circle circle{std::stod(parsed[1u])};
return pro::make_proxy<spec::Drawable>(circle);
}
} else if (parsed[0u] == "Point") {
if (parsed.size() == 1u) {
static Point instance;
return &instance;
}
}
}
throw std::runtime_error{"Invalid command"};
}
class StreamLogger {
public:
explicit StreamLogger(std::ostream& out) : out_(&out) {}
StreamLogger(const StreamLogger&) = default;
void Log(const char* s) {
*out_ << "[INFO] " << s << "\n";
}
void Log(const char* s, const std::exception& e) {
*out_ << "[ERROR] " << s << " (exception info: " << e.what() << ")\n";
}
private:
std::ostream* out_;
};
} // namespace
TEST(ProxyIntegrationTests, TestDrawable) {
pro::proxy<spec::Drawable> p = MakeDrawableFromCommand("Rectangle 2 3");
std::string s = PrintDrawableToString(std::move(p));
ASSERT_EQ(s, "shape = {Rectangle: width = 2.00000, height = 3.00000}, area = 6.00000");
p = MakeDrawableFromCommand("Circle 1");
s = PrintDrawableToString(std::move(p));
ASSERT_EQ(s, "shape = {Circle: radius = 1.00000}, area = 3.14159");
p = MakeDrawableFromCommand("Point");
s = PrintDrawableToString(std::move(p));
ASSERT_EQ(s, "shape = {Point}, area = 0.00000");
try {
p = MakeDrawableFromCommand("Triangle 2 3");
} catch (const std::runtime_error& e) {
ASSERT_STREQ(e.what(), "Invalid command");
}
}
TEST(ProxyIntegrationTests, TestLogger) {
std::ostringstream out;
auto logger = pro::make_proxy<spec::Logger, StreamLogger>(out);
logger->Log("hello");
try {
throw std::runtime_error{"runtime error!"};
} catch (const std::exception& e) {
logger->Log("world", e);
}
auto content = std::move(out).str();
ASSERT_EQ(content, "[INFO] hello\n\
[ERROR] world (exception info: runtime error!)\n");
}