Skip to content

Commit

Permalink
filterx/tests: use helper functions to assert on repr() and str() output
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Feb 16, 2025
1 parent 6ed21a9 commit 3ead54f
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 156 deletions.
20 changes: 4 additions & 16 deletions lib/filterx/tests/test_expr_plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ Test(expr_plus, test_datetime_add_integer)
cr_assert_not_null(obj);
cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(datetime)));

GString *date_repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(obj, date_repr));

cr_assert_str_eq(date_repr->str, "2020-01-01T01:00:00.000+00:00");
assert_object_repr_equals(obj, "2020-01-01T01:00:00.000+00:00");

filterx_object_unref(obj);
filterx_expr_unref(expr);
Expand All @@ -142,10 +139,7 @@ Test(expr_plus, test_datetime_add_double)
cr_assert_not_null(obj);
cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(datetime)));

GString *date_repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(obj, date_repr));

cr_assert_str_eq(date_repr->str, "2020-01-01T01:00:00.000+00:00");
assert_object_repr_equals(obj, "2020-01-01T01:00:00.000+00:00");

filterx_object_unref(obj);
filterx_expr_unref(expr);
Expand Down Expand Up @@ -290,10 +284,7 @@ Test(expr_plus, test_list_add_list)
cr_assert_not_null(obj);
cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(list)));

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(obj, repr));

cr_assert_str_eq(repr->str, "[\"foo\",\"bar\",\"tik\",\"tak\"]");
assert_object_repr_equals(obj, "[\"foo\",\"bar\",\"tik\",\"tak\"]");

filterx_object_unref(obj);
filterx_expr_unref(expr);
Expand Down Expand Up @@ -325,10 +316,7 @@ Test(expr_plus, test_dict_add_dict)
cr_assert_not_null(obj);
cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(dict)));

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(obj, repr));

cr_assert_str_eq(repr->str, "{\"foo\":\"bar\",\"tik\":\"tak\"}");
assert_object_repr_equals(obj, "{\"foo\":\"bar\",\"tik\":\"tak\"}");

filterx_object_unref(obj);
filterx_expr_unref(expr);
Expand Down
6 changes: 1 addition & 5 deletions lib/filterx/tests/test_func_flatten.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ _assert_flatten(GList *args, const gchar *expected_repr)
FilterXObject *modifiable_object = filterx_expr_eval(modifiable_object_expr);
cr_assert(modifiable_object);

GString *repr = g_string_new(NULL);
cr_assert(filterx_object_repr(modifiable_object, repr));
cr_assert_str_eq(repr->str, expected_repr, "flatten() result is unexpected. Actual: %s Expected: %s", repr->str,
expected_repr);
assert_object_repr_equals(modifiable_object, expected_repr);

g_string_free(repr, TRUE);
filterx_object_unref(obj);
filterx_expr_unref(func);
filterx_object_unref(modifiable_object);
Expand Down
5 changes: 1 addition & 4 deletions lib/filterx/tests/test_func_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ Test(filterx_func_keys, valid_input)
cr_assert_not_null(res);
cr_assert(filterx_object_is_type(res, &FILTERX_TYPE_NAME(list)));

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(res, repr));

cr_assert_str_eq(repr->str, "[\"foo\",\"bar\",\"baz\"]");
assert_object_repr_equals(res, "[\"foo\",\"bar\",\"baz\"]");

filterx_object_unref(res);
filterx_expr_unref(fn);
Expand Down
6 changes: 1 addition & 5 deletions lib/filterx/tests/test_func_unset_empties.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,8 @@ _assert_unset_empties(GList *args, const gchar *expected_repr)
FilterXObject *modifiable_object = filterx_expr_eval(modifiable_object_expr);
cr_assert(modifiable_object);

GString *repr = g_string_new(NULL);
cr_assert(filterx_object_repr(modifiable_object, repr));
cr_assert_str_eq(repr->str, expected_repr, "unset_empties() result is unexpected. Actual: %s Expected: %s", repr->str,
expected_repr);
assert_object_repr_equals(modifiable_object, expected_repr);

