]> git.proxmox.com Git - libxdgmime-perl.git/blame - xdgmime-source/src/test-mime.c
Merge commit '73d5a30a1d93eb79761f2472c685afb8e42a8646' from upstream
[libxdgmime-perl.git] / xdgmime-source / src / test-mime.c
CommitLineData
cf31d981
SI
1/*
2 * Copyright (C) 2003,2004 Red Hat, Inc.
3 * Copyright (C) 2003,2004 Jonathan Blandford <jrb@alum.mit.edu>
4 *
73d5a30a 5 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
cf31d981
SI
6 */
7#include "xdgmime.h"
8#include "xdgmimeglob.h"
9#include <string.h>
73d5a30a 10#include <strings.h>
cf31d981
SI
11#include <stdio.h>
12
13
14static void
15test_individual_glob (const char *glob,
16 XdgGlobType expected_type)
17{
18 XdgGlobType test_type;
19
20 test_type = _xdg_glob_determine_type (glob);
21 if (test_type != expected_type)
22 {
23 printf ("Test Failed: %s is of type %s, but %s is expected\n",
24 glob,
25 ((test_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL":
26 ((test_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_FULL")),
27 ((expected_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL":
28 ((expected_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_COMPLEX")));
29 exit (1);
30 }
31}
32
33static void
34test_glob_type (void)
35{
36 test_individual_glob ("*.gif", XDG_GLOB_SIMPLE);
37 test_individual_glob ("Foo*.gif", XDG_GLOB_FULL);
38 test_individual_glob ("*[4].gif", XDG_GLOB_FULL);
39 test_individual_glob ("Makefile", XDG_GLOB_LITERAL);
40 test_individual_glob ("sldkfjvlsdf\\\\slkdjf", XDG_GLOB_FULL);
41 test_individual_glob ("tree.[ch]", XDG_GLOB_FULL);
42}
43
44static void
45test_alias (const char *mime_a,
46 const char *mime_b,
47 int expected)
48{
49 int actual;
50
51 actual = xdg_mime_mime_type_equal (mime_a, mime_b);
52
53 if (actual != expected)
54 {
55 printf ("Test Failed: %s is %s to %s\n",
56 mime_a, actual ? "equal" : "not equal", mime_b);
57 exit (1);
58 }
59}
60
61static void
62test_aliasing (void)
63{
64 test_alias ("application/wordperfect", "application/vnd.wordperfect", 1);
65 test_alias ("application/x-gnome-app-info", "application/x-desktop", 1);
66 test_alias ("application/x-wordperfect", "application/vnd.wordperfect", 1);
67 test_alias ("application/x-wordperfect", "audio/x-midi", 0);
68 test_alias ("/", "vnd/vnd", 0);
69 test_alias ("application/octet-stream", "text/plain", 0);
70 test_alias ("text/plain", "text/*", 0);
71}
72
73static void
74test_subclass (const char *mime_a,
75 const char *mime_b,
76 int expected)
77{
78 int actual;
79
80 actual = xdg_mime_mime_type_subclass (mime_a, mime_b);
81
82 if (actual != expected)
83 {
84 printf ("Test Failed: %s is %s of %s\n",
85 mime_a, actual ? "subclass" : "not subclass", mime_b);
86 exit (1);
87 }
88}
89
90static void
91test_subclassing (void)
92{
93 test_subclass ("application/rtf", "text/plain", 1);
94 test_subclass ("message/news", "text/plain", 1);
95 test_subclass ("message/news", "message/*", 1);
96 test_subclass ("message/news", "text/*", 1);
97 test_subclass ("message/news", "application/octet-stream", 1);
98 test_subclass ("application/rtf", "application/octet-stream", 1);
99 test_subclass ("application/x-gnome-app-info", "text/plain", 1);
100 test_subclass ("image/x-djvu", "image/vnd.djvu", 1);
101 test_subclass ("image/vnd.djvu", "image/x-djvu", 1);
102 test_subclass ("image/vnd.djvu", "text/plain", 0);
103 test_subclass ("image/vnd.djvu", "text/*", 0);
104 test_subclass ("text/*", "text/plain", 1);
105}
106
107static void
108test_one_match (const char *filename, const char *expected)
109{
110 const char *actual;
111
112 actual = xdg_mime_get_mime_type_from_file_name (filename);
113
114 if (strcasecmp (actual, expected) != 0)
115 {
116 printf ("Test Failed: mime type of %s is %s, expected %s\n",
117 filename, actual, expected);
118 exit (1);
119 }
120}
121
122static void
123test_matches (void)
124{
125 test_one_match ("foo.bar.epub", "application/epub+zip");
126 test_one_match ("core", "application/x-core");
127 test_one_match ("README.in", "text/x-readme");
128 test_one_match ("README.gz", "application/gzip");
129 test_one_match ("blabla.cs", "text/x-csharp");
130 test_one_match ("blabla.f90", "text/x-fortran");
131 test_one_match ("blabla.F95", "text/x-fortran");
132 test_one_match ("tarball.tar.gz", "application/x-compressed-tar");
133 test_one_match ("file.gz", "application/gzip");
134 test_one_match ("file.tar.lzo", "application/x-tzo");
135 test_one_match ("file.lzo", "application/x-lzop");
136}
137
138static void
139test_one_icon (const char *mimetype, const char *expected)
140{
141 const char *actual;
142
143 actual = xdg_mime_get_generic_icon (mimetype);
144
145 if (actual != expected && (actual == NULL || strcmp (actual, expected) != 0))
146 {
147 printf ("Test Failed: icon of %s is %s, expected %s\n",
148 mimetype, actual, expected);
149 exit (1);
150 }
151}
152
153static void
154test_icons (void)
155{
156 test_one_icon ("application/x-font-ttx", "font-x-generic");
157 test_one_icon ("application/mathematica", "x-office-document");
158 test_one_icon ("text/plain", NULL);
159}
160
161int
162main (int argc, char *argv[])
163{
164 const char *result;
165 const char *file_name;
166 int i;
167
168 test_glob_type ();
169 test_aliasing ();
170 test_subclassing ();
171 test_matches ();
172 test_icons ();
173
174 for (i = 1; i < argc; i++)
175 {
176 file_name = argv[i];
177 result = xdg_mime_get_mime_type_for_file (file_name, NULL);
178 printf ("File \"%s\" has a mime-type of %s\n", file_name, result);
179 }
180
181#if 0
182 xdg_mime_dump ();
183#endif
184 return 0;
185}
186