gplate/functions/tests/test-for-function.c
author Gary Kramlich <grim@reaperworld.com>
Sat Jul 03 23:24:33 2010 -0500 (22 months ago)
changeset 394 ad26d98e20e6
parent 381 125f0d26e4cb
permissions -rw-r--r--
Changed the license from gplv2 to gplv3
Updated copyrights to be 2007-2010 rather than 2007-2008 (since just about everything has been touched recently
Ignored the top level tests/ directory for now since that is slowly being replaced
grim@394
     1
/*
grim@394
     2
 * Copyright (C) 2007-2010 Gary Kramlich <grim@reaperworld.com>
grim@394
     3
 *
grim@394
     4
 * This program is free software: you can redistribute it and/or modify
grim@394
     5
 * it under the terms of the GNU General Public License as published by
grim@394
     6
 * the Free Software Foundation, either version 3 of the License, or
grim@394
     7
 * (at your option) any later version.
grim@394
     8
 *
grim@394
     9
 * This program is distributed in the hope that it will be useful,
grim@394
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
grim@394
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
grim@394
    12
 * GNU General Public License for more details.
grim@394
    13
 *
grim@394
    14
 * You should have received a copy of the GNU General Public License
grim@394
    15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
grim@394
    16
 */
grim@381
    17
#include <gplate/gplate.h>
grim@381
    18
#include <gplate/variables/gplate-dictionary-variable.h>
grim@264
    19
grim@381
    20
#include <glib.h>
grim@264
    21
grim@264
    22
/******************************************************************************
grim@264
    23
 * Structs
grim@264
    24
 *****************************************************************************/
grim@264
    25
typedef struct {
grim@264
    26
	const gchar *name;
grim@264
    27
	const gchar *value;
grim@381
    28
} GPlateForFunctionTestData;
grim@264
    29
grim@381
    30
typedef struct {
grim@381
    31
	GPlateTemplate *template;
grim@381
    32
grim@381
    33
	GPlateVariable *dict;
grim@381
    34
grim@381
    35
	const gchar *template_string;
grim@381
    36
grim@381
    37
	gchar *actual;
grim@381
    38
	const gchar *expected;
grim@381
    39
grim@381
    40
	GError *error;
grim@381
    41
grim@381
    42
	GPlateForFunctionTestData *data;
grim@381
    43
} GPlateForFunctionTestFixture;
grim@264
    44
grim@264
    45
/******************************************************************************
grim@264
    46
 * Fixtures
grim@264
    47
 *****************************************************************************/
grim@264
    48
static void
grim@381
    49
gplate_for_function_test_setup(GPlateForFunctionTestFixture *fixture,
grim@381
    50
                               gconstpointer d)
grim@381
    51
{
grim@381
    52
	fixture->template = gplate_template_new();
grim@381
    53
	fixture->dict = gplate_dictionary_variable_new("list");
grim@264
    54
grim@381
    55
	gplate_collection_add_variable(GPLATE_COLLECTION(fixture->template),
grim@381
    56
	                               fixture->dict);
grim@264
    57
}
grim@264
    58
grim@264
    59
static void
grim@381
    60
gplate_for_function_test_teardown(GPlateForFunctionTestFixture *fixture,
grim@381
    61
                                  gconstpointer d)
grim@381
    62
{
grim@381
    63
	g_object_unref(fixture->template);
grim@381
    64
	fixture->template = NULL;
grim@264
    65
grim@381
    66
	g_object_unref(fixture->dict);
grim@381
    67
	fixture->dict = NULL;
grim@381
    68
grim@381
    69
	g_free(fixture->actual);
grim@381
    70
	fixture->actual = NULL;
grim@381
    71
grim@381
    72
	fixture->expected = NULL;
grim@381
    73
grim@381
    74
	if(fixture->error) {
grim@381
    75
		g_error_free(fixture->error);
grim@381
    76
		fixture->error = NULL;
grim@381
    77
	}
grim@264
    78
}
grim@264
    79
grim@264
    80
/******************************************************************************
grim@264
    81
 * Helpers
grim@264
    82
 *****************************************************************************/
grim@264
    83
static void
grim@381
    84
gplate_for_function_test(GPlateForFunctionTestFixture *fixture) {
grim@381
    85
	gint i = 0;
grim@264
    86
grim@264
    87
	/* add the variables */
grim@381
    88
	for(i = 0; fixture->data[i].name; i++) {
grim@381
    89
		gplate_collection_add_string(fixture->dict,
grim@381
    90
		                             fixture->data[i].name,
grim@381
    91
		                             fixture->data[i].value);
grim@381
    92
	}
grim@264
    93
grim@381
    94
	fixture->actual = gplate_template_render(fixture->template,
grim@381
    95
	                                         fixture->template_string,
grim@381
    96
	                                         &fixture->error);
grim@381
    97
grim@381
    98
	g_assert(fixture->error == NULL);
grim@381
    99
grim@381
   100
	g_assert_cmpstr(fixture->expected, ==, fixture->actual);
grim@264
   101
}
grim@264
   102
grim@264
   103
/******************************************************************************
grim@264
   104
 * Simple For's
grim@264
   105
 *****************************************************************************/
grim@381
   106
static void
grim@381
   107
gplate_for_function_test_zero_elements(GPlateForFunctionTestFixture *fixture,
grim@381
   108
                                       gconstpointer user_data)
grim@381
   109
{
grim@381
   110
	GPlateForFunctionTestData data[] = {
grim@292
   111
		{ NULL, NULL },
grim@292
   112
	};
grim@292
   113
grim@381
   114
	fixture->data = data;
grim@381
   115
	fixture->template_string = "{% for in in line %}{{ i }}{% endfor %}";
grim@381
   116
	fixture->expected = "";
grim@292
   117
grim@381
   118
	gplate_for_function_test(fixture);
grim@381
   119
}
grim@381
   120
grim@381
   121
static void
grim@381
   122
gplate_for_function_test_one_element(GPlateForFunctionTestFixture *fixture,
grim@381
   123
                                     gconstpointer user_data)
grim@381
   124
{
grim@381
   125
	GPlateForFunctionTestData data[] = {
grim@264
   126
		{ "one", "1" },
grim@264
   127
		{ NULL, NULL },
grim@264
   128
	};
grim@264
   129
grim@381
   130
	fixture->data = data;
grim@381
   131
	fixture->template_string = "{% for i in list %}{{ i }}{% endfor %}";
grim@381
   132
	fixture->expected = "1";
grim@264
   133
grim@381
   134
	gplate_for_function_test(fixture);
grim@381
   135
}
grim@381
   136
grim@381
   137
static void
grim@381
   138
gplate_for_function_test_two_elements(GPlateForFunctionTestFixture *fixture,
grim@381
   139
                                      gconstpointer user_data)
grim@381
   140
{
grim@381
   141
	GPlateForFunctionTestData data[] = {
grim@264
   142
		{ "a", "A" },
grim@264
   143
		{ "b", "B" },
grim@264
   144
		{ NULL, NULL },
grim@264
   145
	};
grim@264
   146
grim@381
   147
	fixture->data = data;
grim@381
   148
	fixture->template_string = "{% for i in list %}{{ i }}{% endfor %}";
grim@381
   149
	fixture->expected = "AB";
grim@381
   150
grim@381
   151
	gplate_for_function_test(fixture);
grim@381
   152
}
grim@264
   153
grim@264
   154
/******************************************************************************
grim@381
   155
 * Main!
grim@264
   156
 *****************************************************************************/
grim@381
   157
gint
grim@381
   158
main(gint argc, gchar **argv) {
grim@381
   159
	g_test_init(&argc, &argv, NULL);
grim@264
   160
grim@381
   161
	g_type_init();
grim@381
   162
grim@381
   163
	gplate_config_load_default();
grim@381
   164
grim@381
   165
	g_test_add("/functions/foo/zero_elements",
grim@381
   166
	           GPlateForFunctionTestFixture,
grim@381
   167
	           NULL,
grim@381
   168
	           gplate_for_function_test_setup,
grim@381
   169
	           gplate_for_function_test_zero_elements,
grim@381
   170
	           gplate_for_function_test_teardown);
grim@381
   171
grim@381
   172
	g_test_add("/functions/foo/one_element",
grim@381
   173
	           GPlateForFunctionTestFixture,
grim@381
   174
	           NULL,
grim@381
   175
	           gplate_for_function_test_setup,
grim@381
   176
	           gplate_for_function_test_one_element,
grim@381
   177
	           gplate_for_function_test_teardown);
grim@381
   178
grim@381
   179
	g_test_add("/functions/foo/two_elements",
grim@381
   180
	           GPlateForFunctionTestFixture,
grim@381
   181
	           NULL,
grim@381
   182
	           gplate_for_function_test_setup,
grim@381
   183
	           gplate_for_function_test_two_elements,
grim@381
   184
	           gplate_for_function_test_teardown);
grim@381
   185
grim@381
   186
	return g_test_run();
grim@264
   187
}
grim@264
   188