Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

libtest: added assert_template_format_with_context() function

Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
  • Loading branch information...
commit 0f3c04fa3283753149779ad2f9589026f809f2da 1 parent cc866a8
Balazs Scheidler bazsi authored

Showing 2 changed files with 35 additions and 6 deletions. Show diff stats Hide diff stats

  1. +34 6 libtest/template_lib.c
  2. +1 0  libtest/template_lib.h
40 libtest/template_lib.c
@@ -45,27 +45,55 @@ create_sample_message(void)
45 45 return msg;
46 46 }
47 47
  48 +LogTemplate *
  49 +compile_template(const gchar *template)
  50 +{
  51 + LogTemplate *templ = log_template_new(configuration, NULL);
  52 + gboolean success;
  53 + GError *error = NULL;
  54 +
  55 + success = log_template_compile(templ, template, &error);
  56 + assert_true(success, "template expected to compile cleanly, but it didn't, template=%s, error=%s", template, error ? error->message : "(none)");
  57 + g_clear_error(&error);
  58 +
  59 + return templ;
  60 +}
  61 +
48 62 void
49 63 assert_template_format(const gchar *template, const gchar *expected)
50 64 {
51 65 LogMessage *msg;
52 66 LogTemplate *templ;
53 67 GString *res = g_string_sized_new(128);
54   - GError *error = NULL;
  68 + const gchar *context_id = "test-context-id";
  69 +
  70 + msg = create_sample_message();
  71 +
  72 + templ = compile_template(template);
  73 + log_template_format(templ, msg, NULL, LTZ_LOCAL, 999, context_id, res);
  74 + assert_string(res->str, expected, "template test failed, template=%s", template);
  75 + log_template_unref(templ);
  76 + g_string_free(res, TRUE);
  77 + log_msg_unref(msg);
  78 +}
  79 +
  80 +void
  81 +assert_template_format_with_context(const gchar *template, const gchar *expected)
  82 +{
  83 + LogMessage *msg;
  84 + LogTemplate *templ;
  85 + GString *res = g_string_sized_new(128);
55 86 const gchar *context_id = "test-context-id";
56 87 LogMessage *context[2];
57 88
58 89 msg = create_sample_message();
59 90 context[0] = context[1] = msg;
60 91
61   - templ = log_template_new(configuration, NULL);
62   -
63   - assert_true(log_template_compile(templ, template, &error), "template expected to compile cleanly, but it didn't, template=%s, error=%s", template, error ? error->message : "(none)");
  92 + templ = compile_template(template);
64 93
65 94 log_template_format_with_context(templ, context, 2, NULL, LTZ_LOCAL, 999, context_id, res);
66   - assert_string(res->str, expected, "template test failed, template=%s, [%s] <=> [%s]\n", template, res->str, expected);
  95 + assert_string(res->str, expected, "context template test failed, template=%s", template);
67 96 log_template_unref(templ);
68   - g_clear_error(&error);
69 97 g_string_free(res, TRUE);
70 98 log_msg_unref(msg);
71 99 }
1  libtest/template_lib.h
@@ -4,6 +4,7 @@
4 4 #include "testutils.h"
5 5
6 6 void assert_template_format(const gchar *template, const gchar *expected);
  7 +void assert_template_format_with_context(const gchar *template, const gchar *expected);
7 8 void assert_template_failure(const gchar *template, const gchar *expected_failure);
8 9
9 10 void init_template_tests(void);

0 comments on commit 0f3c04f

Please sign in to comment.
Something went wrong with that request. Please try again.