1
#include <glib.h>
2
#include <libipuz/libipuz.h>
3
#include <locale.h>
4

            
5
typedef struct {
6
  IPuzStyleMark location;
7
  char *label;
8
} Mark;
9

            
10
static void
11
1
collect_mark_cb (IPuzStyleMark  location,
12
                 const gchar   *label,
13
                 gpointer       user_data)
14
{
15
1
  GPtrArray *result = user_data;
16
1
  Mark *mark = g_new0 (Mark, 1);
17

            
18
1
  mark->location = location;
19
1
  mark->label = g_strdup (label);
20
1
  g_ptr_array_add (result, mark);
21
1
}
22

            
23
static void
24
1
style_parse (void)
25
{
26
2
  g_autoptr (JsonParser) parser = json_parser_new ();
27
1
  GError *error = NULL;
28
  static const char *data = 
29
    "{"
30
    "  \"named\": \"delimiter\","
31
    "  \"border\": 42,"
32
    "  \"shapebg\": \"circle\","
33
    "  \"highlight\": true,"
34
    "  \"divided\": \"-\","
35
    "  \"mark\": { \"TL\": \"blah\" },"
36
    "  \"label\": \"foo\","
37
    "  \"image\": \"http://example.com/image\","
38
    "  \"imagebg\": \"http://example.com/imagebg\","
39
    "  \"color\": \"#112233\","
40
    "  \"colortext\": \"#445566\","
41
    "  \"colorborder\": \"#778899\","
42
    "  \"barred\": \"T\","
43
    "  \"dotted\": \"R\","
44
    "  \"dashed\": \"B\","
45
    "  \"lessthan\": \"L\","
46
    "  \"greaterthan\": \"TR\","
47
    "  \"equal\": \"BL\""
48
    "}";
49

            
50
1
  gboolean parsed = json_parser_load_from_data (parser, data, strlen (data), &error);
51
1
  g_assert_no_error (error);
52
1
  g_assert_true (parsed);
53

            
54
1
  JsonNode *root = json_parser_get_root (parser);
55
2
  g_autoptr (IPuzStyle) style = ipuz_style_new_from_json (root);
56
1
  g_assert (style != NULL);
57

            
58
1
  g_assert_cmpstr (ipuz_style_get_named (style), ==, "delimiter");
59
1
  g_assert_cmpint (ipuz_style_get_border (style), ==, 42);
60
1
  g_assert_cmpint (ipuz_style_get_shapebg (style), ==, IPUZ_STYLE_SHAPE_CIRCLE);
61
1
  g_assert_true (ipuz_style_get_highlight (style));
62
1
  g_assert_cmpint (ipuz_style_get_divided (style), ==, IPUZ_STYLE_DIVIDED_HORIZ);
63

            
64
1
  GPtrArray *result = g_ptr_array_new ();
65
1
  ipuz_style_mark_foreach (style, collect_mark_cb, result);
66
1
  g_assert_cmpint (result->len, ==, 1);
67
1
  Mark *mark = g_ptr_array_index (result, 0);
68
1
  g_assert_cmpint (mark->location, ==, IPUZ_STYLE_MARK_TL);
69
1
  g_assert_cmpstr (mark->label, ==, "blah");
70
1
  g_free (mark->label);
71
1
  g_free (mark);
72
1
  g_ptr_array_free (result, TRUE);
73

            
74
1
  g_assert_cmpstr (ipuz_style_get_label (style), ==, "foo");
75
1
  g_assert_cmpstr (ipuz_style_get_image_url (style), ==, "http://example.com/image");
76
1
  g_assert_cmpstr (ipuz_style_get_imagebg_url (style), ==, "http://example.com/imagebg");
77
1
  g_assert_cmpstr (ipuz_style_get_bg_color (style), ==, "#112233");
78
1
  g_assert_cmpstr (ipuz_style_get_text_color (style), ==, "#445566");
79
1
  g_assert_cmpstr (ipuz_style_get_border_color (style), ==, "#778899");
80

            
81
1
  g_assert_cmpint (ipuz_style_get_barred (style), ==, IPUZ_STYLE_SIDES_TOP);
82
1
  g_assert_cmpint (ipuz_style_get_dotted (style), ==, IPUZ_STYLE_SIDES_RIGHT);
83
1
  g_assert_cmpint (ipuz_style_get_dashed (style), ==, IPUZ_STYLE_SIDES_BOTTOM);
84
1
  g_assert_cmpint (ipuz_style_get_lessthan (style), ==, IPUZ_STYLE_SIDES_LEFT);
85
1
  g_assert_cmpint (ipuz_style_get_greaterthan (style), ==,
86
                   IPUZ_STYLE_SIDES_TOP | IPUZ_STYLE_SIDES_RIGHT);
87
1
  g_assert_cmpint (ipuz_style_get_equal (style), ==,
88
                   IPUZ_STYLE_SIDES_BOTTOM | IPUZ_STYLE_SIDES_LEFT);
89
1
}
90

            
91
int
92
1
main (int argc, char **argv)
93
{
94
1
  g_test_init (&argc, &argv, NULL);
95

            
96
1
  g_test_add_func ("/style/parse", style_parse);
97

            
98
1
  return g_test_run ();
99
}