summaryrefslogtreecommitdiff
path: root/src/tests/eolian/eolian_generation.c
blob: 9512e338c33c4693805b86975b01fb6ecf22324c (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#ifdef HAVE_EVIL
#include "Evil.h"
#endif

#include <Eina.h>
#include <Eolian.h>

#include "eolian_suite.h"

static Eina_Bool
_files_compare (const char *ref_filename, const char *tmp_filename)
{
   Eina_Bool result = EINA_FALSE;

   FILE *tmp_file = NULL, *ref_file = NULL;
   char *tmp_content = NULL, *ref_content = NULL;

   tmp_file = fopen(tmp_filename, "rb");
   if (!tmp_file)
     {
        printf("Unable to open %s\n", tmp_filename);
        goto end;
     }
   ref_file = fopen(ref_filename, "rb");
   if (!ref_file)
     {
        printf("Unable to open %s\n", ref_filename);
        goto end;
     }

   fseek(tmp_file, 0, SEEK_END);
   long tmp_filesize = ftell(tmp_file);
   if (tmp_filesize < 0) goto end;
   fseek(tmp_file, 0, SEEK_SET);
   tmp_content = malloc(tmp_filesize + 1);
   if (fread(tmp_content, tmp_filesize, 1, tmp_file) != 1)
     goto end;
   tmp_content[tmp_filesize] = '\0';
   while (tmp_content[tmp_filesize - 1] == '\n')
     tmp_content[--tmp_filesize] = '\0';

   fseek(ref_file, 0, SEEK_END);
   long ref_filesize = ftell(ref_file);
   if (ref_filesize < 0) goto end;
   fseek(ref_file, 0, SEEK_SET);
   ref_content = malloc(ref_filesize + 1);
   if (fread(ref_content, ref_filesize, 1, ref_file) != 1)
     goto end;
   ref_content[ref_filesize] = '\0';
   while (ref_content[ref_filesize - 1] == '\n')
     ref_content[--ref_filesize] = '\0';

   if (tmp_filesize != ref_filesize) goto end;

   result = !memcmp(ref_content, tmp_content, ref_filesize);
end:
   if (tmp_file) fclose(tmp_file);
   if (ref_file) fclose(ref_file);
   if (tmp_content) free(tmp_content);
   if (ref_content) free(ref_content);

   return result;
}

static int
_eolian_gen_execute(const char *eo_filename, const char *options, const char *output_filename)
{
   char eolian_gen_path[PATH_MAX] = "";
   char command[PATH_MAX];

   if (getenv("EFL_RUN_IN_TREE"))
     {
        snprintf(eolian_gen_path, sizeof(eolian_gen_path),
              "%s/src/bin/eolian/eolian_gen%s",
              PACKAGE_BUILD_DIR, EXEEXT);
     }
   if (eolian_gen_path[0] == '\0')
      return -1;

   snprintf(command, PATH_MAX,
         "%s %s -I "PACKAGE_DATA_DIR"/data -o %s %s",
         eolian_gen_path, options, output_filename, eo_filename);
   return system(command);
}

START_TEST(eolian_dev_impl_code)
{
   char output_filepath[PATH_MAX] = "";
   snprintf(output_filepath, PATH_MAX, "%s/object_impl.c",
            eina_environment_tmp_get());
   remove(output_filepath);
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/object_impl.eo", "--gi", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/object_impl_ref.c", output_filepath));
   /* Check that nothing is added */
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/object_impl.eo", "--gi", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/object_impl_ref.c", output_filepath));
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/object_impl_add.eo", "--gi", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/object_impl_add_ref.c", output_filepath));
}
END_TEST

START_TEST(eolian_types_generation)
{
   char output_filepath[PATH_MAX] = "";
   snprintf(output_filepath, PATH_MAX, "%s/types_output.c",
            eina_environment_tmp_get());
   remove(output_filepath);
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/typedef.eo", "--gh", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/typedef_ref.c", output_filepath));
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/struct.eo", "--gh", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/struct_ref.c", output_filepath));
}
END_TEST

START_TEST(eolian_default_values_generation)
{
   char output_filepath[PATH_MAX] = "";
   snprintf(output_filepath, PATH_MAX, "%s/eolian_output.c",
            eina_environment_tmp_get());
   remove(output_filepath);
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/class_simple.eo", "--gc", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/class_simple_ref.c", output_filepath));
}
END_TEST

START_TEST(eolian_override_generation)
{
   char output_filepath[PATH_MAX] = "";
   snprintf(output_filepath, PATH_MAX, "%s/eolian_output.c",
            eina_environment_tmp_get());
   remove(output_filepath);
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/override.eo", "--gc", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/override_ref.c", output_filepath));
}
END_TEST

START_TEST(eolian_functions_descriptions)
{
   char output_filepath[PATH_MAX] = "";
   snprintf(output_filepath, PATH_MAX, "%s/eolian_output.h",
            eina_environment_tmp_get());
   remove(output_filepath);
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/class_simple.eo", "--gh", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/class_simple_ref_eo.h", output_filepath));
   remove(output_filepath);
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/class_simple.eo", "--legacy --gh", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/class_simple_ref_legacy.h", output_filepath));
}
END_TEST

START_TEST(eolian_import)
{
   char output_filepath[PATH_MAX] = "";
   snprintf(output_filepath, PATH_MAX, "%s/eolian_import_types.h",
            eina_environment_tmp_get());
   remove(output_filepath);
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/import_types.eot", "--gh", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/import_types_ref.h", output_filepath));
   remove(output_filepath);
}
END_TEST

START_TEST(eolian_docs)
{
   char output_filepath[PATH_MAX] = "";
   snprintf(output_filepath, PATH_MAX, "%s/eolian_output.h",
            eina_environment_tmp_get());
   remove(output_filepath);
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/docs.eo", "--gh", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/docs_ref.h", output_filepath));
   remove(output_filepath);
   fail_if(0 != _eolian_gen_execute(PACKAGE_DATA_DIR"/data/docs.eo", "--legacy --gh", output_filepath));
   fail_if(!_files_compare(PACKAGE_DATA_DIR"/data/docs_ref_legacy.h", output_filepath));
}
END_TEST

void eolian_generation_test(TCase *tc)
{
   tcase_add_test(tc, eolian_types_generation);
   tcase_add_test(tc, eolian_default_values_generation);
   tcase_add_test(tc, eolian_override_generation);
   tcase_add_test(tc, eolian_dev_impl_code);
   tcase_add_test(tc, eolian_functions_descriptions);
   tcase_add_test(tc, eolian_import);
   tcase_add_test(tc, eolian_docs);
}