forked from sass/libsass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_theories.c
211 lines (171 loc) · 5.95 KB
/
spec_theories.c
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
#include <stdio.h>
#include <stdlib.h>
#include <criterion/hooks.h>
#include <Criterion/theories.h>
#include <sass.h>
#include "tinydir/tinydir.h"
bool test_todo = false;
static void add(char ***arr, const char *value, int *length) {
++(*length);
char *temp = (char *)realloc(*arr, (*length) * sizeof(**arr));
if (temp) {
*arr = temp;
(*arr)[*length - 1] = (char *)malloc(strlen(value) * sizeof(char) + 1);
strcpy((*arr)[*length - 1], value);
}
else {
--(*length);
}
}
static void empty(char ***arr, int length) {
for (int i = 0; i < length; ++i) {
free((*arr)[i]);
}
}
// You must free the result if result is non-NULL.
char *str_replace(char *orig, char *rep, char *with) {
char *result; // the return string
char *ins; // the next insert point
char *tmp; // varies
int len_rep; // length of rep
int len_with; // length of with
int len_front; // distance between rep and end of last rep
int count; // number of replacements
if (!orig)
return NULL;
if (!rep)
rep = "";
len_rep = strlen(rep);
if (!with)
with = "";
len_with = strlen(with);
ins = orig;
for (count = 0; tmp = strstr(ins, rep); ++count) {
ins = tmp + len_rep;
}
// first time through the loop, all the variable are set correctly
// from here on,
// tmp points to the end of the result string
// ins points to the next occurrence of rep in orig
// orig points to the remainder of orig after "end of rep"
tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
if (!result)
return NULL;
while (count--) {
ins = strstr(orig, rep);
len_front = ins - orig;
tmp = strncpy(tmp, orig, len_front) + len_front;
tmp = strcpy(tmp, with) + len_with;
orig += len_front + len_rep; // move to next "end of rep"
}
strcpy(tmp, orig);
return result;
}
static void recursively_collect(char ***arr, const char *path, int *len) {
tinydir_dir dir;
tinydir_open(&dir, path);
while (dir.has_next) {
tinydir_file file;
if (tinydir_readfile(&dir, &file) == -1) {
perror("Error getting file");
break;
}
if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0) {
tinydir_next(&dir);
continue;
}
if (file.is_dir && (test_todo || !strstr(file.name, "-todo-"))) {
recursively_collect(arr, file.path, len);
}
else if (strcmp(file.name, "input.scss") == 0 || strcmp(file.name, "input.sass") == 0) {
add(arr, file.path, len);
}
tinydir_next(&dir);
}
tinydir_close(&dir);
}
static char *read_file(const char *path) {
FILE *fp;
long lSize;
char *buffer;
fp = fopen(path, "rb");
if (!fp) perror(path), exit(1);
fseek(fp, 0L, SEEK_END);
lSize = ftell(fp);
rewind(fp);
buffer = calloc(1, lSize + 1);
if (!buffer) fclose(fp), fputs("memory alloc fails", stderr), exit(1);
fread(buffer, lSize, 1, fp);
fclose(fp);
return buffer;
}
static void list_directories(void ***out, size_t *size) {
int len = 0;
char *spec_dir = str_replace(__FILE__, "spec_theories.c", "sass-spec/spec");
char *todo_var = getenv("LIBSASS_TEST_TODO");
test_todo = !strcmp("1", (todo_var ? todo_var : "0"));
(*out) = malloc(sizeof(char*));
recursively_collect(&(*out), spec_dir, &len);
free(spec_dir);
(*size) = len;
}
TheoryDataPoints(theory, gen) = {
DataPoints(const char *, "") // parameter placeholder
};
ReportHook(ASSERT)(struct criterion_assert_stats *test) {
// TODO: Open GH issue and describe that we need DataPoint argument here
if (test->passed) {// && test_todo && strstr(test->message, "-todo-")) {
//printf(".\nAlert!\nMarked as todo and passed %s\n\n", test->file);
}
}
static void generate_datapoints(void) {
list_directories(&TheoryDataPoint(theory, gen)[0].arr, &TheoryDataPoint(theory, gen)[0].len);
}
static void free_datapoints(void) {
empty(&TheoryDataPoint(theory, gen)[0].arr, TheoryDataPoint(theory, gen)[0].len);
free(TheoryDataPoint(theory, gen)[0].arr);
}
// to be purged once PCRE is configured
static char *trim(char *c)
{
int i;
while (isspace(*c)) c++;
for (i = strlen(c) - 1; i > -1 && isspace(c[i]); --i);
if (i > -1)
c[i + 1] = '\0';
return c;
}
Theory((const char *input_path), theory, gen, .init = generate_datapoints, .fini = free_datapoints) {
char *output = strstr(input_path, "input.scss") ?
str_replace(input_path, "input.scss", "expected_output.css") :
str_replace(input_path, "input.sass", "expected_output.css");
/* TODO: Needs an efficient way to repeat the process for these:
char *output_compact = str_replace(str_replace(input_path, "input.scss", "expected.compact.css"), "input.sass", "expected.compact.css");
char *output_compressed = str_replace(str_replace(input_path, "input.scss", "expected.compressed.css"), "input.sass", "expected.compressed.css");
char *output_expanded = str_replace(str_replace(input_path, "input.scss", "expected.expanded.css"), "input.sass", "expected.expanded.css");
*/
struct Sass_File_Context* ctx = sass_make_file_context(input_path);
struct Sass_Context* ctx_out = sass_file_context_get_context(ctx);
struct Sass_Options* options = sass_make_options();
#ifdef WIN32 // to be purged once PCRE is configured
sass_option_set_linefeed(options, "\r\n");
#endif
sass_option_set_output_style(options, SASS_STYLE_NESTED);
sass_option_set_output_path(options, output);
sass_option_set_input_path(options, input_path);
sass_file_context_set_options(ctx, options);
sass_compile_file_context(ctx);
int error_status = sass_context_get_error_status(ctx_out);
const char *error_message = sass_context_get_error_message(ctx_out);
const char *actual = sass_context_get_output_string(ctx_out);
char *expected = read_file(output);
if (error_status) {
cr_assert_str_eq(error_message, expected);
}
else {
cr_assert_str_eq(actual, expected, "\n%d\t%d\n%s\n%s\n%s\n", strlen(expected), strlen(actual), input_path, expected, actual);
}
free(output);
free(expected);
sass_delete_file_context(ctx);
}