11 #include "gtest/gtest.h"
21 struct _FlTestMethodCodec {
22 FlMethodCodec parent_instance;
27 fl_method_codec_get_type())
33 static_cast<const gchar*
>(g_bytes_get_data(message, &data_length));
34 return g_strndup(data, data_length);
39 return g_bytes_new(text, strlen(text));
47 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
49 g_autofree gchar* text =
nullptr;
51 text = g_strdup_printf(
"%s()", name);
53 text = g_strdup_printf(
"%s(%" G_GINT64_FORMAT
")", name,
70 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
74 if (strcmp(m,
"error") == 0) {
89 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
91 g_autofree gchar* text =
nullptr;
93 text = g_strdup(
"(null)");
108 const gchar* message,
111 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
119 g_autofree gchar* text =
nullptr;
120 if (message ==
nullptr) {
121 if (details ==
nullptr ||
123 text = g_strdup_printf(
"Error_%s()", code);
125 text = g_strdup_printf(
"Error_%s(%" G_GINT64_FORMAT
")", code,
129 if (details ==
nullptr ||
131 text = g_strdup_printf(
"Error_%s(%s)", code, message);
133 text = g_strdup_printf(
"Error_%s(%s,%" G_GINT64_FORMAT
")", code, message,
145 EXPECT_TRUE(FL_IS_TEST_METHOD_CODEC(codec));
148 if (strcmp(m,
"codec-error") == 0) {
152 }
else if (strcmp(m,
"error") == 0) {
154 return FL_METHOD_RESPONSE(
163 FL_METHOD_CODEC_CLASS(klass)->encode_method_call =
165 FL_METHOD_CODEC_CLASS(klass)->decode_method_call =
167 FL_METHOD_CODEC_CLASS(klass)->encode_success_envelope =
169 FL_METHOD_CODEC_CLASS(klass)->encode_error_envelope =
177 return FL_TEST_METHOD_CODEC(
178 g_object_new(fl_test_method_codec_get_type(),
nullptr));
181 TEST(FlMethodCodecTest, EncodeMethodCall) {
184 g_autoptr(GError)
error =
nullptr;
186 FL_METHOD_CODEC(codec),
"foo",
nullptr, &
error);
187 EXPECT_EQ(
error,
nullptr);
188 EXPECT_NE(message,
nullptr);
191 EXPECT_STREQ(message_text,
"foo()");
194 TEST(FlMethodCodecTest, EncodeMethodCallEmptyName) {
197 g_autoptr(GError)
error =
nullptr;
199 FL_METHOD_CODEC(codec),
"",
nullptr, &
error);
200 EXPECT_EQ(
error,
nullptr);
201 EXPECT_NE(message,
nullptr);
204 EXPECT_STREQ(message_text,
"()");
207 TEST(FlMethodCodecTest, EncodeMethodCallArgs) {
211 g_autoptr(GError)
error =
nullptr;
213 FL_METHOD_CODEC(codec),
"foo",
args, &
error);
214 EXPECT_EQ(
error,
nullptr);
215 EXPECT_NE(message,
nullptr);
218 EXPECT_STREQ(message_text,
"foo(42)");
221 TEST(FlMethodCodecTest, EncodeMethodCallError) {
225 g_autoptr(GError)
error =
nullptr;
227 FL_METHOD_CODEC(codec),
"foo",
args, &
error);
228 EXPECT_EQ(message,
nullptr);
233 TEST(FlMethodCodecTest, DecodeMethodCall) {
238 g_autofree gchar* name =
nullptr;
240 g_autoptr(GError)
error =
nullptr;
242 FL_METHOD_CODEC(codec), message, &name, &
args, &
error);
243 EXPECT_EQ(
error,
nullptr);
246 EXPECT_STREQ(name,
"foo");
250 TEST(FlMethodCodecTest, EncodeSuccessEnvelope) {
254 g_autoptr(GError)
error =
nullptr;
257 EXPECT_EQ(
error,
nullptr);
258 EXPECT_NE(message,
nullptr);
261 EXPECT_STREQ(message_text,
"42");
264 TEST(FlMethodCodecTest, EncodeSuccessEnvelopeEmpty) {
267 g_autoptr(GError)
error =
nullptr;
269 FL_METHOD_CODEC(codec),
nullptr, &
error);
270 EXPECT_EQ(
error,
nullptr);
271 EXPECT_NE(message,
nullptr);
274 EXPECT_STREQ(message_text,
"(null)");
277 TEST(FlMethodCodecTest, EncodeSuccessEnvelopeError) {
281 g_autoptr(GError)
error =
nullptr;
284 EXPECT_EQ(message,
nullptr);
289 TEST(FlMethodCodecTest, EncodeErrorEnvelopeNoMessageOrDetails) {
292 g_autoptr(GError)
error =
nullptr;
294 FL_METHOD_CODEC(codec),
"code",
nullptr,
nullptr, &
error);
295 EXPECT_EQ(
error,
nullptr);
296 EXPECT_NE(message,
nullptr);
299 EXPECT_STREQ(message_text,
"Error_code()");
302 TEST(FlMethodCodecTest, EncodeErrorEnvelopeMessage) {
305 g_autoptr(GError)
error =
nullptr;
307 FL_METHOD_CODEC(codec),
"code",
"message",
nullptr, &
error);
308 EXPECT_EQ(
error,
nullptr);
309 EXPECT_NE(message,
nullptr);
312 EXPECT_STREQ(message_text,
"Error_code(message)");
315 TEST(FlMethodCodecTest, EncodeErrorEnvelopeDetails) {
319 g_autoptr(GError)
error =
nullptr;
321 FL_METHOD_CODEC(codec),
"code",
nullptr, details, &
error);
322 EXPECT_EQ(
error,
nullptr);
323 EXPECT_NE(message,
nullptr);
326 EXPECT_STREQ(message_text,
"Error_code(42)");
329 TEST(FlMethodCodecTest, EncodeErrorEnvelopeMessageAndDetails) {
333 g_autoptr(GError)
error =
nullptr;
335 FL_METHOD_CODEC(codec),
"code",
"message", details, &
error);
336 EXPECT_EQ(
error,
nullptr);
337 EXPECT_NE(message,
nullptr);
340 EXPECT_STREQ(message_text,
"Error_code(message,42)");
343 TEST(FlMethodCodecTest, DecodeResponseSuccess) {
348 g_autoptr(GError)
error =
nullptr;
349 g_autoptr(FlMethodResponse) response =
351 EXPECT_EQ(
error,
nullptr);
352 EXPECT_NE(response,
nullptr);
353 ASSERT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
355 FL_METHOD_SUCCESS_RESPONSE(response));
356 ASSERT_NE(
result,
nullptr);
361 TEST(FlMethodCodecTest, DecodeResponseNotImplemented) {
364 g_autoptr(GBytes) message = g_bytes_new(
nullptr, 0);
366 g_autoptr(GError)
error =
nullptr;
367 g_autoptr(FlMethodResponse) response =
369 EXPECT_EQ(
error,
nullptr);
370 EXPECT_NE(response,
nullptr);
371 ASSERT_TRUE(FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response));
374 TEST(FlMethodCodecTest, DecodeResponseCodecError) {
379 g_autoptr(GError)
error =
nullptr;
380 g_autoptr(FlMethodResponse) response =
384 EXPECT_EQ(response,
nullptr);
387 TEST(FlMethodCodecTest, DecodeResponseError) {
392 g_autoptr(GError)
error =
nullptr;
393 g_autoptr(FlMethodResponse) response =
395 EXPECT_EQ(
error,
nullptr);
396 EXPECT_NE(response,
nullptr);
397 ASSERT_TRUE(FL_METHOD_ERROR_RESPONSE(response));
406 ASSERT_NE(details,
nullptr);