12 #include "gtest/gtest.h"
18 static_cast<const gchar*
>(g_bytes_get_data(message, &data_length));
19 return g_strndup(data, data_length);
24 return g_bytes_new(text, strlen(text));
30 g_autoptr(GError)
error =
nullptr;
32 FL_METHOD_CODEC(codec), name,
args, &
error);
33 EXPECT_NE(message,
nullptr);
34 EXPECT_EQ(
error,
nullptr);
42 g_autoptr(GError)
error =
nullptr;
44 FL_METHOD_CODEC(codec), result, &
error);
45 EXPECT_NE(message,
nullptr);
46 EXPECT_EQ(
error,
nullptr);
53 const gchar* error_message,
56 g_autoptr(GError)
error =
nullptr;
58 FL_METHOD_CODEC(codec), error_code, error_message, details, &
error);
59 EXPECT_NE(message,
nullptr);
60 EXPECT_EQ(
error,
nullptr);
69 g_autoptr(GError)
error =
nullptr;
71 FL_METHOD_CODEC(codec), data, name,
args, &
error);
73 EXPECT_EQ(
error,
nullptr);
82 g_autoptr(GError)
error =
nullptr;
83 g_autofree gchar* name =
nullptr;
86 FL_METHOD_CODEC(codec), data, &name, &
args, &
error);
88 EXPECT_EQ(name,
nullptr);
89 EXPECT_EQ(
args,
nullptr);
90 EXPECT_TRUE(g_error_matches(
error, domain, code));
97 g_autoptr(GError)
error =
nullptr;
98 g_autoptr(FlMethodResponse) response =
100 ASSERT_NE(response,
nullptr);
101 EXPECT_EQ(
error,
nullptr);
102 ASSERT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
104 FL_METHOD_SUCCESS_RESPONSE(response)),
112 const gchar* error_message,
116 g_autoptr(GError)
error =
nullptr;
117 g_autoptr(FlMethodResponse) response =
119 ASSERT_NE(response,
nullptr);
120 EXPECT_EQ(
error,
nullptr);
121 ASSERT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
125 if (error_message ==
nullptr) {
127 FL_METHOD_ERROR_RESPONSE(response)),
131 FL_METHOD_ERROR_RESPONSE(response)),
134 if (details ==
nullptr) {
136 FL_METHOD_ERROR_RESPONSE(response)),
140 FL_METHOD_ERROR_RESPONSE(response)),
149 g_autoptr(GError)
error =
nullptr;
150 g_autoptr(FlMethodResponse) response =
152 EXPECT_EQ(response,
nullptr);
153 EXPECT_TRUE(g_error_matches(
error, domain, code));
156 TEST(FlJsonMethodCodecTest, EncodeMethodCallNullptrArgs) {
158 EXPECT_STREQ(text,
"{\"method\":\"hello\",\"args\":null}");
161 TEST(FlJsonMethodCodecTest, EncodeMethodCallNullArgs) {
164 EXPECT_STREQ(text,
"{\"method\":\"hello\",\"args\":null}");
167 TEST(FlJsonMethodCodecTest, EncodeMethodCallStringArgs) {
170 EXPECT_STREQ(text,
"{\"method\":\"hello\",\"args\":\"world\"}");
173 TEST(FlJsonMethodCodecTest, EncodeMethodCallListArgs) {
178 EXPECT_STREQ(text,
"{\"method\":\"hello\",\"args\":[\"count\",42]}");
181 TEST(FlJsonMethodCodecTest, DecodeMethodCallNoArgs) {
182 g_autofree gchar* name =
nullptr;
185 EXPECT_STREQ(name,
"hello");
186 ASSERT_EQ(
args,
nullptr);
189 TEST(FlJsonMethodCodecTest, DecodeMethodCallNullArgs) {
190 g_autofree gchar* name =
nullptr;
193 EXPECT_STREQ(name,
"hello");
197 TEST(FlJsonMethodCodecTest, DecodeMethodCallStringArgs) {
198 g_autofree gchar* name =
nullptr;
201 EXPECT_STREQ(name,
"hello");
206 TEST(FlJsonMethodCodecTest, DecodeMethodCallListArgs) {
207 g_autofree gchar* name =
nullptr;
211 EXPECT_STREQ(name,
"hello");
224 TEST(FlJsonMethodCodecTest, DecodeMethodCallNoData) {
229 TEST(FlJsonMethodCodecTest, DecodeMethodCallNoMethodOrArgs) {
234 TEST(FlJsonMethodCodecTest, DecodeMethodCallInvalidJson) {
239 TEST(FlJsonMethodCodecTest, DecodeMethodCallWrongType) {
244 TEST(FlJsonMethodCodecTest, DecodeMethodCallNoMethod) {
249 TEST(FlJsonMethodCodecTest, DecodeMethodCallNoTerminator) {
255 TEST(FlJsonMethodCodecTest, DecodeMethodCallExtraData) {
261 TEST(FlJsonMethodCodecTest, EncodeSuccessEnvelopeNullptr) {
263 EXPECT_STREQ(text,
"[null]");
266 TEST(FlJsonMethodCodecTest, EncodeSuccessEnvelopeNull) {
269 EXPECT_STREQ(text,
"[null]");
272 TEST(FlJsonMethodCodecTest, EncodeSuccessEnvelopeString) {
275 EXPECT_STREQ(text,
"[\"hello\"]");
278 TEST(FlJsonMethodCodecTest, EncodeSuccessEnvelopeList) {
283 EXPECT_STREQ(text,
"[[\"count\",42]]");
286 TEST(FlJsonMethodCodecTest, EncodeErrorEnvelopeEmptyCode) {
288 EXPECT_STREQ(text,
"[\"\",null,null]");
291 TEST(FlJsonMethodCodecTest, EncodeErrorEnvelopeNonMessageOrDetails) {
293 EXPECT_STREQ(text,
"[\"error\",null,null]");
296 TEST(FlJsonMethodCodecTest, EncodeErrorEnvelopeMessage) {
298 EXPECT_STREQ(text,
"[\"error\",\"message\",null]");
301 TEST(FlJsonMethodCodecTest, EncodeErrorEnvelopeDetails) {
306 EXPECT_STREQ(text,
"[\"error\",null,[\"count\",42]]");
309 TEST(FlJsonMethodCodecTest, EncodeErrorEnvelopeMessageAndDetails) {
314 EXPECT_STREQ(text,
"[\"error\",\"message\",[\"count\",42]]");
317 TEST(FlJsonMethodCodecTest, DecodeResponseSuccessNull) {
322 TEST(FlJsonMethodCodecTest, DecodeResponseSuccessString) {
327 TEST(FlJsonMethodCodecTest, DecodeResponseSuccessList) {
334 TEST(FlJsonMethodCodecTest, DecodeResponseErrorEmptyCode) {
338 TEST(FlJsonMethodCodecTest, DecodeResponseErrorNoMessageOrDetails) {
343 TEST(FlJsonMethodCodecTest, DecodeResponseErrorMessage) {
348 TEST(FlJsonMethodCodecTest, DecodeResponseErrorDetails) {
356 TEST(FlJsonMethodCodecTest, DecodeResponseErrorMessageAndDetails) {
364 TEST(FlJsonMethodCodecTest, DecodeResponseNotImplemented) {
366 g_autoptr(GBytes) message = g_bytes_new(
nullptr, 0);
367 g_autoptr(GError)
error =
nullptr;
368 g_autoptr(FlMethodResponse) response =
370 ASSERT_NE(response,
nullptr);
371 EXPECT_EQ(
error,
nullptr);
372 EXPECT_TRUE(FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response));
375 TEST(FlJsonMethodCodecTest, DecodeResponseNoTerminator) {
380 TEST(FlJsonMethodCodecTest, DecodeResponseInvalidJson) {
385 TEST(FlJsonMethodCodecTest, DecodeResponseMissingDetails) {
390 TEST(FlJsonMethodCodecTest, DecodeResponseExtraDetails) {
395 TEST(FlJsonMethodCodecTest, DecodeResponseSuccessExtraData) {
400 TEST(FlJsonMethodCodecTest, DecodeResponseErrorExtraData) {
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON
#define FL_JSON_MESSAGE_CODEC_ERROR
G_MODULE_EXPORT FlJsonMethodCodec * fl_json_method_codec_new()
static void decode_error_method_call(const char *text, GQuark domain, gint code)
static gchar * encode_method_call(const gchar *name, FlValue *args)
static void decode_error_response(const char *text, GQuark domain, gint code)
static GBytes * text_to_message(const gchar *text)
TEST(FlJsonMethodCodecTest, EncodeMethodCallNullptrArgs)
static void decode_response_with_success(const char *text, FlValue *result)
static void decode_response_with_error(const char *text, const gchar *code, const gchar *error_message, FlValue *details)
static gchar * encode_success_envelope(FlValue *result)
static gchar * message_to_text(GBytes *message)
static gchar * encode_error_envelope(const gchar *error_code, const gchar *error_message, FlValue *details)
static void decode_method_call(const char *text, gchar **name, FlValue **args)
@ FL_MESSAGE_CODEC_ERROR_FAILED
#define FL_MESSAGE_CODEC_ERROR
GBytes * fl_method_codec_encode_success_envelope(FlMethodCodec *self, FlValue *result, GError **error)
GBytes * fl_method_codec_encode_error_envelope(FlMethodCodec *self, const gchar *code, const gchar *message, FlValue *details, GError **error)
GBytes * fl_method_codec_encode_method_call(FlMethodCodec *self, const gchar *name, FlValue *args, GError **error)
gboolean fl_method_codec_decode_method_call(FlMethodCodec *self, GBytes *message, gchar **name, FlValue **args, GError **error)
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)
G_MODULE_EXPORT const gchar * fl_method_error_response_get_code(FlMethodErrorResponse *self)
G_MODULE_EXPORT const gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
G_MODULE_EXPORT FlValue * fl_method_error_response_get_details(FlMethodErrorResponse *self)
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT FlValue * fl_value_get_list_value(FlValue *self, size_t index)
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
G_MODULE_EXPORT FlValue * fl_value_new_list()
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
G_MODULE_EXPORT FlValue * fl_value_new_null()
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
typedefG_BEGIN_DECLS struct _FlValue FlValue