test_setting.c 2.87 KB
Newer Older
1
/* SPDX-License-Identifier: Zlib */
2

Moritz Lipp's avatar
Moritz Lipp committed
3
#include <check.h>
4

5 6
#include "session.h"
#include "settings.h"
7
#include "tests.h"
Moritz Lipp's avatar
Moritz Lipp committed
8 9

START_TEST(test_settings_basic) {
10
  girara_session_t* session = girara_session_create();
11
  fail_unless(session != NULL, "Couldn't create session.", NULL);
Moritz Lipp's avatar
Moritz Lipp committed
12

Sebastian Ramacher's avatar
Sebastian Ramacher committed
13
  fail_unless(girara_setting_add(session, "test", NULL, STRING, false, NULL, NULL, NULL), NULL);
14
  char* ptr = NULL;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
15 16
  fail_unless(girara_setting_get(session, "test", &ptr), NULL);
  fail_unless(ptr == NULL, NULL);
17

Sebastian Ramacher's avatar
Sebastian Ramacher committed
18 19 20
  fail_unless(girara_setting_set(session, "test", "value"), NULL);
  fail_unless(girara_setting_get(session, "test", &ptr), NULL);
  fail_unless(g_strcmp0(ptr, "value") == 0, NULL);
21 22
  g_free(ptr);

23
  ptr = NULL;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
24 25
  fail_unless(!girara_setting_get(session, "does-not-exist", &ptr), NULL);
  fail_unless(ptr == NULL, NULL);
26

Sebastian Ramacher's avatar
Sebastian Ramacher committed
27 28 29
  fail_unless(girara_setting_add(session, "test2", "value", STRING, false, NULL, NULL, NULL), NULL);
  fail_unless(girara_setting_get(session, "test2", &ptr), NULL);
  fail_unless(g_strcmp0(ptr, "value") == 0, NULL);
30
  g_free(ptr);
31

32
  ptr = NULL;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
33 34 35
  fail_unless(!girara_setting_add(session, "test3", NULL, INT, false, NULL, NULL, NULL), NULL);
  fail_unless(!girara_setting_get(session, "test3", &ptr), NULL);
  fail_unless(ptr == NULL, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
36

37
  float val = 0.0, rval = 0.0;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
38 39 40
  fail_unless(girara_setting_add(session, "test4", &val, FLOAT, false, NULL, NULL, NULL), NULL);
  fail_unless(girara_setting_get(session, "test4", &rval), NULL);
  fail_unless(val == rval, NULL);
41

42
  girara_session_destroy(session);
Moritz Lipp's avatar
Moritz Lipp committed
43
} END_TEST
Sebastian Ramacher's avatar
Sebastian Ramacher committed
44 45 46 47

static int callback_called = 0;

static void
Sebastian Ramacher's avatar
Sebastian Ramacher committed
48
setting_callback(girara_session_t* session, const char* name, girara_setting_type_t type, const void* value, void* data)
Sebastian Ramacher's avatar
Sebastian Ramacher committed
49
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
50 51 52 53 54 55
  fail_unless(callback_called == 0, NULL);
  fail_unless(session != NULL, NULL);
  fail_unless(g_strcmp0(name, "test") == 0, NULL);
  fail_unless(type == STRING, NULL);
  fail_unless(g_strcmp0(value, "value") == 0, NULL);
  fail_unless(g_strcmp0(data, "data") == 0, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
56 57 58
  callback_called++;
}

Moritz Lipp's avatar
Moritz Lipp committed
59
START_TEST(test_settings_callback) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
60
  girara_session_t* session = girara_session_create();
Sebastian Ramacher's avatar
Sebastian Ramacher committed
61
  fail_unless(session != NULL, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
62

Sebastian Ramacher's avatar
Sebastian Ramacher committed
63 64 65
  fail_unless(girara_setting_add(session, "test", "oldvalue", STRING, false, NULL, setting_callback, "data"), NULL);
  fail_unless(girara_setting_set(session, "test", "value"), NULL);
  fail_unless(callback_called == 1, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
66 67

  girara_session_destroy(session);
Moritz Lipp's avatar
Moritz Lipp committed
68 69
} END_TEST

Sebastian Ramacher's avatar
Sebastian Ramacher committed
70
static Suite* suite_settings(void)
Moritz Lipp's avatar
Moritz Lipp committed
71 72 73 74 75 76
{
  TCase* tcase = NULL;
  Suite* suite = suite_create("Settings");

  /* basic */
  tcase = tcase_create("basic");
77
  tcase_add_checked_fixture(tcase, setup, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
78 79 80 81 82
  tcase_add_test(tcase, test_settings_basic);
  suite_add_tcase(suite, tcase);

  /* callback */
  tcase = tcase_create("callback");
83
  tcase_add_checked_fixture(tcase, setup, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
84 85 86 87
  suite_add_tcase(suite, tcase);
  tcase_add_test(tcase, test_settings_callback);

  return suite;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
88
}
Sebastian Ramacher's avatar
Sebastian Ramacher committed
89 90 91

int main()
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
92
  return run_suite(suite_settings());
Sebastian Ramacher's avatar
Sebastian Ramacher committed
93
}