summaryrefslogtreecommitdiff
path: root/tests/testserialize.c
blob: cc6c67bc4a1d6f1a31240906e25f9bb3019cb1b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Pango
 *
 * Copyright (C) 2021 Matthias Clasen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include <glib.h>
#include <pango/pangocairo.h>

static void
test_serialize_attr_list (void)
{
  const char *valid[] = {
    "5 16 style italic",
    "0 10 foreground red, 5 15 weight bold, 0 200 font-desc \"Sans Small-Caps 10\"",
    "0 10 foreground red\n5 15 weight bold\n0 200 font-desc \"Sans Small-Caps 10\"",
    "  0   10   fallback  false,\n 5 15 weight semilight\n\n \n \n",
    "0 100 font-desc \"Cantarell, Sans, Italic Ultra-Light 64\", 10 11 weight 100",
    "0 -1 size 10",
    "0 1 weight 700, 2 4 weight book",
    "0 200 rise 100\n5 15 family Times\n10 11 size 10240\n11 100 fallback 0\n30 60 stretch 2\n",
    ""
  };
  const char *roundtripped[] = {
    "5 16 style italic",
    "0 10 foreground #ffff00000000\n5 15 weight bold\n0 200 font-desc \"Sans Small-Caps 10\"",
    "0 10 foreground #ffff00000000\n5 15 weight bold\n0 200 font-desc \"Sans Small-Caps 10\"",
    "0 10 fallback false\n5 15 weight semilight",
    "0 100 font-desc \"Cantarell,Sans Ultra-Light Italic 64\"\n10 11 weight thin",
    "0 4294967295 size 10",
    "0 1 weight bold\n2 4 weight book",
    "0 200 rise 100\n5 15 family Times\n10 11 size 10240\n11 100 fallback false\n30 60 stretch condensed",
    ""
  };
  const char *invalid[] = {
    "not an attr list",
    "0 -1 FOREGROUND xyz",
    ",,bla.wewq",
  };

  for (int i = 0; i < G_N_ELEMENTS (valid); i++)
    {
      PangoAttrList *attrs;
      char *str;

      attrs = pango_attr_list_from_string (valid[i]);
      g_assert_nonnull (attrs);
      str = pango_attr_list_to_string (attrs);
      g_assert_cmpstr (str, ==, roundtripped[i]);
      g_free (str);
      pango_attr_list_unref (attrs);
    }

  for (int i = 0; i < G_N_ELEMENTS (invalid); i++)
    {
      PangoAttrList *attrs;

      attrs = pango_attr_list_from_string (invalid[i]);
      g_assert_null (attrs);
    }
}

static void
test_serialize_tab_array (void)
{
  const char *valid[] = {
    "0 10 100 200 400",
    "0px 10px 100px 200px 400px",
    "  0    10   ",
    "20 10",
    ""
  };
  const char *roundtripped[] = {
    "0 10 100 200 400",
    "0px 10px 100px 200px 400px",
    "0 10",
    "20 10",
    ""
  };
  const char *invalid[] = {
    "not a tabarray",
    "-10\n-20",
    "10ps 20pu",
    "10, 20",
    "10 20px 30",
  };

  for (int i = 0; i < G_N_ELEMENTS (valid); i++)
    {
      PangoTabArray *tabs;
      char *str;

      tabs = pango_tab_array_from_string (valid[i]);
      g_assert_nonnull (tabs);
      str = pango_tab_array_to_string (tabs);
      g_assert_cmpstr (str, ==, roundtripped[i]);
      g_free (str);
      pango_tab_array_free (tabs);
    }

  for (int i = 0; i < G_N_ELEMENTS (invalid); i++)
    {
      PangoTabArray *tabs;

      tabs = pango_tab_array_from_string (invalid[i]);
      g_assert_null (tabs);
    }
}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/serialize/attr-list", test_serialize_attr_list);
  g_test_add_func ("/serialize/tab-array", test_serialize_tab_array);

  return g_test_run ();
}