]> git.0d.be Git - empathy.git/blob - tests/empathy-parser-test.c
Updated Polish translation
[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_replace_verbatim (const gchar *text,
29                        gssize len,
30                        gpointer match_data,
31                        gpointer user_data)
32 {
33   GString *string = user_data;
34
35   g_string_append_len (string, text, len);
36 }
37
38 static void
39 test_parsers (void)
40 {
41   gchar *tests[] =
42     {
43       /* Basic link matches */
44       "http://foo.com", "[http://foo.com]",
45       "http://foo.com\nhttp://bar.com", "[http://foo.com]\n[http://bar.com]",
46       "http://foo.com/test?id=bar?", "[http://foo.com/test?id=bar]?",
47       "git://foo.com", "[git://foo.com]",
48       "git+ssh://foo.com", "[git+ssh://foo.com]",
49       "mailto:user@server.com", "[mailto:user@server.com]",
50       "www.foo.com", "[www.foo.com]",
51       "ftp.foo.com", "[ftp.foo.com]",
52       "user@server.com", "[user@server.com]",
53       "first.last@server.com", "[first.last@server.com]",
54       "http://foo.com. bar", "[http://foo.com]. bar",
55       "http://foo.com; bar", "[http://foo.com]; bar",
56       "http://foo.com: bar", "[http://foo.com]: bar",
57       "http://foo.com:bar", "[http://foo.com:bar]",
58
59       /* They are not links! */
60       "http://", "http[:/]/", /* Hm... */
61       "www.", "www.",
62       "w.foo.com", "w.foo.com",
63       "@server.com", "@server.com",
64       "mailto:user@", "mailto:user@",
65       "mailto:user@.com", "mailto:user@.com",
66       "user@.com", "user@.com",
67
68       /* Links inside (), {}, [], <> or "" */
69       /* FIXME: How to test if the ending ] is matched or not? */
70       "Foo (www.foo.com)", "Foo ([www.foo.com])",
71       "Foo {www.foo.com}", "Foo {[www.foo.com]}",
72       "Foo [www.foo.com]", "Foo [[www.foo.com]]",
73       "Foo <www.foo.com>", "Foo <[www.foo.com]>",
74       "Foo \"www.foo.com\"", "Foo \"[www.foo.com]\"",
75       "Foo (www.foo.com/bar(123)baz)", "Foo ([www.foo.com/bar(123)baz])",
76       "<a href=\"http://foo.com\">bar</a>", "<a href=\"[http://foo.com]\">bar</a>",
77       "Foo (user@server.com)", "Foo ([user@server.com])",
78       "Foo {user@server.com}", "Foo {[user@server.com]}",
79       "Foo [user@server.com]", "Foo [[user@server.com]]",
80       "Foo <user@server.com>", "Foo <[user@server.com]>",
81       "Foo \"user@server.com\"", "Foo \"[user@server.com]\"",
82
83       /* Basic smileys */
84       "a:)b", "a[:)]b",
85       ">:)", "[>:)]",
86       ">:(", ">[:(]",
87
88       /* Smileys and links mixed */
89       ":)http://foo.com", "[:)][http://foo.com]",
90       "a :) b http://foo.com c :( d www.test.com e", "a [:)] b [http://foo.com] c [:(] d [www.test.com] e",
91
92       /* FIXME: Known issue: Brackets should be counted by the parser */
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 www.bar.com/test{123}", "Foo [www.bar.com/test{123}]",
96       //"Foo (:))", "Foo ([:)])",
97       //"Foo <a href=\"http://foo.com\">:)</a>", "Foo <a href=\"[http://foo.com]\">[:)]</a>",
98
99       NULL, NULL
100     };
101   EmpathyStringParser parsers[] =
102     {
103       {empathy_string_match_link, test_replace_match},
104       {empathy_string_match_smiley, test_replace_match},
105       {empathy_string_match_all, test_replace_verbatim},
106       {NULL, NULL}
107     };
108   guint i;
109   gboolean failed = FALSE;
110
111   DEBUG ("Started");
112   for (i = 0; tests[i] != NULL; i += 2)
113     {
114       GString *string;
115       gboolean ok;
116
117       string = g_string_new (NULL);
118       empathy_string_parser_substr (tests[i], -1, parsers, string);
119
120       ok = !tp_strdiff (tests[i + 1], string->str);
121       DEBUG ("'%s' => '%s': %s", tests[i], string->str, ok ? "OK" : "FAILED");
122       if (!ok)
123         failed = TRUE;
124
125       g_string_free (string, TRUE);
126     }
127
128   g_assert (!failed);
129 }
130
131 int
132 main (int argc,
133     char **argv)
134 {
135   int result;
136
137   test_init (argc, argv);
138
139   g_test_add_func ("/parsers", test_parsers);
140
141   result = g_test_run ();
142   test_deinit ();
143
144   return result;
145 }