Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
tests/variables.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2017 Tobias Ellinghaus.
4 Copyright (C) 2021 Hanno Schwalm.
5 Copyright (C) 2021 Profoktor.
6 Copyright (C) 2022, 2025 Aurélien PIERRE.
7 Copyright (C) 2022 Martin Bařinka.
8
9 darktable is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 darktable is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with darktable. If not, see <http://www.gnu.org/licenses/>.
21*/
22#include "common/darktable.h"
23#include "common/variables.h"
24
25#include <stdio.h>
26
27#ifdef _WIN32
28#include "win/main_wrapper.h"
29#endif
30
31typedef struct test_case_t
32{
35
41
42int run_test(const test_t *test, int *n_tests, int *n_failed)
43{
46 params->filename = test->filename;//"abcdef12345abcdef";
47 params->jobcode = test->jobcode;//"ABCDEF12345ABCDEF";
48 params->sequence = test->sequence;
49
50 *n_failed = 0;
51 *n_tests = 0;
52 for(const test_case_t *test_case = test->test_cases; test_case->input; test_case++)
53 {
54 (*n_tests)++;
55 char *result = dt_variables_expand(params, test_case->input, FALSE);
56 if(g_strcmp0(result, test_case->expected_result))
57 {
58 (*n_failed)++;
59 printf(" [FAIL] input: '%s', result: '%s', expected: '%s'\n", test_case->input, result, test_case->expected_result);
60 }
61 else
62 printf(" [OK] input: '%s', result: '%s'\n", test_case->input, result);
63 }
64
66
67 return *n_failed > 0 ? 1 : 0;
68}
69
70static const test_t test_variables = {
71 "abcdef12345abcdef", "ABCDEF12345ABCDEF", 23,
72 {
73 {"$(FILE_NAME)", "abcdef12345abcdef"},
74 {"foo-$(FILE_NAME)-bar", "foo-abcdef12345abcdef-bar"},
75 {"äöü-$(FILE_NAME)-äöü", "äöü-abcdef12345abcdef-äöü"},
76 {"$(FILE_NAME).$(SEQUENCE)", "abcdef12345abcdef.0023"},
77 {"$(NONEXISTANT)", ""},
78 {"foo-$(NONEXISTANT)-bar", "foo--bar"},
79
80 {NULL, NULL}
81 }
82};
83
85 "abcdef12345abcdef", "ABCDEF12345ABCDEF", 23,
86 {
87 {"$(NONEXISTANT-invälid)", "invälid"},
88 {"$(FILE_NAME-invälid)", "abcdef12345abcdef"},
89
90 {"$(NONEXISTANT+exißts)", ""},
91 {"$(FILE_NAME+exißts)", "exißts"},
92
93 {"$(NONEXISTANT:0)", ""},
94 {"$(FILE_NAME:0)", "abcdef12345abcdef"},
95 {"$(FILE_NAME:5)", "f12345abcdef"},
96 {"$(FILE_NAME:42)", ""},
97 {"$(FILE_NAME:-5)", "bcdef"},
98 {"$(FILE_NAME:-42)", "abcdef12345abcdef"},
99 {"$(FILE_NAME:0:5)", "abcde"},
100 {"$(FILE_NAME:5:3)", "f12"},
101 {"$(FILE_NAME:5:42)", "f12345abcdef"},
102 {"$(FILE_NAME:-5:3)", "bcd"},
103 {"$(FILE_NAME:-7:-2)", "5abcd"},
104 {"$(FILE_NAME:)", "abcdef12345abcdef"},
105 {"$(FILE_NAME:5:)", ""},
106
107 {"$(NONEXISTANT#abc)", ""},
108 {"$(FILE_NAME#abc)", "def12345abcdef"},
109 {"$(FILE_NAME#def)", "abcdef12345abcdef"},
110
111 {"$(NONEXISTANT%abc)", ""},
112 {"$(FILE_NAME%abc)", "abcdef12345abcdef"},
113 {"$(FILE_NAME%def)", "abcdef12345abc"},
114
115 {"$(NONEXISTANT/abc/def)", ""},
116 {"$(FILE_NAME/abc/foobar)", "foobardef12345abcdef"},
117 {"$(FILE_NAME/def/foobar)", "abcfoobar12345abcdef"},
118 {"$(FILE_NAME//abc/foobar)", "foobardef12345foobardef"},
119 {"$(FILE_NAME//def/foobar)", "abcfoobar12345abcfoobar"},
120 {"$(FILE_NAME/#abc/foobar)", "foobardef12345abcdef"},
121 {"$(FILE_NAME/#def/foobar)", "abcdef12345abcdef"},
122 {"$(FILE_NAME/%abc/foobar)", "abcdef12345abcdef"},
123 {"$(FILE_NAME/%def/foobar)", "abcdef12345abcfoobar"},
124
125 {"$(NONEXISTANT^)", ""},
126 {"$(NONEXISTANT^^)", ""},
127 {"$(FILE_NAME^)", "Abcdef12345abcdef"},
128 {"$(FILE_NAME^^)", "ABCDEF12345ABCDEF"},
129
130 {"$(NONEXISTANT,)", ""},
131 {"$(NONEXISTANT,,)", ""},
132 {"$(JOBCODE,)", "aBCDEF12345ABCDEF"},
133 {"$(JOBCODE,,)", "abcdef12345abcdef"},
134
135 {NULL, NULL}
136 }
137};
138
140 "abcdef12345abcdef", "ABCDEF12345ABCDEF", 23,
141 {
142 {"x$(TITLE-$(FILE_NAME))y", "xabcdef12345abcdefy"},
143 {"x$(TITLE-a-$(FILE_NAME)-b)y", "xa-abcdef12345abcdef-by"},
144 {"x$(SEQUENCE-$(FILE_NAME))y", "x0023y"},
145 {"x$(FILE_NAME/12345/$(SEQUENCE))y", "xabcdef0023abcdefy"},
146 {"x$(FILE_NAME/12345/.$(SEQUENCE).)y", "xabcdef.0023.abcdefy"},
147
148 {NULL, NULL}
149 }
150};
151
153 "abcdef12345abcdef", "ABCDEF12345ABCDEF", 23,
154 {
155 {"$(NONEXISTANT", "$(NONEXISTANT"},
156 {"x(NONEXISTANT23", "x(NONEXISTANT23"},
157 {"$(FILE_NAME", "$(FILE_NAME"},
158 {"x$(FILE_NAME", "x$(FILE_NAME"},
159 {"x$(TITLE-$(FILE_NAME)", "x$(TITLE-abcdef12345abcdef"},
160
161 {NULL, NULL}
162 }
163};
164
165static const test_t test_escapes = {
166 "/home/test/Images/IMG_0123.CR2", "/home/test/", 23,
167 {
168 {"foobarbaz", "foobarbaz"},
169 {"foo/bar/baz", "foo/bar/baz"},
170 {"foo\\bar\\baz", "foobarbaz"},
171 {"foo\\\\bar\\\\baz", "foo\\bar\\baz"},
172 {"foo\\$(bar", "foo$(bar"},
173 {"foo$\\(bar", "foo$(bar"},
174 {"foo\\$\\(bar", "foo$(bar"},
175 {"foo\\$(bar$(SEQUENCE)baz", "foo$(bar0023baz"},
176 {"foo$(bar$(SEQUENCE)baz", "foo$(bar0023baz"},
177 {"$(FILE_FOLDER)/ansel_exported/img_$(SEQUENCE)", "/home/test/Images/ansel_exported/img_0023"},
178 {"$(FILE_FOLDER)/ansel_exported/$(FILE_NAME)", "/home/test/Images/ansel_exported/IMG_0123"},
179
180 {NULL, NULL}
181 }
182};
183
184static const test_t test_real_paths = {
185 "/home/test/Images/0023/IMG_0123.CR2", "/home/test", 23,
186 {
187 {"$(FILE_FOLDER#$(JOBCODE))", "/Images/0023"},
188 {"$(FILE_FOLDER#$(JOBCODE)/Images)", "/0023"},
189
190 {"$(FILE_FOLDER%$(SEQUENCE))", "/home/test/Images/"},
191 {"$(FILE_FOLDER%/$(SEQUENCE))", "/home/test/Images"},
192
193 {"$(FILE_FOLDER/test/$(SEQUENCE))", "/home/0023/Images/0023"},
194 {"$(FILE_FOLDER/test/$(SEQUENCE)-$(SEQUENCE))", "/home/0023-0023/Images/0023"},
195 {"$(FILE_FOLDER/test/$(SEQUENCE//0/o))", "/home/oo23/Images/0023"},
196 {"$(FILE_FOLDER/$(SEQUENCE)/XXX)", "/home/test/Images/XXX"},
197 {"$(FILE_FOLDER/$(JOBCODE)\\///media/)", "/media/Images/0023"},
198 {"$(FILE_FOLDER/\\/home\\/test\\///media/exports/)/ansel_exported/img_$(SEQUENCE)", "/media/exports/Images/0023/ansel_exported/img_0023"},
199
200 {"$(FILE_FOLDER/", "$(FILE_FOLDER/"},
201 {"$(FILE_FOLDER/home", "$(FILE_FOLDER/home"},
202 {"$(FILE_FOLDER/home/media", "$(FILE_FOLDER/home/media"},
203 {"$(FILE_FOLDER/home/media)", "/media/test/Images/0023"},
204
205 {NULL, NULL}
206 }
207};
208
209
210#define TEST(t) \
211{\
212 int n_failed = 0, n_tests = 0;\
213 n_test_functions++;\
214 printf("running test '" #t "'\n");\
215 n_test_functions_failed += run_test(&t, &n_tests, &n_failed);\
216 n_tests_overall += n_tests;\
217 n_failed_overall += n_failed;\
218 printf("%d / %d tests failed\n\n", n_failed, n_tests);\
219}
220
221int main(int argc, char* argv[])
222{
223 char *argv_override[] = {"ansel-test-variables", "--library", ":memory:", "--conf", "write_sidecar_files=FALSE", NULL};
224 int argc_override = sizeof(argv_override) / sizeof(*argv_override) - 1;
225
226 // init dt without gui and without data.db:
227 if(dt_init(argc_override, argv_override, FALSE, FALSE)) exit(1);
228
229 int n_tests_overall = 0, n_failed_overall = 0, n_test_functions = 0, n_test_functions_failed = 0;
230
232
234
236
238
240
242
243 printf("%d / %d tests failed (%d / %d)\n",
244 n_failed_overall,
245 n_tests_overall,
246 n_test_functions_failed,
247 n_test_functions);
248
249 dt_cleanup();
250
251 return 0;
252}
253// clang-format off
254// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
255// vim: shiftwidth=2 expandtab tabstop=2 cindent
256// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
257// clang-format on
#define FALSE
Definition ashift_lsd.c:158
void dt_cleanup()
Definition darktable.c:1311
int dt_init(int argc, char *argv[], const gboolean init_gui, const gboolean load_data)
Definition darktable.c:451
int main()
Definition prova.c:47
char * dt_variables_expand(dt_variables_params_t *params, gchar *source, gboolean iterate)
void dt_variables_params_destroy(dt_variables_params_t *params)
void dt_variables_params_init(dt_variables_params_t **params)
char * expected_result
test_case_t test_cases[]
char * jobcode
char sequence
char * filename
static const test_t test_recursive_substitutions
static const test_t test_variables
static const test_t test_real_paths
static const test_t test_simple_substitutions
int run_test(const test_t *test, int *n_tests, int *n_failed)
static const test_t test_escapes
static const test_t test_broken_variables
#define TEST(t)