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