makefiles: Always use the global SOURCES variable for .l files.
[wine.git] / dlls / msxml4 / tests / domdoc.c
blob39fe8c5a9952fd380722d61a15a2a78b551ed5f6
1 /*
2 * XML test
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2007-2008 Alistair Leslie-Hughes
6 * Copyright 2010-2011 Adam Martinson for CodeWeavers
7 * Copyright 2010-2013 Nikolay Sivov for CodeWeavers
8 * Copyright 2023 Daniel Lehman
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define COBJMACROS
27 #define CONST_VTABLE
29 #include <stdio.h>
30 #include <assert.h>
32 #include "windows.h"
34 #include "initguid.h"
35 #include "msxml2.h"
37 #include "wine/test.h"
39 static BSTR alloced_bstrs[256];
40 static int alloced_bstrs_count;
42 static BSTR alloc_str_from_narrow(const char *str)
44 int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
45 BSTR ret = SysAllocStringLen(NULL, len - 1); /* NUL character added automatically */
46 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
47 return ret;
50 static BSTR _bstr_(const char *str)
52 assert(alloced_bstrs_count < ARRAY_SIZE(alloced_bstrs));
53 alloced_bstrs[alloced_bstrs_count] = alloc_str_from_narrow(str);
54 return alloced_bstrs[alloced_bstrs_count++];
57 static void free_bstrs(void)
59 int i;
60 for (i = 0; i < alloced_bstrs_count; i++)
61 SysFreeString(alloced_bstrs[i]);
62 alloced_bstrs_count = 0;
65 /* see dlls/msxml[36]/tests/domdoc.c */
66 static void test_namespaces_as_attributes(void)
68 struct test
70 const char *xml;
71 int explen;
72 const char *names[3];
73 const char *prefixes[3];
74 const char *basenames[3];
75 const char *uris[3];
76 const char *texts[3];
78 static const struct test tests[] =
81 "<a ns:b=\"b attr\" d=\"d attr\" xmlns:ns=\"nshref\" />", 3,
82 { "ns:b", "d", "xmlns:ns" }, /* nodeName */
83 { "ns", NULL, "xmlns" }, /* prefix */
84 { "b", "d", "ns" }, /* baseName */
85 { "nshref", NULL, "" }, /* namespaceURI */
86 { "b attr", "d attr", "nshref" }, /* text */
88 /* property only */
90 "<a d=\"d attr\" />", 1,
91 { "d" }, /* nodeName */
92 { NULL }, /* prefix */
93 { "d" }, /* baseName */
94 { NULL }, /* namespaceURI */
95 { "d attr" }, /* text */
97 /* namespace only */
99 "<a xmlns:ns=\"nshref\" />", 1,
100 { "xmlns:ns" }, /* nodeName */
101 { "xmlns" }, /* prefix */
102 { "ns" }, /* baseName */
103 { "" }, /* namespaceURI */
104 { "nshref" }, /* text */
106 /* no properties or namespaces */
108 "<a />", 0,
111 { NULL }
113 const struct test *test;
114 IXMLDOMNamedNodeMap *map;
115 IXMLDOMNode *node, *item;
116 IXMLDOMDocument2 *doc;
117 VARIANT_BOOL b;
118 LONG len, i;
119 HRESULT hr;
120 BSTR str;
122 test = tests;
123 while (test->xml)
125 hr = CoCreateInstance(&CLSID_DOMDocument40, NULL, CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument2, (void **)&doc);
126 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
128 hr = IXMLDOMDocument2_loadXML(doc, _bstr_(test->xml), &b);
129 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
131 node = NULL;
132 hr = IXMLDOMDocument2_selectSingleNode(doc, _bstr_("a"), &node);
133 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
135 hr = IXMLDOMNode_get_attributes(node, &map);
136 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
138 len = -1;
139 hr = IXMLDOMNamedNodeMap_get_length(map, &len);
140 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
141 ok(len == test->explen, "got %ld\n", len);
143 item = NULL;
144 hr = IXMLDOMNamedNodeMap_get_item(map, test->explen+1, &item);
145 ok(hr == S_FALSE, "Unexpected hr %#lx.\n", hr);
146 ok(!item, "Item should be NULL\n");
148 for (i = 0; i < len; i++)
150 item = NULL;
151 hr = IXMLDOMNamedNodeMap_get_item(map, i, &item);
152 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
154 str = NULL;
155 hr = IXMLDOMNode_get_nodeName(item, &str);
156 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
157 ok(!lstrcmpW(str, _bstr_(test->names[i])), "got %s\n", wine_dbgstr_w(str));
158 SysFreeString(str);
160 str = NULL;
161 hr = IXMLDOMNode_get_prefix(item, &str);
162 if (test->prefixes[i])
164 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
165 ok(!lstrcmpW(str, _bstr_(test->prefixes[i])), "got %s\n", wine_dbgstr_w(str));
166 SysFreeString(str);
168 else
169 ok(hr == S_FALSE, "Unexpected hr %#lx.\n", hr );
171 str = NULL;
172 hr = IXMLDOMNode_get_baseName(item, &str);
173 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
174 ok(!lstrcmpW(str, _bstr_(test->basenames[i])), "got %s\n", wine_dbgstr_w(str));
175 SysFreeString(str);
177 str = NULL;
178 hr = IXMLDOMNode_get_namespaceURI(item, &str);
179 if (test->uris[i])
181 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
182 if (test->prefixes[i] && !strcmp(test->prefixes[i], "xmlns"))
183 ok(!lstrcmpW(str, L""), "got %s\n", wine_dbgstr_w(str));
184 else
185 ok(!lstrcmpW(str, _bstr_(test->uris[i])), "got %s\n", wine_dbgstr_w(str));
186 SysFreeString(str);
188 else
189 ok(hr == S_FALSE, "Unexpected hr %#lx.\n", hr );
191 str = NULL;
192 hr = IXMLDOMNode_get_text(item, &str);
193 ok(hr == S_OK, "Unexpected hr %#lx.\n", hr);
194 ok(!lstrcmpW(str, _bstr_(test->texts[i])), "got %s\n", wine_dbgstr_w(str));
195 SysFreeString(str);
197 IXMLDOMNode_Release(item);
200 IXMLDOMNamedNodeMap_Release(map);
201 IXMLDOMNode_Release(node);
202 IXMLDOMDocument2_Release(doc);
204 test++;
206 free_bstrs();
209 START_TEST(domdoc)
211 HRESULT hr;
212 IXMLDOMDocument2 *doc;
214 hr = CoInitialize(NULL);
215 ok(hr == S_OK, "failed to init com\n");
217 hr = CoCreateInstance(&CLSID_DOMDocument40, NULL, CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument2, (void **)&doc);
218 if (hr != S_OK)
220 win_skip("class &CLSID_DOMDocument40 not supported\n");
221 return;
223 IXMLDOMDocument2_Release(doc);
225 test_namespaces_as_attributes();
227 CoUninitialize();