summaryrefslogtreecommitdiff
path: root/src/tests/efreet/ef_ini.c
blob: 9fb95cade465d374edb8c22d3dacb302231246f0 (plain) (blame)
1
2
3
4
5
6
7
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "config.h" 

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "Efreet.h"
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ef_test.h"

int
ef_cb_ini_parse(void)
{
   int ret = 1;
   Efreet_Ini *ini;

   putenv("LC_ALL=en_US");

   ini = efreet_ini_new(ef_test_path_get("test.ini"));
   if (!ini)
     {
        printf("efreet_ini_parse() Failed to initialize Efreet_Ini\n");
        return 0;
     }

   if (efreet_ini_section_set(ini, "contact"))
     {
        const char *val;
        int ival;
        unsigned int bval;

        val = efreet_ini_string_get(ini, "Name");
        if (!val || strcmp(val, "Foo Bar"))
          {
             printf("efreet_ini_string_get() Name parsed incorrectly\n");
             ret = 0;
          }

        val = efreet_ini_localestring_get(ini, "Name");
        if (!val || strcmp(val, "English Foo Bar"))
          {
             printf("efreet_ini_localestring_get() Name parsed incorrectly\n");
             ret = 0;
          }

        val = efreet_ini_string_get(ini, "Email");
        if (!val || strcmp(val, "foo@bar.com"))
          {
             printf("efreet_ini_string_get() Email parsed incorrectly\n");
             ret = 0;
          }

        val = efreet_ini_localestring_get(ini, "Email");
        if (!val || strcmp(val, "foo@bar.com"))
          {
             printf("efreet_ini_localestring_get() Email parsed incorrectly\n");
             ret = 0;
          }

        ival = efreet_ini_int_get(ini, "Age");
        if (ival != 30)
          {
             printf("efreet_ini_int_get() Age parsed incorrectly\n");
             ret = 0;
          }

        bval = efreet_ini_boolean_get(ini, "TrueBoolean");
        if (!bval)
          {
             printf("efreet_ini_boolean_get() TrueBoolean parsed incorrectly\n");
             ret = 0;
          }

        bval = efreet_ini_boolean_get(ini, "FalseBoolean");
        if (bval)
          {
             printf("efreet_ini_boolean_get() FalseBoolean parsed incorrectly\n");
             ret = 0;
          }

        bval = efreet_ini_boolean_get(ini, "InvalidBoolean");
        if (bval)
          {
             printf("efreet_ini_boolean_get() InvalidBoolean parsed incorrectly\n");
             ret = 0;
          }

        val = efreet_ini_string_get(ini, "Escaped");
        if (!val || strcmp(val, "line1\nline2\r\nline3\ttabbed \\ with a backslash and spaces"))
          {
             printf("efreet_ini_unescape() improperly unescaped value\n");
             ret = 0;
          }
     }
   else
     {
        printf("efreet_ini_section_set() Failed to set 'contact' section\n");
        ret = 0;
     }

   efreet_ini_free(ini);

   return ret;
}

int
ef_cb_ini_long_line(void)
{
   Efreet_Ini *ini;
   int ret = 1;

   struct
     {
        char *key;
        int len;
     } tests[] = {
            {"key", 5099},
            {"key2", 5099},
            {NULL, 0}
     };

   ini = efreet_ini_new(ef_test_path_get("long.ini"));
   if (!ini)
     {
        printf("Ini failed to parse.\n");
        ret = 0;
     }

   if (ret) ret = efreet_ini_section_set(ini, "section");
   if (ret)
     {
        const char *val;
        int i, len;

        for (i = 0; tests[i].key; i++)
          {
             val = efreet_ini_string_get(ini, tests[i].key);
             if (val)
               {
                  len = strlen(val);
                  if (len != tests[i].len)
                    {
                       printf("Invalid long line parsing. Value length: %d (expected %d)\n", len, tests[i].len);
                       ret = 0;
                    }
               }
             else
               {
                  printf("Key missing: %s.", tests[i].key);
                  ret = 0;
               }
          }
     }
   else
     {
        printf("Section missing: 'section'.");
     }

   if (ini) efreet_ini_free(ini);
   return ret;
}

int
ef_cb_ini_garbage(void)
{
   Efreet_Ini *ini;
   int ret = 1;

   ini = efreet_ini_new(ef_test_path_get("test_garbage"));
   if (!ini)
     {
        printf("Ini failed to parse.\n");
        return 0;
     }
   if (ini->data) ret = 0;
   efreet_ini_free(ini);
   return ret;
}