Welcome to mirror list, hosted at ThFree Co, Russian Federation.

declaration.c « mycss « test - github.com/lexborisov/Modest.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 13f74b10d4ac63933a46ae6aa9cc0ea87089ed6c (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 Copyright (C) 2015-2016 Alexander Borisov

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

 Author: lex.borisov@gmail.com (Alexander Borisov)
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdbool.h>

#include <myhtml/myhtml.h>
#include <myhtml/mystring.h>
#include <myhtml/serialization.h>

#include <mycss/mycss.h>
#include <mycss/declaration/init.h>

#define TEST_FAILED(status) ((status) != 0)

typedef bool (*test_read_dir_callback_f)(const char* filename, size_t filename_length, void* ctx);

struct test_stat {
    size_t good;
    size_t total;
}
typedef test_stat_t;

struct test_data {
    myhtml_tree_t* tree;
    mycss_entry_t* entry;
    
    test_stat_t stat;
}
typedef test_data_t;

struct test_res {
    char*  data;
    size_t size;
}
typedef test_res_t;

test_res_t test_load_file(const char* filename)
{
    FILE *fh = fopen(filename, "rb");
    if(fh == NULL) {
        fprintf(stderr, "Can't open file: %s\n", filename);
        exit(EXIT_FAILURE);
    }

    if(fseek(fh, 0L, SEEK_END) != 0) {
        fprintf(stderr, "Can't set position (fseek) in file: %s\n", filename);
        exit(EXIT_FAILURE);
    }
    
    long size = ftell(fh);
    
    if(fseek(fh, 0L, SEEK_SET) != 0) {
        fprintf(stderr, "Can't set position (fseek) in file: %s\n", filename);
        exit(EXIT_FAILURE);
    }

    if(size <= 0) {
        fprintf(stderr, "Can't get file size or file is empty: %s\n", filename);
        exit(EXIT_FAILURE);
    }
    
    char *file_data = (char*)malloc(size + 1);
    if(file_data == NULL) {
        fprintf(stderr, "Can't allocate mem for file: %s\n", filename);
        exit(EXIT_FAILURE);
    }

    size_t nread = fread(file_data, 1, size, fh);
    if (nread != size) {
        fprintf(stderr, "Could not read %ld bytes (" MyCORE_FORMAT_Z " bytes done)\n", size, nread);
        exit(EXIT_FAILURE);
    }

    fclose(fh);
    
    return (test_res_t){file_data, (size_t)size};
}

void test_init_str_raw(mycore_string_raw_t* str, size_t size)
{
    str->size   = size;
    str->length = 0;
    str->data   = (char*)mycore_malloc(str->size * sizeof(char));
    
    if(str->data == NULL) {
        fprintf(stderr, "Can't allocation resources for mycore_string_raw_t object\n");
        exit(EXIT_FAILURE);
    }
}

void test_reallocate_str(mycore_string_raw_t *str, size_t size)
{
    str->data = (char*)mycore_realloc(str->data, size * sizeof(char));
    
    if(str->data == NULL) {
        fprintf(stderr, "Can't reallocation resources for mycore_string_raw_t object\n");
        exit(EXIT_FAILURE);
    }
}

void test_str_set_term(mycore_string_raw_t *str)
{
    if((str->length + 1) >= str->size)
        test_reallocate_str(str, 64);
    
    str->data[ str->length ] = '\0';
}

mycore_string_raw_t test_combine_to_style(const char* one, size_t one_len, const char* two, size_t two_len)
{
    size_t new_size = one_len + two_len;
    
    mycore_string_raw_t str = {0};
    test_init_str_raw(&str, (new_size + 2));
    
    memcpy(str.data, one, sizeof(char) * one_len);
    str.length = one_len;
    
    str.data[str.length] = ':';
    str.length++;
    
    memcpy(&str.data[str.length], two, sizeof(char) * two_len);
    str.length += two_len;
    
    str.data[ str.length ] = '\0';
    
    return str;
}

/* basic init */
myhtml_tree_t * test_declaration_create_myhtml(void)
{
    myhtml_t* myhtml = myhtml_create();
    if(myhtml == NULL)
        exit(EXIT_FAILURE);
    
    myhtml_status_t status = myhtml_init(myhtml, MyHTML_OPTIONS_PARSE_MODE_SINGLE, 1, 0);
    if(TEST_FAILED(status)) exit(EXIT_FAILURE);
    
    myhtml_tree_t* tree = myhtml_tree_create();
    if(tree == NULL)
        exit(EXIT_FAILURE);
    
    status = myhtml_tree_init(tree, myhtml);
    if(TEST_FAILED(status)) exit(EXIT_FAILURE);
    
    myhtml_tree_parse_flags_set(tree, MyHTML_TREE_PARSE_FLAGS_SKIP_WHITESPACE_TOKEN|MyHTML_TREE_PARSE_FLAGS_WITHOUT_DOCTYPE_IN_TREE);
    
    return tree;
}

void test_declaration_destroy_myhtml(myhtml_tree_t *tree)
{
    myhtml_t* myhtml = tree->myhtml;
    
    myhtml_tree_destroy(tree);
    myhtml_destroy(myhtml);
}

mycss_entry_t * test_declaration_create_mycss(void)
{
    mycss_t *mycss = mycss_create();
    if(mycss == NULL)
        exit(EXIT_FAILURE);
        
    mycss_status_t status = mycss_init(mycss);
    if(TEST_FAILED(status)) exit(EXIT_FAILURE);
    
    // currenr entry work init
    mycss_entry_t *entry = mycss_entry_create();
    if(entry == NULL)
        exit(EXIT_FAILURE);
    
    status = mycss_entry_init(mycss, entry);
    if(TEST_FAILED(status)) exit(EXIT_FAILURE);
    
    return entry;
}

void test_declaration_destroy_mycss(mycss_entry_t *entry)
{
    mycss_t *mycss = entry->mycss;
    
    mycss_entry_destroy(entry, true);
    mycss_destroy(mycss, true);
}

test_stat_t test_read_dir(const char* dir_path, test_read_dir_callback_f callback, void* context)
{
    /* ho-ho-ho */
    char path[(4096 * 4)];
    sprintf(path, "%s", dir_path);
    
    printf("Tests in directory: %s\n", path);
    
    size_t path_len = strlen(dir_path);

    DIR *dir;
    struct dirent *ent;
    struct stat path_stat;

    test_stat_t result_stat = {0};

    if((dir = opendir(dir_path)) != NULL)
    {
        while((ent = readdir(dir)) != NULL)
        {
            sprintf(&path[path_len], "/%s", ent->d_name);

            if(stat(path, &path_stat) != 0) {
                fprintf(stderr, "Can't get status for file: %s\n", path);
                exit(EXIT_FAILURE);
            }

            if(ent->d_name[0] == '.' || S_ISDIR(path_stat.st_mode))
                continue;
            
            result_stat.total++;
            printf(MyCORE_FORMAT_Z ") %s: ", result_stat.total, ent->d_name);
            
            if(callback(path, (strlen(ent->d_name) + path_len + 1), context)) {
                result_stat.good++;
            }
        }

        closedir (dir);
    }
    
    return result_stat;
}

/* test */
bool test_process_elements(test_data_t *test_data, myhtml_collection_t *collection, test_stat_t* result_stat);
mycore_string_raw_t test_process_result_from_node(test_data_t *test_data, myhtml_tree_node_t *node);
mycore_string_raw_t test_process_serialize_stype(test_data_t *test_data, const char* style, size_t style_size);
mystatus_t test_process_serialize_callback(const char* buffer, size_t size, void* ctx);

bool test_process_callback(const char* filename, size_t filename_len, void* context)
{
    test_data_t *test_data = (test_data_t*)context;
    test_res_t html_data = test_load_file(filename);
    test_stat_t result_stat = {0};
    
    /* parse html */
    myhtml_status_t status = myhtml_parse(test_data->tree, MyENCODING_UTF_8, html_data.data, html_data.size);
    if(status) {
        fprintf(stderr, "Could parse HTML from file %s error code %d\n", filename, status);
        exit(EXIT_FAILURE);
    }
    
    /* find test elements */
    myhtml_collection_t *collection = myhtml_get_nodes_by_name_in_scope(test_data->tree, NULL, test_data->tree->node_body, "test", 4, NULL);
    
    if(collection->length)
        test_process_elements(test_data, collection, &result_stat);
    
    myhtml_collection_destroy(collection);
    
    if((result_stat.total - result_stat.good)) {
        printf("\tResult: ");
    }
    
    printf("count(" MyCORE_FORMAT_Z ") good(" MyCORE_FORMAT_Z ") bad(" MyCORE_FORMAT_Z ")\n", result_stat.total, result_stat.good, (result_stat.total - result_stat.good));
    
    test_data->stat.good += result_stat.good;
    test_data->stat.total += result_stat.total;
    
    mycore_free(html_data.data);
    return true;
}

bool test_process_elements(test_data_t *test_data, myhtml_collection_t *collection, test_stat_t* result_stat)
{
    for(size_t i = 0; i < collection->length; i++)
    {
        myhtml_tree_node_t *node = collection->list[i];
        myhtml_tree_attr_t* attr_name = myhtml_attribute_by_key(node, "name", 4);
        myhtml_tree_attr_t* attr_value = myhtml_attribute_by_key(node, "value", 5);
        
        size_t name_length;
        size_t value_length;
        
        const char* name = myhtml_attribute_value(attr_name, &name_length);
        const char* value = myhtml_attribute_value(attr_value, &value_length);
        
        mycore_string_raw_t style = test_combine_to_style(name, name_length, value, value_length);
        
        mycore_string_raw_t correct_result = test_process_result_from_node(test_data, node);
        mycore_string_raw_t parse_result = test_process_serialize_stype(test_data, style.data, style.length);
        
        result_stat->total++;
        
        if(correct_result.length == parse_result.length &&
           strcmp(correct_result.data, parse_result.data) == 0)
        {
            result_stat->good++;
        }
        else {
            if((result_stat->total - result_stat->good) == 1) {
                printf("\n");
            }
            
            printf("\tBad: name=\"%s\" value=\"%s\" need=\"%s\" result=\"%s\"\n", name, value, correct_result.data, parse_result.data);
        }

        mycore_string_raw_destroy(&style, false);
        mycore_string_raw_destroy(&correct_result, false);
        mycore_string_raw_destroy(&parse_result, false);
        
        mycss_entry_clean_all(test_data->entry);
    }
    
    return true;
}

mycore_string_raw_t test_process_result_from_node(test_data_t *test_data, myhtml_tree_node_t *node)
{
    mycore_string_raw_t str = {0};
    
    if(myhtml_serialization_node_buffer(node->child, &str)) {
        fprintf(stderr, "Could serialization HTML node\n");
        exit(EXIT_FAILURE);
    }
    
    return str;
}

mycore_string_raw_t test_process_serialize_stype(test_data_t *test_data, const char* style, size_t style_size)
{
    mycore_string_raw_t str;
    test_init_str_raw(&str, 4096);
    
    mycss_status_t status;
    mycss_declaration_entry_t *declaration = mycss_declaration_parse(test_data->entry->declaration, MyENCODING_UTF_8, style, style_size, &status);
    
    if(status) {
        fprintf(stderr, "Could parse CSS Style (%d): %s\n", status, style);
        exit(EXIT_FAILURE);
    }
    
    mycss_declaration_serialization_entry_only_value(test_data->entry, declaration, test_process_serialize_callback, &str);
    test_str_set_term(&str);
    
    return str;
}

mystatus_t test_process_serialize_callback(const char* buffer, size_t size, void* ctx)
{
    mycore_string_raw_t *str = (mycore_string_raw_t*)ctx;
    
    if((str->length + size) >= str->size)
        test_reallocate_str(str, 4096);
    
    memcpy(&str->data[ str->length ], buffer, sizeof(char) * size);
    str->length += size;
    
    return MyCORE_STATUS_OK;
}

int main(int argc, const char * argv[])
{
    setbuf(stdout, NULL);
    
    if (argc < 2) {
        printf("Bad ARGV!\nUse: declaration <path_to_dir_test>\n");
        exit(EXIT_FAILURE);
    }
    
    test_data_t test_data = {
        test_declaration_create_myhtml(),
        test_declaration_create_mycss(),
        {0}
    };
    
    //test_read_dir("/new/C-git/Modest/test/mycss/data/declaration", test_process_callback, &test_data);
    
    test_read_dir(argv[1], test_process_callback, &test_data);
    
    size_t bad_count = (test_data.stat.total - test_data.stat.good);
    printf("\nTotal: " MyCORE_FORMAT_Z "; Good: " MyCORE_FORMAT_Z "; Bad: " MyCORE_FORMAT_Z "\n", test_data.stat.total, test_data.stat.good, bad_count);
    
    test_declaration_destroy_myhtml(test_data.tree);
    test_declaration_destroy_mycss(test_data.entry);
    
    return (bad_count ? EXIT_FAILURE : EXIT_SUCCESS);
}