2 * fontconfig/test/test-family-matching.c
4 * Copyright © 2020 Zoltan Vandrus
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
26 #include <fontconfig/fontconfig.h>
28 #define FC_TEST_RESULT "testresult"
30 typedef enum _TestMatchResult {
36 typedef enum _TestResult {
42 static TestMatchResult
43 TestMatchPattern (const char *test, FcPattern *p)
45 const FcChar8 *xml_pre = (const FcChar8 *) ""
50 const FcChar8 *xml_post = (const FcChar8 *) ""
51 " <edit name=\""FC_TEST_RESULT"\">\n"
52 " <bool>true</bool>\n"
58 FcPattern *pat = NULL;
59 FcChar8 *concat = NULL;
64 TestMatchResult ret = TestMatchError;
66 pat = FcPatternDuplicate (p);
69 fprintf (stderr, "Unable to duplicate pattern.\n");
73 concat = FcStrPlus (xml_pre, (const FcChar8 *) test);
76 fprintf (stderr, "Concatenation failed.\n");
80 xml = FcStrPlus (concat, xml_post);
83 fprintf (stderr, "Concatenation failed.\n");
87 cfg = FcConfigCreate ();
90 fprintf (stderr, "Unable to create a new empty config.\n");
94 if (!FcConfigParseAndLoadFromMemory (cfg, xml, FcTrue))
96 fprintf (stderr, "Unable to load a config from memory.\n");
100 if (!FcConfigSubstitute (cfg, pat, FcMatchPattern))
102 fprintf (stderr, "Unable to substitute config.\n");
106 result = FcPatternGetBool (pat, FC_TEST_RESULT, 0, &dummy);
111 case FcResultNoMatch:
115 fprintf (stderr, "Unable to check pattern.\n");
121 FcConfigDestroy (cfg);
127 FcPatternDestroy (pat);
132 TestShouldMatchPattern(const char* test, FcPattern *pat, int negate)
134 switch (TestMatchPattern (test, pat)) {
141 printf ("Following test unexpectedly matched:\n%s", test);
143 FcPatternPrint (pat);
149 printf ("Following test should have matched:\n%s", test);
151 FcPatternPrint (pat);
163 fprintf (stderr, "This shouldn't have been reached.\n");
168 #define MAX(a,b) ((a) > (b) ? (a) : (b))
171 UpdateResult (TestResult* res, TestResult resNew)
173 *res = MAX(*res, resNew);
181 TestResult res = TestPassed;
183 FcPattern *pat = FcPatternBuild (NULL,
184 FC_FAMILY, FcTypeString, "family1",
185 FC_FAMILY, FcTypeString, "family2",
186 FC_FAMILY, FcTypeString, "family3",
191 fprintf (stderr, "Unable to build pattern.\n");
195 #define SHOULD_MATCH(p,t) \
196 UpdateResult (&res, TestShouldMatchPattern (t, p, 0))
197 #define SHOULD_NOT_MATCH(p,t) \
198 UpdateResult (&res, TestShouldMatchPattern (t, p, 1))
200 test = "<test qual=\"all\" name=\"family\" compare=\"not_eq\">\n"
201 " <string>foo</string>\n"
204 SHOULD_MATCH(pat, test);
207 "<test qual=\"all\" name=\"family\" compare=\"not_eq\">\n"
208 " <string>family2</string>\n"
211 SHOULD_NOT_MATCH(pat, test);
214 "<test qual=\"any\" name=\"family\" compare=\"eq\">\n"
215 " <string>family3</string>\n"
218 SHOULD_MATCH(pat, test);
221 "<test qual=\"any\" name=\"family\" compare=\"eq\">\n"
222 " <string>foo</string>\n"
225 SHOULD_NOT_MATCH(pat, test);
227 FcPatternDestroy (pat);
234 return (TestFamily ());