]> git.0d.be Git - empathy.git/blob - tests/empathy-parser-test.c
e9983c7f873ec95d21f38a34b387e2a87ffbc092
[empathy.git] / tests / empathy-parser-test.c
1 #include "config.h"
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #include "test-helper.h"
8
9 #include <telepathy-glib/telepathy-glib.h>
10
11 #define DEBUG_FLAG EMPATHY_DEBUG_TESTS
12 #include "libempathy/empathy-debug.h"
13
14 #include "libempathy-gtk/empathy-string-parser.h"
15
16 static void
17 test_replace_match (const gchar *text,
18                     gssize len,
19                     gpointer match_data,
20                     gpointer user_data)
21 {
22   GString *string = user_data;
23
24   g_string_append_c (string, '[');
25   g_string_append_len (string, text, len);
26   g_string_append_c (string, ']');
27 }
28
29 static void
30 test_parsers (void)
31 {
32   gchar *tests[] =
33     {
34       /* Basic link matches */
35       "http://foo.com", "[http://foo.com]",
36       "http://foo.com\nhttp://bar.com", "[http://foo.com]\n[http://bar.com]",
37       "http://foo.com/test?id=bar?", "[http://foo.com/test?id=bar]?",
38       "git://foo.com", "[git://foo.com]",
39       "git+ssh://foo.com", "[git+ssh://foo.com]",
40       "mailto:user@server.com", "[mailto:user@server.com]",
41       "www.foo.com", "[www.foo.com]",
42       "ftp.foo.com", "[ftp.foo.com]",
43       "user@server.com", "[user@server.com]",
44       "first.last@server.com", "[first.last@server.com]",
45       "http://foo.com. bar", "[http://foo.com]. bar",
46       "http://foo.com; bar", "[http://foo.com]; bar",
47       "http://foo.com: bar", "[http://foo.com]: bar",
48       "http://foo.com:bar", "[http://foo.com:bar]",
49       "http://apos'foo.com", "[http://apos'foo.com]",
50       "mailto:bar'?user@server.com", "[mailto:bar'?user@server.com]",
51
52       /* They are not links! */
53       "http://", "http[:/]/", /* Hm... */
54       "www.", "www.",
55       "w.foo.com", "w.foo.com",
56       "@server.com", "@server.com",
57       "mailto:user@", "mailto:user@",
58       "mailto:user@.com", "mailto:user@.com",
59       "user@.com", "user@.com",
60
61       /* Links inside (), {}, [], <>, "" or '' */
62       /* FIXME: How to test if the ending ] is matched or not? */
63       "Foo (www.foo.com)", "Foo ([www.foo.com])",
64       "Foo {www.foo.com}", "Foo {[www.foo.com]}",
65       "Foo [www.foo.com]", "Foo [[www.foo.com]]",
66       "Foo <www.foo.com>", "Foo &lt;[www.foo.com]&gt;",
67       "Foo \"www.foo.com\"", "Foo &quot;[www.foo.com]&quot;",
68       "Foo (www.foo.com/bar(123)baz)", "Foo ([www.foo.com/bar(123)baz])",
69       "<a href=\"http://foo.com\">bar</a>", "&lt;a href=&quot;[http://foo.com]&quot;&gt;bar&lt;/a&gt;",
70       "Foo (user@server.com)", "Foo ([user@server.com])",
71       "Foo {user@server.com}", "Foo {[user@server.com]}",
72       "Foo [user@server.com]", "Foo [[user@server.com]]",
73       "Foo <user@server.com>", "Foo &lt;[user@server.com]&gt;",
74       "Foo \"user@server.com\"", "Foo &quot;[user@server.com]&quot;",
75       "<a href='http://apos'foo.com'>bar</a>", "&lt;a href=&apos;[http://apos'foo.com]&apos;&gt;bar&lt;/a&gt;",
76       "Foo 'bar'?user@server.com'", "Foo &apos;[bar'?user@server.com]&apos;",
77
78       /* Basic smileys */
79       "a:)b", "a[:)]b",
80       ">:)", "[>:)]",
81       ">:(", "&gt;[:(]",
82
83       /* Smileys and links mixed */
84       ":)http://foo.com", "[:)][http://foo.com]",
85       "a :) b http://foo.com c :( d www.test.com e", "a [:)] b [http://foo.com] c [:(] d [www.test.com] e",
86
87       /* '\r' should be stripped */
88       "badger\n\rmushroom", "badger\nmushroom",
89       "badger\r\nmushroom", "badger\nmushroom",
90
91       /* FIXME: Known issue: Brackets should be counted by the parser */
92       //"Foo www.bar.com/test(123)", "Foo [www.bar.com/test(123)]",
93       //"Foo (www.bar.com/test(123))", "Foo ([www.bar.com/test(123)])",
94       //"Foo www.bar.com/test{123}", "Foo [www.bar.com/test{123}]",
95       //"Foo (:))", "Foo ([:)])",
96       //"Foo <a href=\"http://foo.com\">:)</a>", "Foo <a href=\"[http://foo.com]\">[:)]</a>",
97
98       NULL, NULL
99     };
100   EmpathyStringParser parsers[] =
101     {
102       {empathy_string_match_link, test_replace_match},
103       {empathy_string_match_smiley, test_replace_match},
104       {empathy_string_match_all, empathy_string_replace_escaped},
105       {NULL, NULL}
106     };
107   guint i;
108
109   DEBUG ("Started");
110   for (i = 0; tests[i] != NULL; i += 2)
111     {
112       GString *string;
113       gboolean ok;
114
115       string = g_string_new (NULL);
116       empathy_string_parser_substr (tests[i], -1, parsers, string);
117
118       ok = !tp_strdiff (tests[i + 1], string->str);
119       DEBUG ("'%s' => '%s': %s", tests[i], string->str, ok ? "OK" : "FAILED");
120       g_assert (ok);
121
122       g_string_free (string, TRUE);
123     }
124 }
125
126 int
127 main (int argc,
128     char **argv)
129 {
130   int result;
131
132   test_init (argc, argv);
133
134   g_test_add_func ("/parsers", test_parsers);
135
136   result = g_test_run ();
137   test_deinit ();
138
139   return result;
140 }