]> git.0d.be Git - empathy.git/blob - tests/empathy-parser-test.c
Updated Galician translations
[empathy.git] / tests / empathy-parser-test.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "test-helper.h"
6
7 #include <telepathy-glib/util.h>
8
9 #define DEBUG_FLAG EMPATHY_DEBUG_TESTS
10 #include <libempathy/empathy-debug.h>
11
12 #include <libempathy-gtk/empathy-string-parser.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
48       /* They are not links! */
49       "http://", "http[:/]/", /* Hm... */
50       "www.", "www.",
51       "w.foo.com", "w.foo.com",
52       "@server.com", "@server.com",
53       "mailto:user@", "mailto:user@",
54       "mailto:user@.com", "mailto:user@.com",
55       "user@.com", "user@.com",
56
57       /* Links inside (), {}, [], <> or "" */
58       /* FIXME: How to test if the ending ] is matched or not? */
59       "Foo (www.foo.com)", "Foo ([www.foo.com])",
60       "Foo {www.foo.com}", "Foo {[www.foo.com]}",
61       "Foo [www.foo.com]", "Foo [[www.foo.com]]",
62       "Foo <www.foo.com>", "Foo &lt;[www.foo.com]&gt;",
63       "Foo \"www.foo.com\"", "Foo &quot;[www.foo.com]&quot;",
64       "Foo (www.foo.com/bar(123)baz)", "Foo ([www.foo.com/bar(123)baz])",
65       "<a href=\"http://foo.com\">bar</a>", "&lt;a href=&quot;[http://foo.com]&quot;&gt;bar&lt;/a&gt;",
66       "Foo (user@server.com)", "Foo ([user@server.com])",
67       "Foo {user@server.com}", "Foo {[user@server.com]}",
68       "Foo [user@server.com]", "Foo [[user@server.com]]",
69       "Foo <user@server.com>", "Foo &lt;[user@server.com]&gt;",
70       "Foo \"user@server.com\"", "Foo &quot;[user@server.com]&quot;",
71
72       /* Basic smileys */
73       "a:)b", "a[:)]b",
74       ">:)", "[>:)]",
75       ">:(", "&gt;[:(]",
76
77       /* Smileys and links mixed */
78       ":)http://foo.com", "[:)][http://foo.com]",
79       "a :) b http://foo.com c :( d www.test.com e", "a [:)] b [http://foo.com] c [:(] d [www.test.com] e",
80
81       /* '\r' should be stripped */
82       "badger\n\rmushroom", "badger\nmushroom",
83       "badger\r\nmushroom", "badger\nmushroom",
84
85       /* FIXME: Known issue: Brackets should be counted by the parser */
86       //"Foo www.bar.com/test(123)", "Foo [www.bar.com/test(123)]",
87       //"Foo (www.bar.com/test(123))", "Foo ([www.bar.com/test(123)])",
88       //"Foo www.bar.com/test{123}", "Foo [www.bar.com/test{123}]",
89       //"Foo (:))", "Foo ([:)])",
90       //"Foo <a href=\"http://foo.com\">:)</a>", "Foo <a href=\"[http://foo.com]\">[:)]</a>",
91
92       NULL, NULL
93     };
94   EmpathyStringParser parsers[] =
95     {
96       {empathy_string_match_link, test_replace_match},
97       {empathy_string_match_smiley, test_replace_match},
98       {empathy_string_match_all, empathy_string_replace_escaped},
99       {NULL, NULL}
100     };
101   guint i;
102
103   DEBUG ("Started");
104   for (i = 0; tests[i] != NULL; i += 2)
105     {
106       GString *string;
107       gboolean ok;
108
109       string = g_string_new (NULL);
110       empathy_string_parser_substr (tests[i], -1, parsers, string);
111
112       ok = !tp_strdiff (tests[i + 1], string->str);
113       DEBUG ("'%s' => '%s': %s", tests[i], string->str, ok ? "OK" : "FAILED");
114       g_assert (ok);
115
116       g_string_free (string, TRUE);
117     }
118 }
119
120 int
121 main (int argc,
122     char **argv)
123 {
124   int result;
125
126   test_init (argc, argv);
127
128   g_test_add_func ("/parsers", test_parsers);
129
130   result = g_test_run ();
131   test_deinit ();
132
133   return result;
134 }