aboutsummaryrefslogtreecommitdiffstats
path: root/tests/empathy-parser-test.c
blob: 6c549f90563af0ad3bab6b1ca4cc4b2b61cda641 (plain) (blame)
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
135
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "test-helper.h"

#define DEBUG_FLAG EMPATHY_DEBUG_TESTS
#include <libempathy/empathy-debug.h>

#include <libempathy-gtk/empathy-ui-utils.h>

static void
test_replace_match (const gchar *text,
                    gssize len,
                    gpointer match_data,
                    gpointer user_data)
{
  GString *string = user_data;

  g_string_append_c (string, '[');
  g_string_append_len (string, text, len);
  g_string_append_c (string, ']');
}

static void
test_replace_verbatim (const gchar *text,
                       gssize len,
                       gpointer match_data,
                       gpointer user_data)
{
  GString *string = user_data;

  g_string_append_len (string, text, len);
}

static void
test_parsers (void)
{
  guint i;
  gchar *tests[] =
    {
      /* Basic link matches */
      "http://foo.com", "[http://foo.com]",
      "git://foo.com", "[git://foo.com]",
      "git+ssh://foo.com", "[git+ssh://foo.com]",
      "mailto:user@server.com", "[mailto:user@server.com]",
      "www.foo.com", "[www.foo.com]",
      "ftp.foo.com", "[ftp.foo.com]",
      "user@server.com", "[user@server.com]",
      "http://foo.com. bar", "[http://foo.com]. bar",
      "http://foo.com; bar", "[http://foo.com]; bar",
      "http://foo.com: bar", "[http://foo.com]: bar",
      "http://foo.com:bar", "[http://foo.com:bar]",

      /* They are not links! */
      "http://", "http[:/]/", /* Hm... */
      "www.", "www.",
      "w.foo.com", "w.foo.com",
      "@server.com", "@server.com",
      "mailto:user@", "mailto:user@",
      "mailto:user@.com", "mailto:user@.com",
      "user@.com", "user@.com",

      /* Links inside (), {}, [], <> or "" */
      /* FIXME: How to test if the ending ] is matched or not? */
      "Foo (www.foo.com)", "Foo ([www.foo.com])",
      "Foo {www.foo.com}", "Foo {[www.foo.com]}",
      "Foo [www.foo.com]", "Foo [[www.foo.com]]",
      "Foo <www.foo.com>", "Foo <[www.foo.com]>",
      "Foo \"www.foo.com\"", "Foo \"[www.foo.com]\"",
      "Foo (www.foo.com/bar(123)baz)", "Foo ([www.foo.com/bar(123)baz])",
      "<a href=\"http://foo.com\">bar</a>", "<a href=\"[http://foo.com]\">bar</a>",
      /* FIXME; Known issue: With email addresses, any leading character is matched */
      //"Foo (user@server.com)", "Foo ([user@server.com])",
      //"Foo {user@server.com}", "Foo {[user@server.com]}",
      //"Foo [user@server.com]", "Foo [[user@server.com]]",
      //"Foo <user@server.com>", "Foo <[user@server.com]>",
      "Foo \"user@server.com\"", "Foo \"[user@server.com]\"",

      /* Basic smileys */
      "a:)b", "a[:)]b",
      ">:)", "[>:)]",
      ">:(", ">[:(]",

      /* Smileys and links mixed */
      ":)http://foo.com", "[:)][http://foo.com]",
      "a :) b http://foo.com c :( d www.test.com e", "a [:)] b [http://foo.com] c [:(] d [www.test.com] e",

      /* FIXME: Known issue: Brackets should be counted by the parser */
      //"Foo www.bar.com/test(123)", "Foo [www.bar.com/test(123)]",
      //"Foo (www.bar.com/test(123))", "Foo ([www.bar.com/test(123)])",
      //"Foo www.bar.com/test{123}", "Foo [www.bar.com/test{123}]",
      //"Foo (:))", "Foo ([:)])",
      //"Foo <a href=\"http://foo.com\">:)</a>", "Foo <a href=\"[http://foo.com]\">[:)]</a>",

      NULL, NULL
    };
  EmpathyStringParser parsers[] =
    {
      {empathy_string_match_link, test_replace_match},
      {empathy_string_match_smiley, test_replace_match},
      {empathy_string_match_all, test_replace_verbatim},
      {NULL, NULL}
    };

  DEBUG ("Started");
  for (i = 0; tests[i] != NULL; i += 2)
    {
      GString *string;

      string = g_string_new (NULL);
      empathy_string_parser_substr (tests[i], -1, parsers, string);

      DEBUG ("'%s' => '%s'", tests[i], string->str);
      g_assert_cmpstr (tests[i + 1], ==, string->str);

      g_string_free (string, TRUE);
    }
}

int
main (int argc,
    char **argv)
{
  int result;

  test_init (argc, argv);

  g_test_add_func ("/parsers", test_parsers);

  result = g_test_run ();
  test_deinit ();

  return result;
}