* add xdgmime from freedesktop.org
[citadel.git] / libcitadel / lib / xdgmime / test-mime.c
1 /* 
2  * Copyright (C) 2003,2004  Red Hat, Inc.
3  * Copyright (C) 2003,2004  Jonathan Blandford <jrb@alum.mit.edu>
4  *
5  * Licensed under the Academic Free License version 2.0
6  * Or under the following terms:
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 #include "xdgmime.h"
24 #include "xdgmimeglob.h"
25 #include <string.h>
26 #include <stdio.h>
27
28
29 static void
30 test_individual_glob (const char  *glob,
31                       XdgGlobType  expected_type)
32 {
33   XdgGlobType test_type;
34
35   test_type = _xdg_glob_determine_type (glob);
36   if (test_type != expected_type)
37     {
38       printf ("Test Failed: %s is of type %s, but %s is expected\n",
39               glob,
40               ((test_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL":
41                ((test_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_FULL")),
42               ((expected_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL":
43                ((expected_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_COMPLEX")));
44     }
45 }
46
47 static void
48 test_glob_type (void)
49 {
50   test_individual_glob ("*.gif", XDG_GLOB_SIMPLE);
51   test_individual_glob ("Foo*.gif", XDG_GLOB_FULL);
52   test_individual_glob ("*[4].gif", XDG_GLOB_FULL);
53   test_individual_glob ("Makefile", XDG_GLOB_LITERAL);
54   test_individual_glob ("sldkfjvlsdf\\\\slkdjf", XDG_GLOB_FULL);
55   test_individual_glob ("tree.[ch]", XDG_GLOB_FULL);
56 }
57
58 static void
59 test_alias (const char *mime_a,
60             const char *mime_b,
61             int         expected)
62 {
63   int actual;
64
65   actual = xdg_mime_mime_type_equal (mime_a, mime_b);
66
67   if (actual != expected)
68     {
69       printf ("Test Failed: %s is %s to %s\n", 
70               mime_a, actual ? "equal" : "not equal", mime_b);
71     }
72 }
73
74 static void
75 test_aliasing (void)
76 {
77   test_alias ("application/wordperfect", "application/vnd.wordperfect", 1);
78   test_alias ("application/x-gnome-app-info", "application/x-desktop", 1);
79   test_alias ("application/x-wordperfect", "application/vnd.wordperfect", 1);
80   test_alias ("application/x-wordperfect", "audio/x-midi", 0);
81   test_alias ("/", "vnd/vnd", 0);
82   test_alias ("application/octet-stream", "text/plain", 0);
83   test_alias ("text/plain", "text/*", 0);
84 }
85
86 static void
87 test_subclass (const char *mime_a,
88                const char *mime_b,
89                int         expected)
90 {
91   int actual;
92
93   actual = xdg_mime_mime_type_subclass (mime_a, mime_b);
94
95   if (actual != expected)
96     {
97       printf ("Test Failed: %s is %s of %s\n", 
98               mime_a, actual ? "subclass" : "not subclass", mime_b);
99     }
100 }
101
102 static void
103 test_subclassing (void)
104 {
105   test_subclass ("application/rtf", "text/plain", 1);
106   test_subclass ("message/news", "text/plain", 1);
107   test_subclass ("message/news", "message/*", 1);
108   test_subclass ("message/news", "text/*", 1);
109   test_subclass ("message/news", "application/octet-stream", 1);
110   test_subclass ("application/rtf", "application/octet-stream", 1);
111   test_subclass ("application/x-gnome-app-info", "text/plain", 1);
112   test_subclass ("image/x-djvu", "image/vnd.djvu", 1);
113   test_subclass ("image/vnd.djvu", "image/x-djvu", 1);
114   test_subclass ("image/vnd.djvu", "text/plain", 0);
115   test_subclass ("image/vnd.djvu", "text/*", 0);
116   test_subclass ("text/*", "text/plain", 0);
117 }
118
119 int
120 main (int argc, char *argv[])
121 {
122   const char *result;
123   const char *file_name;
124   int i;
125
126   test_glob_type ();
127   test_aliasing ();
128   test_subclassing ();
129
130   for (i = 1; i < argc; i++)
131     {
132       file_name = argv[i];
133       result = xdg_mime_get_mime_type_for_file (file_name, NULL);
134       printf ("File \"%s\" has a mime-type of %s\n", file_name, result);
135     }
136
137 #if 0
138   xdg_mime_dump ();
139 #endif
140   return 0;
141 }
142