gplate/tags/tests/test-gplate-variable-tag.c
author Gary Kramlich <grim@reaperworld.com>
Sun Jul 04 15:34:45 2010 -0500 (22 months ago)
changeset 403 13b5031b2846
parent 400 0938cb5d076b
permissions -rw-r--r--
added a few more tests for gplate-variable-tag
     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 
    19 #include <glib.h>
    20 
    21 #include "test-gplate-tag.h"
    22 
    23 /******************************************************************************
    24  * Helpers
    25  *****************************************************************************/
    26 static void
    27 test_gplate_variable_tag(TestGPlateTagFixture *fixture, gconstpointer data,
    28                          const gchar *template_string, const gchar *expected,
    29                          ...)
    30 {
    31 	va_list vargs;
    32 	const gchar *name = NULL, *value = NULL;
    33 
    34 	va_start(vargs, expected);
    35 	while((name = va_arg(vargs, const gchar *))) {
    36 		value = va_arg(vargs, const gchar *);
    37 
    38 		gplate_collection_add_string(GPLATE_COLLECTION(fixture->template),
    39 		                             name, value);
    40 	}
    41 	va_end(vargs);
    42 
    43 	test_gplate_tag_output(fixture, data, template_string, expected);
    44 }
    45 
    46 /******************************************************************************
    47  * Tests
    48  *****************************************************************************/
    49 static void
    50 test_gplate_variable_tag_single(TestGPlateTagFixture *fixture,
    51                                 gconstpointer data)
    52 {
    53 	test_gplate_variable_tag(fixture, data,
    54 	                         "{{ foo }}", "foo",
    55 	                         "foo", "foo",
    56 	                         NULL);
    57 }
    58 
    59 static void
    60 test_gplate_variable_tag_double(TestGPlateTagFixture *fixture,
    61                                 gconstpointer data)
    62 {
    63 	test_gplate_variable_tag(fixture, data,
    64 	                         "{{ foo }}{{ bar }}", "foobar",
    65 	                         "foo", "foo",
    66 	                         "bar", "bar",
    67 	                         NULL);
    68 }
    69 
    70 static void
    71 test_gplate_variable_tag_tripple(TestGPlateTagFixture *fixture,
    72                                  gconstpointer data)
    73 {
    74 	test_gplate_variable_tag(fixture, data,
    75 	                         "{{ foo }}{{ bar }}{{ baz }}", "foobarbaz",
    76 	                         "foo", "foo",
    77 	                         "bar", "bar",
    78 	                         "baz", "baz",
    79 	                         NULL);
    80 }
    81 
    82 static void
    83 test_gplate_variable_tag_newline_prefix(TestGPlateTagFixture *fixture,
    84                                         gconstpointer data)
    85 {
    86 	test_gplate_variable_tag(fixture, data,
    87 	                         "{{\nfoo}}", "foo",
    88 	                         "foo", "foo",
    89 	                         NULL);
    90 }
    91 
    92 static void
    93 test_gplate_variable_tag_newline_suffix(TestGPlateTagFixture *fixture,
    94                                         gconstpointer data)
    95 {
    96 	test_gplate_variable_tag(fixture, data,
    97 	                         "{{foo\n}}", "foo",
    98 	                         "foo", "foo",
    99 	                         NULL);
   100 }
   101 
   102 static void
   103 test_gplate_variable_tag_newline_wrapped(TestGPlateTagFixture *fixture,
   104                                          gconstpointer data)
   105 {
   106 	test_gplate_variable_tag(fixture, data,
   107 	                         "{{\nfoo\n}}", "foo",
   108 	                         "foo", "foo",
   109 	                         NULL);
   110 }
   111 
   112 static void
   113 test_gplate_variable_tag_single_quotes(TestGPlateTagFixture *fixture,
   114                                        gconstpointer data)
   115 {
   116 	test_gplate_variable_tag(fixture, data,
   117 	                         "foo '{{ bar }}' baz", "foo 'bar' baz",
   118 	                         "bar", "bar",
   119 	                         NULL);
   120 }
   121 
   122 static void
   123 test_gplate_variable_tag_double_quotes(TestGPlateTagFixture *fixture,
   124                                        gconstpointer data)
   125 {
   126 	test_gplate_variable_tag(fixture, data,
   127 	                         "foo \"{{ bar }}\" baz", "foo \"bar\" baz",
   128 	                         "bar", "bar",
   129 	                         NULL);
   130 }
   131 
   132 static void
   133 test_gplate_variable_tag_mixed_quotes(TestGPlateTagFixture *fixture,
   134                                       gconstpointer data)
   135 {
   136 	test_gplate_variable_tag(fixture, data,
   137 	                         "foo \"{{ bar }}' baz", "foo \"bar' baz",
   138 	                         "bar", "bar",
   139 	                         NULL);
   140 
   141 	test_gplate_variable_tag(fixture, data,
   142 	                         "foo '{{ bar }}\" baz", "foo 'bar\" baz",
   143 	                         "bar", "bar",
   144 	                         NULL);
   145 }
   146 
   147 static void
   148 test_gplate_variable_tag_two_vars_one_block(TestGPlateTagFixture *fixture,
   149                                             gconstpointer data)
   150 {
   151 	test_gplate_variable_tag(fixture, data,
   152 	                         "one {{ two five }} three",
   153 	                         "one 2 three",
   154 	                         "two", "2",
   155 	                         "five", "5",
   156 	                         NULL);
   157 }
   158 
   159 static void
   160 test_gplate_variable_tag_nested_single(TestGPlateTagFixture *fixture,
   161                                        gconstpointer data)
   162 {
   163 	GPlateVariable *dict = gplate_dictionary_variable_new("foo");
   164 
   165 	gplate_collection_add_string(GPLATE_COLLECTION(dict),
   166 	                             "bar", "baz");
   167 
   168 	test_gplate_tag_output(fixture, data, "{{ foo.bar }}", "baz");
   169 }
   170 
   171 static void
   172 test_gplate_variable_tag_nested_double(TestGPlateTagFixture *fixture,
   173                                        gconstpointer data)
   174 {
   175 	GPlateVariable *dict1 = gplate_dictionary_variable_new("foo");
   176 	GPlateVariable *dict2 = gplate_dictionary_variable_new("bar");
   177 
   178 	gplate_collection_add_variable(GPLATE_COLLECTION(fixture->template),
   179 	                               dict1);
   180 	gplate_collection_add_variable(GPLATE_COLLECTION(dict1), dict2);
   181 	gplate_collection_add_string(GPLATE_COLLECTION(dict2),
   182 	                             "baz", "42");
   183 
   184 	test_gplate_tag_output(fixture, data, "{{ foo.bar.baz }}", "42");
   185 }
   186 
   187 /******************************************************************************
   188  * Main
   189  *****************************************************************************/
   190 gint
   191 main(gint argc, gchar **argv) {
   192 	g_test_init(&argc, &argv, NULL);
   193 
   194 	g_type_init();
   195 
   196 	gplate_config_load_default();
   197 
   198 	test_gplate_tag_add("/tags/variable/single",
   199 	                    test_gplate_variable_tag_single);
   200 
   201 	test_gplate_tag_add("/tags/variable/double",
   202 	                    test_gplate_variable_tag_double);
   203 
   204 	test_gplate_tag_add("/tags/variable/tripple",
   205 	                    test_gplate_variable_tag_tripple);
   206 
   207 	test_gplate_tag_add("/tags/variable/newline_prefix",
   208 	                    test_gplate_variable_tag_newline_prefix);
   209 
   210 	test_gplate_tag_add("/tags/variable/newline_suffix",
   211 	                    test_gplate_variable_tag_newline_suffix);
   212 
   213 	test_gplate_tag_add("/tags/variable/newline_wrapped",
   214 	                    test_gplate_variable_tag_newline_wrapped);
   215 
   216 	test_gplate_tag_add("/tags/variable/quotes/single",
   217 	                    test_gplate_variable_tag_single_quotes);
   218 
   219 	test_gplate_tag_add("/tags/variable/quotes/double",
   220 	                    test_gplate_variable_tag_double_quotes);
   221 
   222 	test_gplate_tag_add("/tags/variable/quotes/mixed",
   223 	                    test_gplate_variable_tag_mixed_quotes);
   224 
   225 	test_gplate_tag_add("/tags/variable/two_vars_one_block",
   226 	                    test_gplate_variable_tag_two_vars_one_block);
   227 
   228 	test_gplate_tag_add("/tags/variables/nested/single",
   229 	                    test_gplate_variable_tag_nested_single);
   230 
   231 	test_gplate_tag_add("/tags/variable/nested/double",
   232 	                    test_gplate_variable_tag_nested_double);
   233 
   234 	return g_test_run();
   235 }
   236