g_string_free(repr, TRUE);
filterx_object_unref(obj);
filterx_expr_unref(func);
filterx_object_unref(modifiable_object);
Expand Down
7 changes: 1 addition & 6 deletions lib/filterx/tests/test_object_boolean.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,7 @@ Test(filterx_boolean, test_filterx_boolean_typecast_from_boolean)
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);
assert_object_repr_equals(obj, "true");
filterx_object_unref(obj);
}

Expand Down
9 changes: 3 additions & 6 deletions lib/filterx/tests/test_object_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "apphook.h"
#include "scratch-buffers.h"

#include <libtest/filterx-lib.h>

Test(filterx_bytes, test_filterx_bytes_typecast_null_args)
{
FilterXObject *obj = filterx_typecast_bytes(NULL, NULL, 0);
Expand Down Expand Up @@ -108,12 +110,7 @@ Test(filterx_bytes, test_filterx_bytes_typecast_from_protobuf)
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);
assert_object_repr_equals(obj, "00010203");
filterx_object_unref(obj);
}

Expand Down
20 changes: 4 additions & 16 deletions lib/filterx/tests/test_object_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,7 @@ Test(filterx_datetime, test_filterx_datetime_repr)
cr_assert_not_null(obj);
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);
assert_object_repr_equals(obj, "2024-03-18T12:34:13.000+09:00");

filterx_simple_function_free_args(args, G_N_ELEMENTS(args));
filterx_object_unref(obj);
Expand All @@ -229,10 +224,7 @@ Test(filterx_datetime, test_filterx_datetime_repr_isodate_Z)
cr_assert_not_null(obj);
cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(datetime)));

GString *repr = scratch_buffers_alloc();

cr_assert(filterx_object_repr(args[0], repr));
cr_assert_str_eq(test_time_str, repr->str);
assert_object_repr_equals(args[0], test_time_str);

filterx_simple_function_free_args(args, G_N_ELEMENTS(args));
filterx_object_unref(obj);
Expand Down Expand Up @@ -299,9 +291,7 @@ Test(filterx_datetime, test_filterx_datetime_strptime_matching_timefmt)
cr_assert(obj);
cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(datetime)));

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(obj, repr));
cr_assert_str_eq(repr->str, "2024-04-08T10:11:12.000+00:00");
assert_object_repr_equals(obj, "2024-04-08T10:11:12.000+00:00");

filterx_object_unref(obj);
filterx_expr_unref(func_expr);
Expand All @@ -324,9 +314,7 @@ Test(filterx_datetime, test_filterx_datetime_strptime_matching_nth_timefmt)
cr_assert(obj);
cr_assert(filterx_object_is_type(obj, &FILTERX_TYPE_NAME(datetime)));

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(obj, repr));
cr_assert_str_eq(repr->str, "2024-04-08T10:11:12.000+01:00");
assert_object_repr_equals(obj, "2024-04-08T10:11:12.000+01:00");

filterx_object_unref(obj);
filterx_expr_unref(func_expr);
Expand Down
16 changes: 4 additions & 12 deletions lib/filterx/tests/test_object_dict_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ Test(filterx_dict_interface, test_keys_empty)
cr_assert(ok);
cr_assert_not_null(keys);

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(keys, repr));
cr_assert_str_eq(repr->str, "[]");
assert_object_repr_equals(keys, "[]");

filterx_object_unref(keys);
}
Expand All @@ -60,9 +58,7 @@ Test(filterx_dict_interface, test_keys_single)
cr_assert(ok);
cr_assert_not_null(keys);

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(keys, repr));
cr_assert_str_eq("[\"foo\"]", repr->str);
assert_object_repr_equals(keys, "[\"foo\"]");

filterx_object_unref(keys);
}
Expand All @@ -79,9 +75,7 @@ Test(filterx_dict_interface, test_keys_multi)
cr_assert(ok);
cr_assert_not_null(keys);

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(keys, repr));
cr_assert_str_eq("[\"foo\",\"bar\"]", repr->str);
assert_object_repr_equals(keys, "[\"foo\",\"bar\"]");

