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