Port empathy-call to GtkApplication
[empathy-mirror.git] / tests / empathy-parser-test.c
bloba79a7a2875c5e2b0a0bd89250a9c2155e30aa37b
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
5 #include "test-helper.h"
7 #include <telepathy-glib/util.h>
9 #define DEBUG_FLAG EMPATHY_DEBUG_TESTS
10 #include <libempathy/empathy-debug.h>
12 #include <libempathy-gtk/empathy-string-parser.h>
14 static void
15 test_replace_match (const gchar *text,
16 gssize len,
17 gpointer match_data,
18 gpointer user_data)
20 GString *string = user_data;
22 g_string_append_c (string, '[');
23 g_string_append_len (string, text, len);
24 g_string_append_c (string, ']');
27 static void
28 test_parsers (void)
30 gchar *tests[] =
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]",
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",
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;",
76 /* Basic smileys */
77 "a:)b", "a[:)]b",
78 ">:)", "[>:)]",
79 ">:(", "&gt;[:(]",
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",
85 /* '\r' should be stripped */
86 "badger\n\rmushroom", "badger\nmushroom",
87 "badger\r\nmushroom", "badger\nmushroom",
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>",
96 NULL, NULL
98 EmpathyStringParser parsers[] =
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}
105 guint i;
107 DEBUG ("Started");
108 for (i = 0; tests[i] != NULL; i += 2)
110 GString *string;
111 gboolean ok;
113 string = g_string_new (NULL);
114 empathy_string_parser_substr (tests[i], -1, parsers, string);
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);
120 g_string_free (string, TRUE);
125 main (int argc,
126 char **argv)
128 int result;
130 test_init (argc, argv);
132 g_test_add_func ("/parsers", test_parsers);
134 result = g_test_run ();
135 test_deinit ();
137 return result;