exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

test_mustach_jansson.c (4086B)


      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 "taler/platform.h"
     26 #include "mustach-jansson.h"
     27 #include <gnunet/gnunet_util_lib.h>
     28 
     29 static void
     30 assert_template (const char *template,
     31                  json_t *root,
     32                  const char *expected)
     33 {
     34   char *r;
     35   size_t sz;
     36 
     37   GNUNET_assert (0 == mustach_jansson_mem (template,
     38                                            0,
     39                                            root,
     40                                            Mustach_With_AllExtensions,
     41                                            &r,
     42                                            &sz));
     43   GNUNET_assert (0 == strcmp (r,
     44                               expected));
     45   GNUNET_free (r);
     46 }
     47 
     48 
     49 int
     50 main (int argc,
     51       char *const *argv)
     52 {
     53   json_t *root = json_object ();
     54   json_t *arr = json_array ();
     55   json_t *obj = json_object ();
     56   /* test 1 */
     57   const char *t1 = "hello world";
     58   const char *x1 = "hello world";
     59   /* test 2 */
     60   const char *t2 = "hello {{ v1 }}";
     61   const char *x2 = "hello world";
     62   /* test 3 */
     63   const char *t3 = "hello {{ v3.x }}";
     64   const char *x3 = "hello baz";
     65   /* test 4 */
     66   const char *t4 = "hello {{# v2 }}{{ . }}{{/ v2 }}";
     67   const char *x4 = "hello foobar";
     68   /* test 5 */
     69   const char *t5 = "hello {{# v3 }}{{ y }}/{{ x }}{{ z }}{{/ v3 }}";
     70   const char *x5 = "hello quux/baz";
     71   /* test 8 */
     72   const char *t8 = "{{^ v4 }}fallback{{/ v4 }}";
     73   const char *x8 = "fallback";
     74 
     75   (void) argc;
     76   (void) argv;
     77   GNUNET_log_setup ("test-mustach-jansson",
     78                     "INFO",
     79                     NULL);
     80   GNUNET_assert (NULL != root);
     81   GNUNET_assert (NULL != arr);
     82   GNUNET_assert (NULL != obj);
     83   GNUNET_assert (0 ==
     84                  json_object_set_new (root,
     85                                       "v1",
     86                                       json_string ("world")));
     87   GNUNET_assert (0 ==
     88                  json_object_set_new (root,
     89                                       "v4",
     90                                       json_array ()));
     91   GNUNET_assert (0 ==
     92                  json_array_append_new (arr,
     93                                         json_string ("foo")));
     94   GNUNET_assert (0 ==
     95                  json_array_append_new (arr,
     96                                         json_string ("bar")));
     97   GNUNET_assert (0 ==
     98                  json_object_set_new (root,
     99                                       "v2",
    100                                       arr));
    101   GNUNET_assert (0 ==
    102                  json_object_set_new (root,
    103                                       "v3",
    104                                       obj));
    105   GNUNET_assert (0 ==
    106                  json_object_set_new (root,
    107                                       "amt",
    108                                       json_string ("EUR:123.00")));
    109   GNUNET_assert (0 ==
    110                  json_object_set_new (obj,
    111                                       "x",
    112                                       json_string ("baz")));
    113   GNUNET_assert (0 ==
    114                  json_object_set_new (obj,
    115                                       "y",
    116                                       json_string ("quux")));
    117   assert_template (t1, root, x1);
    118   assert_template (t2, root, x2);
    119   assert_template (t3, root, x3);
    120   assert_template (t4, root, x4);
    121   assert_template (t5, root, x5);
    122   assert_template (t8, root, x8);
    123   json_decref (root);
    124   return 0;
    125 }