WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 331d12b

Browse files
committed
PROTON-2861: [C++] Use idiomatic raw strings now that we can
1 parent 6b95b8b commit 331d12b

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

cpp/src/connect_config_test.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ string configure(connection_options& opts, const string& config) {
8888
void test_default_file() {
8989
// Default file locations in order of preference.
9090
::setenv("MESSAGING_CONNECT_FILE", "environment", 1);
91-
ofstream("connect.json") << "{ \"host\": \"current\" }" << endl;
91+
ofstream("connect.json") << R"({ "host": "current" })" << endl;
9292
::setenv("HOME", "testdata", 1);
93-
ofstream("testdata/.config/messaging/connect.json") << "{ \"host\": \".config\" }" << endl;
93+
ofstream("testdata/.config/messaging/connect.json") << R"({ "host": ".config" })" << endl;
9494
ASSERT_EQUAL("environment", connect_config::default_file());
9595
::unsetenv("MESSAGING_CONNECT_FILE");
9696
ASSERT_EQUAL("connect.json", connect_config::default_file());
@@ -106,28 +106,28 @@ void test_default_file() {
106106

107107
void test_addr() {
108108
connection_options opts;
109-
ASSERT_EQUAL("foo:bar", configure(opts, "{ \"host\":\"foo\", \"port\":\"bar\" }"));
110-
ASSERT_EQUAL("foo:1234", configure(opts, "{ \"host\":\"foo\", \"port\":\"1234\" }"));
109+
ASSERT_EQUAL("foo:bar", configure(opts, R"({ "host":"foo", "port":"bar" })"));
110+
ASSERT_EQUAL("foo:1234", configure(opts, R"({ "host":"foo", "port":"1234" })"));
111111
ASSERT_EQUAL("localhost:amqps", configure(opts, "{}"));
112-
ASSERT_EQUAL("localhost:amqp", configure(opts, "{\"scheme\":\"amqp\"}"));
113-
ASSERT_EQUAL("foo:bar", configure(opts, "{ \"host\":\"foo\", /* inline comment */\"port\":\"bar\" // end of line comment\n}"));
114-
115-
ASSERT_THROWS_MSG(error, "'scheme' must be", configure(opts, "{\"scheme\":\"bad\"}"));
116-
ASSERT_THROWS_MSG(error, "'scheme' expected string, found boolean", configure(opts, "{\"scheme\":true}"));
117-
ASSERT_THROWS_MSG(error, "'port' expected string or uint, found boolean", configure(opts, "{\"port\":true}"));
118-
ASSERT_THROWS_MSG(error, "'host' expected string, found boolean", configure(opts, "{\"host\":true}"));
112+
ASSERT_EQUAL("localhost:amqp", configure(opts, R"({"scheme":"amqp"})"));
113+
ASSERT_EQUAL("foo:bar", configure(opts, R"(
114+
{ "host":"foo", /* inline comment */"port":"bar" // end of line comment
119115
}
116+
)"));
120117

121-
// Hack to write strings with embedded '"' and newlines
122-
#define RAW_STRING(...) #__VA_ARGS__
118+
ASSERT_THROWS_MSG(error, "'scheme' must be", configure(opts, R"({"scheme":"bad"})"));
119+
ASSERT_THROWS_MSG(error, "'scheme' expected string, found boolean", configure(opts, R"({"scheme":true})"));
120+
ASSERT_THROWS_MSG(error, "'port' expected string or uint, found boolean", configure(opts, R"({"port":true})"));
121+
ASSERT_THROWS_MSG(error, "'host' expected string, found boolean", configure(opts, R"({"host":true})"));
122+
}
123123

124124
void test_invalid_config() {
125125
connection_options opts;
126-
ASSERT_THROWS_MSG(proton::error, "expected string", configure(opts, RAW_STRING({ "scheme":true})));
127-
ASSERT_THROWS_MSG(proton::error, "expected object", configure(opts, RAW_STRING({ "tls":""})));
128-
ASSERT_THROWS_MSG(proton::error, "expected object", configure(opts, RAW_STRING({ "sasl":true})));
129-
ASSERT_THROWS_MSG(proton::error, "expected boolean", configure(opts, RAW_STRING({ "sasl": { "enable":""}})));
130-
ASSERT_THROWS_MSG(proton::error, "expected boolean", configure(opts, RAW_STRING({ "tls": { "verify":""}})));
126+
ASSERT_THROWS_MSG(proton::error, "expected string", configure(opts, R"({ "scheme":true})"));
127+
ASSERT_THROWS_MSG(proton::error, "expected object", configure(opts, R"({ "tls":""})"));
128+
ASSERT_THROWS_MSG(proton::error, "expected object", configure(opts, R"({ "sasl":true})"));
129+
ASSERT_THROWS_MSG(proton::error, "expected boolean", configure(opts, R"({ "sasl": { "enable":""}})"));
130+
ASSERT_THROWS_MSG(proton::error, "expected boolean", configure(opts, R"({ "tls": { "verify":""}})"));
131131
}
132132

133133
void test_invalid_json() {
@@ -138,11 +138,11 @@ void test_invalid_json() {
138138
if (std::make_tuple(JSONCPP_VERSION_MAJOR, JSONCPP_VERSION_MINOR) < std::make_tuple(1, 7)) {
139139
ASSERT_THROWS_MSG(proton::error, "reader error", configure(opts, "{"));
140140
ASSERT_THROWS_MSG(proton::error, "reader error", configure(opts, ""));
141-
ASSERT_THROWS_MSG(proton::error, "reader error", configure(opts, RAW_STRING({ "user" : "x" "host" : "y"})));
141+
ASSERT_THROWS_MSG(proton::error, "reader error", configure(opts, R"({ "user" : "x" "host" : "y"})"));
142142
} else {
143143
ASSERT_THROWS_MSG(proton::error, "Missing '}'", configure(opts, "{"));
144144
ASSERT_THROWS_MSG(proton::error, "Syntax error", configure(opts, ""));
145-
ASSERT_THROWS_MSG(proton::error, "Missing ','", configure(opts, RAW_STRING({ "user":"x" "host":"y"})));
145+
ASSERT_THROWS_MSG(proton::error, "Missing ','", configure(opts, R"({ "user":"x" "host":"y"})"));
146146
}
147147
}
148148

@@ -231,7 +231,7 @@ class test_almost_default_connect : public test_handler {
231231

232232
void on_listener_start(container& c) override {
233233
ofstream os("connect.json");
234-
ASSERT(os << config_with_port(RAW_STRING("scheme":"amqp")));
234+
ASSERT(os << config_with_port(R"("scheme":"amqp")"));
235235
os.close();
236236
c.connect();
237237
}
@@ -282,7 +282,7 @@ class test_host_user_pass : public test_handler {
282282
public:
283283

284284
void on_listener_start(proton::container & c) override {
285-
connect(c, RAW_STRING("scheme":"amqp", "host":"127.0.0.1", "user":"user@proton", "password":"password"));
285+
connect(c, R"("scheme":"amqp", "host":"127.0.0.1", "user":"user@proton", "password":"password")");
286286
}
287287

288288
void check_connection(connection& c) override {
@@ -310,7 +310,7 @@ class test_tls : public test_handler {
310310
test_tls() : test_handler(make_opts()) {}
311311

312312
void on_listener_start(proton::container & c) override {
313-
connect(c, RAW_STRING("scheme":"amqps", "tls": { "verify":false }));
313+
connect(c, R"("scheme":"amqps", "tls": { "verify":false })");
314314
}
315315
};
316316

@@ -330,7 +330,7 @@ class test_tls_default_fail : public test_handler {
330330
test_tls_default_fail() : test_handler(make_opts()), failed_(false) {}
331331

332332
void on_listener_start(proton::container& c) override {
333-
connect(c, RAW_STRING("scheme":"amqps"));
333+
connect(c, R"("scheme":"amqps")");
334334
}
335335

336336
void on_messaging_error(const proton::error_condition& c) override {
@@ -366,14 +366,15 @@ class test_tls_external : public test_handler {
366366
test_tls_external() : test_handler(make_opts()) {}
367367

368368
void on_listener_start(container& c) override {
369-
connect(c, RAW_STRING(
369+
connect(c, R"(
370370
"scheme":"amqps",
371371
"sasl":{ "mechanisms": "EXTERNAL" },
372372
"tls": {
373373
"cert":"testdata/certs/client-certificate.pem",
374374
"key":"testdata/certs/client-private-key-no-password.pem",
375375
"ca":"testdata/certs/ca-certificate.pem",
376-
"verify":true }));
376+
"verify":true })"
377+
);
377378
}
378379
};
379380

@@ -396,14 +397,15 @@ class test_tls_plain : public test_handler {
396397
test_tls_plain() : test_handler(make_opts()) {}
397398

398399
void on_listener_start(container& c) override {
399-
connect(c, RAW_STRING(
400+
connect(c, R"(
400401
"scheme":"amqps", "user":"user@proton", "password": "password",
401402
"sasl":{ "mechanisms": "PLAIN" },
402403
"tls": {
403404
"cert":"testdata/certs/client-certificate.pem",
404405
"key":"testdata/certs/client-private-key-no-password.pem",
405406
"ca":"testdata/certs/ca-certificate.pem",
406-
"verify":true }));
407+
"verify":true })"
408+
);
407409
}
408410
};
409411

0 commit comments

Comments
 (0)