From 502a3c66a62388c2f952360837eb0cfc519739a8 Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Fri, 17 May 2024 14:49:38 +0200 Subject: [PATCH] filterx: test repr_append() Signed-off-by: Attila Szakacs --- lib/filterx/tests/test_object_boolean.c | 3 ++ lib/filterx/tests/test_object_bytes.c | 12 ++++++ lib/filterx/tests/test_object_datetime.c | 7 ++-- lib/filterx/tests/test_object_double.c | 3 ++ lib/filterx/tests/test_object_integer.c | 3 ++ lib/filterx/tests/test_object_json.c | 25 ++++++++++++ lib/filterx/tests/test_object_message.c | 51 ++++++++++++++++-------- lib/filterx/tests/test_object_null.c | 4 ++ 8 files changed, 89 insertions(+), 19 deletions(-) diff --git a/lib/filterx/tests/test_object_boolean.c b/lib/filterx/tests/test_object_boolean.c index b2fd6eab62..d4c6e1634f 100644 --- a/lib/filterx/tests/test_object_boolean.c +++ b/lib/filterx/tests/test_object_boolean.c @@ -124,8 +124,11 @@ Test(filterx_boolean, test_filterx_boolean_repr) { FilterXObject *obj = filterx_boolean_new(TRUE); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(obj, repr)); cr_assert_str_eq("true", repr->str); + cr_assert(filterx_object_repr_append(obj, repr)); + cr_assert_str_eq("truetrue", repr->str); filterx_object_unref(obj); } diff --git a/lib/filterx/tests/test_object_bytes.c b/lib/filterx/tests/test_object_bytes.c index b4c3d6d7fc..040d0484e6 100644 --- a/lib/filterx/tests/test_object_bytes.c +++ b/lib/filterx/tests/test_object_bytes.c @@ -122,6 +122,18 @@ Test(filterx_bytes, test_filterx_bytes_typecast_from_protobuf) filterx_object_unref(obj); } +Test(filterx_bytes, filterx_bytes_repr) +{ + FilterXObject *obj = filterx_bytes_new("\0\1\2\3", 4); + GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); + cr_assert(filterx_object_repr(obj, repr)); + cr_assert_str_eq("00010203", repr->str); + cr_assert(filterx_object_repr_append(obj, repr)); + cr_assert_str_eq("0001020300010203", repr->str); + filterx_object_unref(obj); +} + static void setup(void) { diff --git a/lib/filterx/tests/test_object_datetime.c b/lib/filterx/tests/test_object_datetime.c index a760c4142d..6f3d2fc3f9 100644 --- a/lib/filterx/tests/test_object_datetime.c +++ b/lib/filterx/tests/test_object_datetime.c @@ -172,9 +172,8 @@ Test(filterx_datetime, test_filterx_datetime_typecast_from_datetime) Test(filterx_datetime, test_filterx_datetime_repr) { - const gchar *test_time_str = "2024-03-18T12:34:13+234"; GPtrArray *args = g_ptr_array_new_with_free_func((GDestroyNotify) filterx_object_unref); - FilterXObject *in = filterx_string_new(test_time_str, -1); + FilterXObject *in = filterx_string_new("2024-03-18T12:34:13+234", -1); g_ptr_array_add(args, in); FilterXObject *obj = filterx_typecast_datetime(args); @@ -182,9 +181,11 @@ Test(filterx_datetime, test_filterx_datetime_repr) cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(datetime))); GString *repr = scratch_buffers_alloc(); - + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(obj, repr)); cr_assert_str_eq("2024-03-18T12:34:13.000+09:00", repr->str); + cr_assert(filterx_object_repr_append(obj, repr)); + cr_assert_str_eq("2024-03-18T12:34:13.000+09:002024-03-18T12:34:13.000+09:00", repr->str); g_ptr_array_free(args, TRUE); filterx_object_unref(obj); diff --git a/lib/filterx/tests/test_object_double.c b/lib/filterx/tests/test_object_double.c index c71145bd33..8abdcdae37 100644 --- a/lib/filterx/tests/test_object_double.c +++ b/lib/filterx/tests/test_object_double.c @@ -152,8 +152,11 @@ Test(filterx_double, test_filterx_double_repr) { FilterXObject *obj = filterx_double_new(123.456); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(obj, repr)); cr_assert_str_eq("123.456", repr->str); + cr_assert(filterx_object_repr_append(obj, repr)); + cr_assert_str_eq("123.456123.456", repr->str); filterx_object_unref(obj); } diff --git a/lib/filterx/tests/test_object_integer.c b/lib/filterx/tests/test_object_integer.c index e3d803d527..67798ef5b9 100644 --- a/lib/filterx/tests/test_object_integer.c +++ b/lib/filterx/tests/test_object_integer.c @@ -206,8 +206,11 @@ Test(filterx_integer, test_filterx_integer_repr) { FilterXObject *obj = filterx_integer_new(65566); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(obj, repr)); cr_assert_str_eq("65566", repr->str); + cr_assert(filterx_object_repr_append(obj, repr)); + cr_assert_str_eq("6556665566", repr->str); filterx_object_unref(obj); } diff --git a/lib/filterx/tests/test_object_json.c b/lib/filterx/tests/test_object_json.c index 55810b3623..cbe76d7520 100644 --- a/lib/filterx/tests/test_object_json.c +++ b/lib/filterx/tests/test_object_json.c @@ -28,6 +28,7 @@ #include "filterx/object-message-value.h" #include "filterx/expr-function.h" #include "apphook.h" +#include "scratch-buffers.h" Test(filterx_json, test_filterx_object_json_from_repr) { @@ -164,6 +165,30 @@ Test(filterx_json, test_json_array_function) filterx_object_unref(fobj); } +Test(filterx_json, filterx_json_object_repr) +{ + FilterXObject *obj = filterx_json_object_new_from_repr("{\"foo\": \"foovalue\"}", -1); + GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); + cr_assert(filterx_object_repr(obj, repr)); + cr_assert_str_eq("{\"foo\":\"foovalue\"}", repr->str); + cr_assert(filterx_object_repr_append(obj, repr)); + cr_assert_str_eq("{\"foo\":\"foovalue\"}{\"foo\":\"foovalue\"}", repr->str); + filterx_object_unref(obj); +} + +Test(filterx_json, filterx_json_array_repr) +{ + FilterXObject *obj = filterx_json_array_new_from_repr("[\"foo\", \"bar\"]", -1); + GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); + cr_assert(filterx_object_repr(obj, repr)); + cr_assert_str_eq("[\"foo\",\"bar\"]", repr->str); + cr_assert(filterx_object_repr_append(obj, repr)); + cr_assert_str_eq("[\"foo\",\"bar\"][\"foo\",\"bar\"]", repr->str); + filterx_object_unref(obj); +} + static void setup(void) { diff --git a/lib/filterx/tests/test_object_message.c b/lib/filterx/tests/test_object_message.c index 946d600c39..9ffb61c826 100644 --- a/lib/filterx/tests/test_object_message.c +++ b/lib/filterx/tests/test_object_message.c @@ -77,52 +77,59 @@ Test(filterx_message, test_filterx_message_type_null_repr) { FilterXObject *fobj = filterx_message_value_new(NULL, 0, LM_VT_NULL); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); cr_assert_str_eq("null", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("nullnull", repr->str); filterx_object_unref(fobj); } Test(filterx_message, test_filterx_message_type_string_repr) { - gchar *test_str = "any string"; - - FilterXObject *fobj = filterx_message_value_new(test_str, -1, LM_VT_STRING); + FilterXObject *fobj = filterx_message_value_new("any string", -1, LM_VT_STRING); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); - cr_assert_str_eq(test_str, repr->str); + cr_assert_str_eq("any string", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("any stringany string", repr->str); filterx_object_unref(fobj); } Test(filterx_message, test_filterx_message_type_bytes_repr) { - gchar *test_str = "any bytes"; - - FilterXObject *fobj = filterx_message_value_new(test_str, -1, LM_VT_BYTES); + FilterXObject *fobj = filterx_message_value_new("any bytes", -1, LM_VT_BYTES); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); - cr_assert_str_eq(test_str, repr->str); + cr_assert_str_eq("any bytes", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("any bytesany bytes", repr->str); filterx_object_unref(fobj); } Test(filterx_message, test_filterx_message_type_protobuf_repr) { - gchar *test_str = "not a valid protobuf!"; - - FilterXObject *fobj = filterx_message_value_new(test_str, -1, LM_VT_PROTOBUF); + FilterXObject *fobj = filterx_message_value_new("not a valid protobuf!", -1, LM_VT_PROTOBUF); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); - cr_assert_str_eq(test_str, repr->str); + cr_assert_str_eq("not a valid protobuf!", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("not a valid protobuf!not a valid protobuf!", repr->str); filterx_object_unref(fobj); } Test(filterx_message, test_filterx_message_type_json_repr) { - gchar *test_str = "{\"test\":\"json\"}"; - - FilterXObject *fobj = filterx_message_value_new(test_str, -1, LM_VT_JSON); + FilterXObject *fobj = filterx_message_value_new("{\"test\": \"json\"}", -1, LM_VT_JSON); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); - cr_assert_str_eq(test_str, repr->str); + cr_assert_str_eq("{\"test\": \"json\"}", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("{\"test\": \"json\"}{\"test\": \"json\"}", repr->str); filterx_object_unref(fobj); } @@ -131,8 +138,11 @@ Test(filterx_message, test_filterx_message_type_boolean_repr) gchar *val = "T"; FilterXObject *fobj = filterx_message_value_new(val, -1, LM_VT_BOOLEAN); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); cr_assert_str_eq("true", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("truetrue", repr->str); filterx_object_unref(fobj); } @@ -141,8 +151,11 @@ Test(filterx_message, test_filterx_message_type_int_repr) gchar *val = "443"; FilterXObject *fobj = filterx_message_value_new(val, -1, LM_VT_INTEGER); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); cr_assert_str_eq("443", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("443443", repr->str); filterx_object_unref(fobj); } @@ -151,8 +164,11 @@ Test(filterx_message, test_filterx_message_type_double_repr) gchar *val = "17.756"; FilterXObject *fobj = filterx_message_value_new(val, -1, LM_VT_DOUBLE); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); cr_assert_str_eq("17.756", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("17.75617.756", repr->str); filterx_object_unref(fobj); } @@ -161,8 +177,11 @@ Test(filterx_message, test_filterx_message_type_datetime_repr) gchar *val = "1713520972.000000+02:00"; FilterXObject *fobj = filterx_message_value_new(val, -1, LM_VT_DATETIME); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); cr_assert_str_eq("2024-04-19T12:02:52.000+02:00", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("2024-04-19T12:02:52.000+02:002024-04-19T12:02:52.000+02:00", repr->str); filterx_object_unref(fobj); } diff --git a/lib/filterx/tests/test_object_null.c b/lib/filterx/tests/test_object_null.c index 0f17d5b306..b17694d0c5 100644 --- a/lib/filterx/tests/test_object_null.c +++ b/lib/filterx/tests/test_object_null.c @@ -45,8 +45,12 @@ Test(filterx_null, test_filterx_object_null_repr) { FilterXObject *fobj = filterx_null_new(); GString *repr = scratch_buffers_alloc(); + g_string_assign(repr, "foo"); cr_assert(filterx_object_repr(fobj, repr)); cr_assert_str_eq("null", repr->str); + cr_assert(filterx_object_repr_append(fobj, repr)); + cr_assert_str_eq("nullnull", repr->str); + filterx_object_unref(fobj); } static void