2 * config-test.c: tests svn_config
4 * ====================================================================
5 * Copyright (c) 2000-2004 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
19 /* ====================================================================
20 To add tests, look toward the bottom of this file.
28 #include <apr_getopt.h>
29 #include <apr_pools.h>
31 #include "svn_error.h"
32 #include "svn_config.h"
34 #include "../svn_test.h"
37 /* Initialize parameters for the tests. */
39 extern const char **test_argv
;
41 static const apr_getopt_option_t opt_def
[] =
43 {"srcdir", 'S', 1, "the source directory for VPATH test runs"},
46 static const char *srcdir
= NULL
;
48 static svn_error_t
*init_params(apr_pool_t
*pool
)
55 apr_getopt_init(&opt
, pool
, test_argc
, test_argv
);
56 while (!(status
= apr_getopt_long(opt
, opt_def
, &optch
, &opt_arg
)))
67 return svn_error_create(SVN_ERR_TEST_FAILED
, 0,
68 "missing required parameter '--srcdir'");
73 /* A quick way to create error messages. */
75 fail(apr_pool_t
*pool
, const char *fmt
, ...)
81 msg
= apr_pvsprintf(pool
, fmt
, ap
);
84 return svn_error_create(SVN_ERR_TEST_FAILED
, 0, msg
);
88 static const char *config_keys
[] = { "foo", "a", "b", "c", "d", "e", "f", "g",
90 static const char *config_values
[] = { "bar", "Aa", "100", "bar",
91 "a %(bogus)s oyster bar",
93 "%Aa", "lyrical bard", "%(unterminated",
97 test1(const char **msg
,
98 svn_boolean_t msg_only
,
99 svn_test_opts_t
*opts
,
104 const char *cfg_file
;
106 *msg
= "test svn_config";
112 SVN_ERR(init_params(pool
));
114 cfg_file
= apr_pstrcat(pool
, srcdir
, "/", "config-test.cfg", NULL
);
115 SVN_ERR(svn_config_read(&cfg
, cfg_file
, TRUE
, pool
));
117 /* Test values retrieved from our ConfigParser instance against
118 values retrieved using svn_config. */
119 for (i
= 0; config_keys
[i
] != NULL
; i
++)
121 const char *key
, *py_val
, *c_val
;
123 key
= config_keys
[i
];
124 py_val
= config_values
[i
];
125 svn_config_get(cfg
, &c_val
, "section1", key
, "default value");
127 printf("Testing expected value '%s' against '%s' for "
128 "option '%s'\n", py_val
, c_val
, key
);
130 /* Fail iff one value is null, or the strings don't match. */
131 if ((c_val
== NULL
) != (py_val
== NULL
)
132 || (c_val
!= NULL
&& py_val
!= NULL
&& strcmp(c_val
, py_val
) != 0))
133 return fail(pool
, "Expected value '%s' not equal to '%s' for "
134 "option '%s'", py_val
, c_val
, key
);
140 static const char *true_keys
[] = {"true1", "true2", "true3", "true4",
142 static const char *false_keys
[] = {"false1", "false2", "false3", "false4",
146 test2(const char **msg
,
147 svn_boolean_t msg_only
,
148 svn_test_opts_t
*opts
,
153 const char *cfg_file
;
155 *msg
= "test svn_config boolean conversion";
161 SVN_ERR(init_params(pool
));
163 cfg_file
= apr_pstrcat(pool
, srcdir
, "/", "config-test.cfg", NULL
);
164 SVN_ERR(svn_config_read(&cfg
, cfg_file
, TRUE
, pool
));
166 for (i
= 0; true_keys
[i
] != NULL
; i
++)
169 SVN_ERR(svn_config_get_bool(cfg
, &value
, "booleans", true_keys
[i
],
172 return fail(pool
, "Value of option '%s' is not true", true_keys
[i
]);
175 for (i
= 0; false_keys
[i
] != NULL
; i
++)
178 SVN_ERR(svn_config_get_bool(cfg
, &value
, "booleans", false_keys
[i
],
181 return fail(pool
, "Value of option '%s' is not true", false_keys
[i
]);
188 svn_error_clear((err
= svn_config_get_bool(cfg
, &value
,
189 "booleans", "bad_true",
192 return fail(pool
, "No error on bad truth value");
194 svn_error_clear((err
= svn_config_get_bool(cfg
, &value
,
195 "booleans", "bad_false",
198 return fail(pool
, "No error on bad truth value");
205 has_section_test(const char **msg
,
206 svn_boolean_t msg_only
,
207 svn_test_opts_t
*opts
,
211 const char *cfg_file
;
213 *msg
= "test svn_config_has_section";
219 SVN_ERR(init_params(pool
));
221 cfg_file
= apr_pstrcat(pool
, srcdir
, "/", "config-test.cfg", NULL
);
222 SVN_ERR(svn_config_read(&cfg
, cfg_file
, TRUE
, pool
));
224 if (! svn_config_has_section(cfg
, "section1"))
225 return fail(pool
, "Failed to find section1");
227 if (svn_config_has_section(cfg
, "notthere"))
228 return fail(pool
, "Returned true on missing section");
234 ====================================================================
235 If you add a new test to this file, update this array.
237 (These globals are required by our included main())
240 /* An array of all test functions */
241 struct svn_test_descriptor_t test_funcs
[] =
244 SVN_TEST_PASS(test1
),
245 SVN_TEST_PASS(test2
),
246 SVN_TEST_PASS(has_section_test
),