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