test_template.c 4.61 KB
Newer Older
1 2 3 4
/* See LICENSE file for license and copyright information */

#include <check.h>

5 6
#include "template.h"
#include "datastructures.h"
7
#include "tests.h"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

START_TEST(test_new) {
  GiraraTemplate* obj = girara_template_new(NULL);
  ck_assert_ptr_ne(obj, NULL);
  g_object_unref(obj);

  obj = girara_template_new("base");
  ck_assert_ptr_ne(obj, NULL);
  g_object_unref(obj);
} END_TEST

START_TEST(test_new_with_null) {
  GiraraTemplate* obj = girara_template_new(NULL);
  ck_assert_ptr_ne(obj, NULL);

  const char* base = girara_template_get_base(obj);
  ck_assert_str_eq((char*) base, "");

  g_object_unref(obj);
} END_TEST

START_TEST(test_new_with_base) {
  GiraraTemplate* obj = girara_template_new("base");
  ck_assert_ptr_ne(obj, NULL);

  const char* base = girara_template_get_base(obj);
  ck_assert_str_eq((char*) base, "base");

  g_object_unref(obj);
} END_TEST

START_TEST(test_base_variables_none) {
  GiraraTemplate* obj = girara_template_new("base");
  ck_assert_ptr_ne(obj, NULL);

  girara_list_t* variables = girara_template_referenced_variables(obj);
  ck_assert_uint_eq(girara_list_size(variables), 0);

  g_object_unref(obj);
} END_TEST

START_TEST(test_base_variables_one) {
  GiraraTemplate* obj = girara_template_new("@test@");
  ck_assert_ptr_ne(obj, NULL);

  girara_list_t* variables = girara_template_referenced_variables(obj);
  ck_assert_uint_eq(girara_list_size(variables), 1);

  char* variable = girara_list_nth(variables, 0);
  ck_assert_str_eq(variable, "test");

  g_object_unref(obj);
} END_TEST

START_TEST(test_base_variables_one_twice) {
  GiraraTemplate* obj = girara_template_new("@test@ @test@");
  ck_assert_ptr_ne(obj, NULL);

  girara_list_t* variables = girara_template_referenced_variables(obj);
  ck_assert_uint_eq(girara_list_size(variables), 1);

  g_object_unref(obj);
} END_TEST

START_TEST(test_variable_add) {
  GiraraTemplate* obj = girara_template_new(NULL);
  ck_assert_ptr_ne(obj, NULL);

76 77 78 79 80 81 82 83 84
  ck_assert(girara_template_add_variable(obj, "name"));
  g_object_unref(obj);
} END_TEST

START_TEST(test_variable_add_invalid) {
  GiraraTemplate* obj = girara_template_new(NULL);
  ck_assert_ptr_ne(obj, NULL);

  ck_assert(!girara_template_add_variable(obj, "na|me"));
85 86 87 88 89 90 91
  g_object_unref(obj);
} END_TEST

START_TEST(test_variable_set) {
  GiraraTemplate* obj = girara_template_new(NULL);
  ck_assert_ptr_ne(obj, NULL);

92
  ck_assert(girara_template_add_variable(obj, "name"));
93 94 95 96 97 98 99 100
  girara_template_set_variable_value(obj, "name", "value");
  g_object_unref(obj);
} END_TEST

START_TEST(test_full_1) {
  GiraraTemplate* obj = girara_template_new("name = @name@");
  ck_assert_ptr_ne(obj, NULL);

101
  ck_assert(girara_template_add_variable(obj, "name"));
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  girara_template_set_variable_value(obj, "name", "value");

  char* result = girara_template_evaluate(obj);
  ck_assert_ptr_ne(result, NULL);
  ck_assert_str_eq(result, "name = value");

  g_free(result);
  g_object_unref(obj);
} END_TEST

START_TEST(test_full_2) {
  GiraraTemplate* obj = girara_template_new("name = @name@; test = @test@");
  ck_assert_ptr_ne(obj, NULL);

  girara_template_add_variable(obj, "name");
  girara_template_set_variable_value(obj, "name", "value");

  char* result = girara_template_evaluate(obj);
  ck_assert_ptr_eq(result, NULL);

  g_object_unref(obj);
} END_TEST

Sebastian Ramacher's avatar
Sebastian Ramacher committed
125
static Suite* suite_template(void)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
{
  TCase* tcase = NULL;
  Suite* suite = suite_create("Template");

  /* girara_template_new */
  tcase = tcase_create("object creation");
  tcase_add_test(tcase, test_new);
  tcase_add_test(tcase, test_new_with_null);
  tcase_add_test(tcase, test_new_with_base);
  suite_add_tcase(suite, tcase);

  /* base processing */
  tcase = tcase_create("base processing");
  tcase_add_test(tcase, test_base_variables_none);
  tcase_add_test(tcase, test_base_variables_one);
  tcase_add_test(tcase, test_base_variables_one_twice);
  suite_add_tcase(suite, tcase);

  /* basic variable operations */
  tcase = tcase_create("variables");
  tcase_add_test(tcase, test_variable_add);
147
  tcase_add_test(tcase, test_variable_add_invalid);
148 149 150 151 152 153 154 155 156 157 158
  tcase_add_test(tcase, test_variable_set);
  suite_add_tcase(suite, tcase);

  /* full processing */
  tcase = tcase_create("full");
  tcase_add_test(tcase, test_full_1);
  tcase_add_test(tcase, test_full_2);
  suite_add_tcase(suite, tcase);

  return suite;
}
Sebastian Ramacher's avatar
Sebastian Ramacher committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

int main()
{
  Suite* suite          = NULL;
  SRunner* suite_runner = NULL;
  int number_failed     = 0;

  /* test template */
  suite        = suite_template();
  suite_runner = srunner_create(suite);
  srunner_run_all(suite_runner, CK_NORMAL);
  number_failed += srunner_ntests_failed(suite_runner);
  srunner_free(suite_runner);

  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}