summaryrefslogtreecommitdiffstats
path: root/guththila/tests/test.c
blob: 4442c503683b98b62f3fb04c23f5fa37187a2975 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <check.h>
#include <guththila.h>
#include "guththila_defines.h"
#include "test.h"

void
setup(
    void)
{
    allocator = axutil_allocator_init(NULL);
    env = axutil_env_create(allocator);
}

void
teardown(
    void)
{
    guththila_reader_free(env, red);
    guththila_free(env, parser);
    axutil_env_free(env);
}

START_TEST(test_guththila)
{
    red = guththila_reader_create_for_file(env, "resources/om/axis.xml");
    parser = guththila_create(env, red);
    fail_if(red == NULL, "guththila reader failed");
    fail_if(parser == NULL, "guththila parser failed");
}

END_TEST
START_TEST(
    test_guththila_start_element)
{
    int c = 0;
    char *p;
    red = guththila_reader_create_for_file(env, "resources/om/axis.xml");
    parser = guththila_create(env, red);
    guththila_read(env, parser);
    c = guththila_next(env, parser);

    while((c != GUTHTHILA_START_ELEMENT))
        c = guththila_next(env, parser);
    p = guththila_get_name(env, parser);
    fail_unless((c == GUTHTHILA_START_ELEMENT), "no start element found");
    fail_if((p == NULL), "no name found");
    fail_unless(!strcmp(p, "root"), "root element differed");
    c = 0;

    while((c != GUTHTHILA_START_ELEMENT))
        c = guththila_next(env, parser);
    p = guththila_get_name(env, parser);
    fail_unless((c == GUTHTHILA_START_ELEMENT), "no start element found");
    fail_if((p == NULL), "no name found");
    fail_unless(!strcmp(p, "a"), "a element differed");

    c = 0;
    while((c != GUTHTHILA_START_ELEMENT))
        c = guththila_next(env, parser);
    p = guththila_get_name(env, parser);
    fail_unless(!strcmp(p, "b"), "b element differed");
}

END_TEST
START_TEST(
    test_guththila_empty_element)
{
    int c = 0;
    char *p;
    red = guththila_reader_create_for_file(env, "resources/om/axis.xml");
    parser = guththila_create(env, red);
    guththila_read(env, parser);
    c = guththila_next(env, parser);

    while((c != GUTHTHILA_EMPTY_ELEMENT))
        c = guththila_next(env, parser);
    p = guththila_get_name(env, parser);
    fail_unless((c == GUTHTHILA_EMPTY_ELEMENT), "no empty element found");
    fail_if((p == NULL), "no name found");
    fail_unless(!strcmp(p, "a.1"), "a.1 element differed");

    c = 0;

    while((c != GUTHTHILA_EMPTY_ELEMENT))
        c = guththila_next(env, parser);
    p = guththila_get_name(env, parser);
    fail_unless((c == GUTHTHILA_EMPTY_ELEMENT), "no empty element found");
    fail_if((p == NULL), "no name found");
    fail_unless(!strcmp(p, "a.2"), "a.2 element differed");

    c = 0;
    while((c != GUTHTHILA_START_ELEMENT))
        c = guththila_next(env, parser);

    c = 0;
    while((c != GUTHTHILA_EMPTY_ELEMENT))
        c = guththila_next(env, parser);
    p = guththila_get_name(env, parser);
    fail_unless((c == GUTHTHILA_EMPTY_ELEMENT), "no empty element found");
    fail_if((p == NULL), "no name found");
    fail_unless(!strcmp(p, "b.1"), "b.1 element differed");
}

END_TEST
START_TEST(
    test_guththila_end_element)
{
    int c = 0;
    char *p;
    red = guththila_reader_create_for_file(env, "resources/om/axis.xml");
    parser = guththila_create(env, red);
    guththila_read(env, parser);
    c = guththila_next(env, parser);

    while((c != GUTHTHILA_END_ELEMENT))
        c = guththila_next(env, parser);
    p = guththila_get_name(env, parser);
    fail_unless((c == GUTHTHILA_END_ELEMENT), "no end element found");
    fail_if((p == NULL), "no name found");
    fail_unless(!strcmp(p, "a"), "a element differed");

    c = 0;
    while((c != GUTHTHILA_END_ELEMENT))
        c = guththila_next(env, parser);
    p = guththila_get_name(env, parser);
    fail_unless((c == GUTHTHILA_END_ELEMENT), "no endelement found");
    fail_if((p == NULL), "no name found");
    fail_unless(!strcmp(p, "b"), "b element differed");

    c = 0;
    while((c != GUTHTHILA_END_ELEMENT))
        c = guththila_next(env, parser);
    p = guththila_get_name(env, parser);
    fail_unless((c == GUTHTHILA_END_ELEMENT), "no empty element found");
    fail_if((p == NULL), "no name found");
    fail_unless(!strcmp(p, "root"), "root element differed");
}

END_TEST
START_TEST(
    test_guththila_character)
{
    int c = 0;
    int i = 0;
    char *p;
    red = guththila_reader_create_for_file(env, "resources/om/numbers.xml");
    parser = guththila_create(env, red);
    guththila_read(env, parser);
    c = guththila_next(env, parser);
    while(i < 3)
    {
        if(c == GUTHTHILA_START_ELEMENT)
            i++;
        c = guththila_next(env, parser);
    }

    if(c == GUTHTHILA_CHARACTER)
        p = guththila_get_value(env, parser);
    fail_unless(!strcmp(p, "3"), "3 not found");

    c = 0;
    while((c != GUTHTHILA_CHARACTER) || (parser->is_whitespace))
        c = guththila_next(env, parser);
    p = guththila_get_value(env, parser);
    fail_unless(!strcmp(p, "24"), "24 not found");

}
END_TEST Suite *
guththila_suite(
    void)
{
    Suite *s = suite_create("Guththila");

    /* Core test case */
    TCase *tc_core = tcase_create("Core");
    tcase_add_checked_fixture(tc_core, setup, teardown);
    tcase_add_test(tc_core, test_guththila);
    tcase_add_test(tc_core, test_guththila_start_element);
    tcase_add_test(tc_core, test_guththila_empty_element);
    tcase_add_test(tc_core, test_guththila_end_element);
    tcase_add_test(tc_core, test_guththila_character);
    suite_add_tcase(s, tc_core);
    return s;
}

int
main(
    void)
{
    int number_failed;
    Suite *s = guththila_suite();
    Suite *att = guththila_attribute_suite();
    SRunner *sr = srunner_create(s);
    srunner_add_suite(sr, att);
    srunner_set_log(sr, "guththila.log");
    srunner_run_all(sr, CK_NORMAL);
    number_failed = srunner_ntests_failed(sr);
    srunner_free(sr);
    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}