test_config.c 2.46 KB
Newer Older
1 2 3 4 5
/* See LICENSE file for license and copyright information */

#include <check.h>
#include <glib.h>
#include <glib/gstdio.h>
Sebastian Ramacher's avatar
Sebastian Ramacher committed
6
#include <unistd.h>
7

8 9 10
#include "session.h"
#include "settings.h"
#include "config.h"
11
#include "tests.h"
12 13 14

START_TEST(test_config_parse) {
  girara_session_t* session = girara_session_create();
15
  fail_unless(session != NULL, "Failed to create girara session.", NULL);
16 17

  int default_val = 1;
18
  fail_unless(girara_setting_add(session, "test1", "default-string", STRING, false, NULL, NULL, NULL),
19
      "Failed to add setting 'test1'", NULL);
20
  fail_unless(girara_setting_add(session, "test2", &default_val, INT, false, NULL, NULL, NULL),
21
      "Failed to add setting 'test2'", NULL);
22

23 24
  char* filename = NULL;
  int fd = g_file_open_tmp(NULL, &filename, NULL);
25
  fail_unless(fd != -1 && filename != NULL, "Couldn't open temporary file.", NULL);
26 27
  GError* error = NULL;
  if (g_file_set_contents(filename,
28
        "set test1 config-string\n" \
29
        "set test2 2\n", -1, &error) == FALSE) {
30
    fail_unless(false, "Couldn't set content: %s", error->message, NULL);
31 32
    g_error_free(error);
  }
33 34 35
  girara_config_parse(session, filename);

  char* ptr = NULL;
36
  fail_unless(girara_setting_get(session, "test1", &ptr), "Failed to get setting 'test1'.", NULL);
37
  fail_unless(g_strcmp0(ptr, "config-string") == 0, "Value of 'test1' doesn't match (got: %s, expected: %s",
38
      ptr, "config-string", NULL);
39 40 41
  g_free(ptr);

  int real_val = 0;
42
  fail_unless(girara_setting_get(session, "test2", &real_val), "Failed to get setting 'test1'.", NULL);
43
  fail_unless(real_val == 2, "Value of 'test2' doesn't match (got: %d, expected: %d",
Sebastian Ramacher's avatar
Sebastian Ramacher committed
44
      real_val, 2, NULL);
45

46
  close(fd);
47
  fail_unless(g_remove(filename) == 0, "Failed to remove temporary file.", NULL);
48
  g_free(filename);
49 50 51
  girara_session_destroy(session);
} END_TEST

Sebastian Ramacher's avatar
Sebastian Ramacher committed
52
static Suite* suite_config(void)
53 54 55 56 57 58
{
  TCase* tcase = NULL;
  Suite* suite = suite_create("Config");

  /* parse */
  tcase = tcase_create("parse");
59
  tcase_add_checked_fixture(tcase, setup, NULL);
60 61 62 63 64
  tcase_add_test(tcase, test_config_parse);
  suite_add_tcase(suite, tcase);

  return suite;
}
Sebastian Ramacher's avatar
Sebastian Ramacher committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

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

  /* test config */
  suite        = suite_config();
  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;
}