filterx_object_unref(keys);
}
Expand All @@ -98,9 +92,7 @@ Test(filterx_dict_interface, test_keys_nested)
cr_assert(ok);
cr_assert_not_null(keys);

GString *repr = scratch_buffers_alloc();
cr_assert(filterx_object_repr(keys, repr));
cr_assert_str_eq("[\"foo\",\"bar\"]", repr->str);
assert_object_repr_equals(keys, "[\"foo\",\"bar\"]");

filterx_object_unref(keys);
}
Expand Down
7 changes: 1 addition & 6 deletions lib/filterx/tests/test_object_double.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,7 @@ Test(filterx_double, test_filterx_double_typecast_from_datetime)
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);
assert_object_repr_equals(obj, "123.456");
filterx_object_unref(obj);
}

Expand Down
7 changes: 1 addition & 6 deletions lib/filterx/tests/test_object_integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,7 @@ Test(filterx_integer, test_filterx_integer_typecast_from_datetime)
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);
assert_object_repr_equals(obj, "65566");
filterx_object_unref(obj);
}

Expand Down
14 changes: 2 additions & 12 deletions lib/filterx/tests/test_object_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,14 @@ Test(filterx_json, test_json_array_function)
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);
assert_object_repr_equals(obj, "{\"foo\":\"foovalue\"}");
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);
assert_object_repr_equals(obj, "[\"foo\",\"bar\"]");
filterx_object_unref(obj);
}

Expand Down
66 changes: 10 additions & 56 deletions lib/filterx/tests/test_object_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,114 +76,68 @@ Test(filterx_message, test_filterx_object_value_maps_to_the_right_json_value)
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);
assert_object_repr_equals(fobj, "null");
filterx_object_unref(fobj);
}

Test(filterx_message, test_filterx_message_type_string_repr)
{
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("any string", repr->str);
cr_assert(filterx_object_repr_append(fobj, repr));
cr_assert_str_eq("any stringany string", repr->str);
assert_object_repr_equals(fobj, "any string");
filterx_object_unref(fobj);
}

Test(filterx_message, test_filterx_message_type_bytes_repr)
{
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("any bytes", repr->str);
cr_assert(filterx_object_repr_append(fobj, repr));
cr_assert_str_eq("any bytesany bytes", repr->str);
assert_object_repr_equals(fobj, "any bytes");
filterx_object_unref(fobj);
}

Test(filterx_message, test_filterx_message_type_protobuf_repr)
{
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("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);
assert_object_repr_equals(fobj, "not a valid protobuf!");
filterx_object_unref(fobj);
}

Test(filterx_message, test_filterx_message_type_json_repr)
{
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\": \"json\"}", repr->str);
cr_assert(filterx_object_repr_append(fobj, repr));
cr_assert_str_eq("{\"test\": \"json\"}{\"test\": \"json\"}", repr->str);
assert_object_repr_equals(fobj, "{\"test\": \"json\"}");
filterx_object_unref(fobj);
}

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);
assert_object_repr_equals(fobj, "true");
filterx_object_unref(fobj);
}

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);
assert_object_repr_equals(fobj, "443");
filterx_object_unref(fobj);
}

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);
assert_object_repr_equals(fobj, "17.756");
filterx_object_unref(fobj);
}

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_str(fobj, repr));
cr_assert_str_eq(repr->str, "1713520972.000000");
cr_assert(filterx_object_repr(fobj, repr));
cr_assert_str_eq(repr->str, "2024-04-19T12:02:52.000+02:00");
cr_assert(filterx_object_repr_append(fobj, repr));
cr_assert_str_eq(repr->str, "2024-04-19T12:02:52.000+02:002024-04-19T12:02:52.000+02:00");
assert_object_str_equals(fobj, "1713520972.000000");
assert_object_repr_equals(fobj, "2024-04-19T12:02:52.000+02:00");
filterx_object_unref(fobj);
}

Expand Down
Loading

0 comments on commit 3ead54f

Please sign in to comment.