Files
cve-check-tool/tests/check-template.c
T
Ikey Doherty 6dc11a4aed template: Move to block-based render passes for sections
We now build a complete unexpanded section at a time whilst iterating the
document structure. As we exit each subsection, we push our block into the
parent render block. This leads to a bottom up render approach for emitting
sections, skipping non-emitters. In future this can be expanded upon by
enabling iterative conditional sections, as a full section block is available
to render as many times as required into the parent context.

Signed-off-by: Ikey Doherty <michael.i.doherty@intel.com>
2015-04-30 12:17:17 +01:00

160 lines
4.8 KiB
C

/*
* This file is part of cve-check-tool
* Copyright (C) 2015 Intel Corporation
*
* cve-check-tool is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#define _GNU_SOURCE
#include <check.h>
#include <stdlib.h>
#include <stdio.h>
#include "cve-string.c"
#include "template.c"
#include "config.h"
/**
* Kept here as a no-op for now (linking)
*/
void cve_add_package(__attribute__((unused)) const char *path)
{
}
START_TEST(cve_template_basic)
{
gchar *ret = NULL;
GHashTable *table = NULL;
const char *unchanged = "I have no {{hashtable}}";
ret = template_string(unchanged, NULL);
fail_if(!ret, "No return string");
fail_if(!g_str_equal(unchanged, ret), "Differing string for no hashtable");
free(ret);
table = g_hash_table_new(g_str_hash, g_str_equal);
fail_if(!table, "Unable to allocate table");
g_hash_table_insert(table, "key1", "Frodo");
g_hash_table_insert(table, "key2", " Baggins");
g_hash_table_insert(table, "key3", "Hobbit");
g_hash_table_insert(table, "location", "Mordor");
const char *str = "{{key1}}{{key2}} was a curious {{key3}}. With hindsight it is likely he would have avoided {{location}}.";
const char *exp = "Frodo Baggins was a curious Hobbit. With hindsight it is likely he would have avoided Mordor.";
ret = template_string(str, table);
fail_if(!ret, "Unable to allocate string");
fail_if(!g_str_equal(ret, exp), "Built string does not match expected template output");
g_free(ret);
}
END_TEST
START_TEST(cve_template_context)
{
TemplateContext *top = NULL;
TemplateContext *child = NULL;
gchar *ret = NULL;
top = template_context_new();
fail_if(!top, "Failed to allocate TemplateContext");
template_context_add(top, "name", "correctname");
child = template_context_new();
fail_if(!child, "Failed to allocate TemplateContext");
template_context_add(child, "number", "42");
template_context_add_subcontext(top, "section", child);
char *input = "The name is {{name}}{{#norender}}Output{{/norender}}{{#section}} {{name}} {{number}}{{/section}}";
char *exp = "The name is correctname correctname 42";
ret = template_context_process_line(top, input, false);
fail_if(!ret, "Failed to get return fro template_context_process_line");
fail_if(!g_str_equal(ret, exp), "Returned context string does not match expected template output");
g_free(ret);
/* HTMLish */
input = "table a:visited { color: #999999 ; }";
ret = template_context_process_line(top, input, false);
fail_if(!g_str_equal(ret, input), "Returned context string does not match expected Style template output");
g_free(ret);
template_context_free(top);
g_free(ret);
}
END_TEST
START_TEST(cve_template_fails)
{
char *ret = NULL;
GHashTable *table = NULL;
table = g_hash_table_new(g_str_hash, g_str_equal);
fail_if(!table, "Unable to allocate table");
g_hash_table_insert(table, "failure", "onoes");
const char *msg = "This is {{failure 1";
/* Expected this will be stripped */
char *exp = "This is {{failure 1";
ret = template_string(msg, table);
fail_if(!ret, "Unable to allocate string");
fail_if(!g_str_equal(ret, exp), "Differing return string");
free(ret);
msg = "This is {{failure }2";
exp = "This is {{failure }2";
ret = template_string(msg, table);
fail_if(!ret, "Unable to allocate string");
fail_if(!g_str_equal(ret, exp), "Differing return string #2");
free(ret);
msg = "This is {{failure}} {{test}}!";
exp = "This is onoes !";
ret = template_string(msg, table);
fail_if(!ret, "Unable to allocate string");
fail_if(!g_str_equal(ret, exp), "Unknown value not collapsed");
free(ret);
}
END_TEST
static Suite *core_suite(void)
{
Suite *s = NULL;
TCase *tc = NULL;
s = suite_create("cve_template");
tc = tcase_create("cve_template_functions");
tcase_add_test(tc, cve_template_basic);
tcase_add_test(tc, cve_template_fails);
tcase_add_test(tc, cve_template_context);
suite_add_tcase(s, tc);
return s;
}
int main(void)
{
Suite *s;
SRunner *sr;
int fail;
s = core_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_VERBOSE);
fail = srunner_ntests_failed(sr);
srunner_free(sr);
if (fail > 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}