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
|
#include <test.h>
#include <platform.h>
#include <alloc.h>
#include <string_expressions.h>
#include <string_lib.h>
static char *ForbiddenVarRefEval(ARG_UNUSED const char *varname,
ARG_UNUSED VarRefType type,
ARG_UNUSED void *param)
{
fail();
}
static char *IdentityVarRefEval(const char *varname,
ARG_UNUSED VarRefType type,
ARG_UNUSED void *param)
{
return xstrdup(varname);
}
static char *AppendAVarRefEval(const char *varname,
ARG_UNUSED VarRefType type,
ARG_UNUSED void *param)
{
return StringConcatenate(2, "a", varname);
}
static char *DiscriminateVarTypesVarRefEval(const char *varname, VarRefType type,
ARG_UNUSED void *param)
{
if (type == VAR_REF_TYPE_SCALAR)
{
return StringConcatenate(3, "cozy(", varname, ")");
}
else
{
return StringConcatenate(3, "ugly{", varname, "}");
}
}
static void CheckParse(const char *string_expression, const char *expected_output, VarRefEvaluator evaluator, void *param)
{
StringParseResult res = ParseStringExpression(string_expression, 0, strlen(string_expression));
assert_true(res.result);
char *eval_result = EvalStringExpression(res.result, evaluator, param);
assert_string_equal(expected_output, eval_result);
free(eval_result);
FreeStringExpression(res.result);
}
static void test_literal(void)
{
CheckParse("hello", "hello", ForbiddenVarRefEval, NULL);
}
static void test_var_naked(void)
{
CheckParse("$(foo)", "foo", IdentityVarRefEval, NULL);
}
static void test_var_naked_two_level(void)
{
CheckParse("$($(foo))", "aafoo", AppendAVarRefEval, NULL);
}
static void test_var_one_level(void)
{
CheckParse("$(foo)x$(bar)y$(baz)", "fooxbarybaz", IdentityVarRefEval, NULL);
}
static void test_different_var_types(void)
{
CheckParse("@{a$(b@(c)${d})@(e)}", "ugly{acozy(bugly{c}cozy(d))ugly{e}}", DiscriminateVarTypesVarRefEval, NULL);
}
int main()
{
PRINT_TEST_BANNER();
const UnitTest tests[] =
{
unit_test(test_literal),
unit_test(test_var_naked),
unit_test(test_var_naked_two_level),
unit_test(test_var_one_level),
unit_test(test_different_var_types),
};
return run_tests(tests);
}
|