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