test_mustach_jansson.c (4058B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014-2020 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 3, or 8 (at your option) any later version. 9 10 TALER is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with TALER; see the file COPYING. If not, see 17 <http://www.gnu.org/licenses/> 18 */ 19 20 /** 21 * @file test_mustach_jansson.c 22 * @brief testcase to test the mustach/jansson integration 23 * @author Florian Dold 24 */ 25 #include "mustach-jansson.h" 26 #include <gnunet/gnunet_util_lib.h> 27 28 static void 29 assert_template (const char *template, 30 json_t *root, 31 const char *expected) 32 { 33 char *r; 34 size_t sz; 35 36 GNUNET_assert (0 == mustach_jansson_mem (template, 37 0, 38 root, 39 Mustach_With_AllExtensions, 40 &r, 41 &sz)); 42 GNUNET_assert (0 == strcmp (r, 43 expected)); 44 GNUNET_free (r); 45 } 46 47 48 int 49 main (int argc, 50 char *const *argv) 51 { 52 json_t *root = json_object (); 53 json_t *arr = json_array (); 54 json_t *obj = json_object (); 55 /* test 1 */ 56 const char *t1 = "hello world"; 57 const char *x1 = "hello world"; 58 /* test 2 */ 59 const char *t2 = "hello {{ v1 }}"; 60 const char *x2 = "hello world"; 61 /* test 3 */ 62 const char *t3 = "hello {{ v3.x }}"; 63 const char *x3 = "hello baz"; 64 /* test 4 */ 65 const char *t4 = "hello {{# v2 }}{{ . }}{{/ v2 }}"; 66 const char *x4 = "hello foobar"; 67 /* test 5 */ 68 const char *t5 = "hello {{# v3 }}{{ y }}/{{ x }}{{ z }}{{/ v3 }}"; 69 const char *x5 = "hello quux/baz"; 70 /* test 8 */ 71 const char *t8 = "{{^ v4 }}fallback{{/ v4 }}"; 72 const char *x8 = "fallback"; 73 74 (void) argc; 75 (void) argv; 76 GNUNET_log_setup ("test-mustach-jansson", 77 "INFO", 78 NULL); 79 GNUNET_assert (NULL != root); 80 GNUNET_assert (NULL != arr); 81 GNUNET_assert (NULL != obj); 82 GNUNET_assert (0 == 83 json_object_set_new (root, 84 "v1", 85 json_string ("world"))); 86 GNUNET_assert (0 == 87 json_object_set_new (root, 88 "v4", 89 json_array ())); 90 GNUNET_assert (0 == 91 json_array_append_new (arr, 92 json_string ("foo"))); 93 GNUNET_assert (0 == 94 json_array_append_new (arr, 95 json_string ("bar"))); 96 GNUNET_assert (0 == 97 json_object_set_new (root, 98 "v2", 99 arr)); 100 GNUNET_assert (0 == 101 json_object_set_new (root, 102 "v3", 103 obj)); 104 GNUNET_assert (0 == 105 json_object_set_new (root, 106 "amt", 107 json_string ("EUR:123.00"))); 108 GNUNET_assert (0 == 109 json_object_set_new (obj, 110 "x", 111 json_string ("baz"))); 112 GNUNET_assert (0 == 113 json_object_set_new (obj, 114 "y", 115 json_string ("quux"))); 116 assert_template (t1, root, x1); 117 assert_template (t2, root, x2); 118 assert_template (t3, root, x3); 119 assert_template (t4, root, x4); 120 assert_template (t5, root, x5); 121 assert_template (t8, root, x8); 122 json_decref (root); 123 return 0; 124 }