jscript: Added expression statement implementation.
[wine] / dlls / jscript / tests / run.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdio.h>
20
21 #define COBJMACROS
22 #define CONST_VTABLE
23
24 #include <ole2.h>
25 #include <dispex.h>
26 #include <activscp.h>
27
28 #include "wine/test.h"
29
30 static const CLSID CLSID_JScript =
31     {0xf414c260,0x6ac0,0x11cf,{0xb6,0xd1,0x00,0xaa,0x00,0xbb,0xbb,0x58}};
32
33 #define DEFINE_EXPECT(func) \
34     static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
35
36 #define SET_EXPECT(func) \
37     expect_ ## func = TRUE
38
39 #define SET_CALLED(func) \
40     called_ ## func = TRUE
41
42 #define CHECK_EXPECT2(func) \
43     do { \
44         ok(expect_ ##func, "unexpected call " #func "\n"); \
45         called_ ## func = TRUE; \
46     }while(0)
47
48 #define CHECK_EXPECT(func) \
49     do { \
50         CHECK_EXPECT2(func); \
51         expect_ ## func = FALSE; \
52     }while(0)
53
54 #define CHECK_CALLED(func) \
55     do { \
56         ok(called_ ## func, "expected " #func "\n"); \
57         expect_ ## func = called_ ## func = FALSE; \
58     }while(0)
59
60 static const WCHAR testW[] = {'t','e','s','t',0};
61
62 static const char *debugstr_w(LPCWSTR str)
63 {
64     static char buf[1024];
65
66     if(!str)
67         return "(null)";
68
69     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, sizeof(buf), NULL, NULL);
70     return buf;
71 }
72
73 static BSTR a2bstr(const char *str)
74 {
75     BSTR ret;
76     int len;
77
78     len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
79     ret = SysAllocStringLen(NULL, len-1);
80     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
81
82     return ret;
83 }
84
85 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
86 {
87     *ppv = NULL;
88
89     if(IsEqualGUID(riid, &IID_IUnknown)
90        || IsEqualGUID(riid, &IID_IDispatch)
91        || IsEqualGUID(riid, &IID_IDispatchEx))
92         *ppv = iface;
93     else
94         return E_NOINTERFACE;
95
96     return S_OK;
97 }
98
99 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
100 {
101     return 2;
102 }
103
104 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
105 {
106     return 1;
107 }
108
109 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
110 {
111     ok(0, "unexpected call\n");
112     return E_NOTIMPL;
113 }
114
115 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
116                                               LCID lcid, ITypeInfo **ppTInfo)
117 {
118     ok(0, "unexpected call\n");
119     return E_NOTIMPL;
120 }
121
122 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
123                                                 LPOLESTR *rgszNames, UINT cNames,
124                                                 LCID lcid, DISPID *rgDispId)
125 {
126     ok(0, "unexpected call\n");
127     return E_NOTIMPL;
128 }
129
130 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
131                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
132                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
133 {
134     ok(0, "unexpected call\n");
135     return E_NOTIMPL;
136 }
137
138 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
139 {
140     ok(0, "unexpected call %s %x\n", debugstr_w(bstrName), grfdex);
141     return E_NOTIMPL;
142 }
143
144 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
145 {
146     ok(0, "unexpected call\n");
147     return E_NOTIMPL;
148 }
149
150 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
151 {
152     ok(0, "unexpected call\n");
153     return E_NOTIMPL;
154 }
155
156 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
157 {
158     ok(0, "unexpected call\n");
159     return E_NOTIMPL;
160 }
161
162 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
163 {
164     ok(0, "unexpected call\n");
165     return E_NOTIMPL;
166 }
167
168 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
169 {
170     ok(0, "unexpected call\n");
171     return E_NOTIMPL;
172 }
173
174 static HRESULT WINAPI Global_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
175 {
176     return DISP_E_UNKNOWNNAME;
177 }
178
179 static HRESULT WINAPI Global_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
180         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
181 {
182     return DISP_E_MEMBERNOTFOUND;
183 }
184
185 static IDispatchExVtbl GlobalVtbl = {
186     DispatchEx_QueryInterface,
187     DispatchEx_AddRef,
188     DispatchEx_Release,
189     DispatchEx_GetTypeInfoCount,
190     DispatchEx_GetTypeInfo,
191     DispatchEx_GetIDsOfNames,
192     DispatchEx_Invoke,
193     Global_GetDispID,
194     Global_InvokeEx,
195     DispatchEx_DeleteMemberByName,
196     DispatchEx_DeleteMemberByDispID,
197     DispatchEx_GetMemberProperties,
198     DispatchEx_GetMemberName,
199     DispatchEx_GetNextDispID,
200     DispatchEx_GetNameSpaceParent
201 };
202
203 static IDispatchEx Global = { &GlobalVtbl };
204
205 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
206 {
207     *ppv = NULL;
208
209     if(IsEqualGUID(&IID_IUnknown, riid))
210         *ppv = iface;
211     else if(IsEqualGUID(&IID_IActiveScriptSite, riid))
212         *ppv = iface;
213     else
214         return E_NOINTERFACE;
215
216     IUnknown_AddRef((IUnknown*)*ppv);
217     return S_OK;
218 }
219
220 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
221 {
222     return 2;
223 }
224
225 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
226 {
227     return 1;
228 }
229
230 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
231 {
232     *plcid = GetUserDefaultLCID();
233     return S_OK;
234 }
235
236 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR pstrName,
237         DWORD dwReturnMask, IUnknown **ppiunkItem, ITypeInfo **ppti)
238 {
239     ok(!lstrcmpW(pstrName, testW), "unexpected pstrName %s\n", debugstr_w(pstrName));
240     ok(dwReturnMask == SCRIPTINFO_IUNKNOWN, "unexpected dwReturnMask %x\n", dwReturnMask);
241     ok(!ppti, "ppti != NULL\n");
242
243     *ppiunkItem = (IUnknown*)&Global;
244     return S_OK;
245 }
246
247 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *pbstrVersion)
248 {
249     return E_NOTIMPL;
250 }
251
252 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
253         const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
254 {
255     return E_NOTIMPL;
256 }
257
258 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE ssScriptState)
259 {
260     return E_NOTIMPL;
261 }
262
263 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *pscripterror)
264 {
265     return E_NOTIMPL;
266 }
267
268 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
269 {
270     return E_NOTIMPL;
271 }
272
273 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
274 {
275     return E_NOTIMPL;
276 }
277
278 #undef ACTSCPSITE_THIS
279
280 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
281     ActiveScriptSite_QueryInterface,
282     ActiveScriptSite_AddRef,
283     ActiveScriptSite_Release,
284     ActiveScriptSite_GetLCID,
285     ActiveScriptSite_GetItemInfo,
286     ActiveScriptSite_GetDocVersionString,
287     ActiveScriptSite_OnScriptTerminate,
288     ActiveScriptSite_OnStateChange,
289     ActiveScriptSite_OnScriptError,
290     ActiveScriptSite_OnEnterScript,
291     ActiveScriptSite_OnLeaveScript
292 };
293
294 static IActiveScriptSite ActiveScriptSite = { &ActiveScriptSiteVtbl };
295
296 static IActiveScript *create_script(void)
297 {
298     IActiveScript *script;
299     HRESULT hres;
300
301     hres = CoCreateInstance(&CLSID_JScript, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
302             &IID_IActiveScript, (void**)&script);
303     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
304
305     return script;
306 }
307
308 static void parse_script(BSTR script_str)
309 {
310     IActiveScriptParse *parser;
311     IActiveScript *engine;
312     HRESULT hres;
313
314     engine = create_script();
315     if(!engine)
316         return;
317
318     hres = IActiveScript_QueryInterface(engine, &IID_IActiveScriptParse, (void**)&parser);
319     ok(hres == S_OK, "Could not get IActiveScriptParse: %08x\n", hres);
320
321     hres = IActiveScriptParse_InitNew(parser);
322     ok(hres == S_OK, "InitNew failed: %08x\n", hres);
323
324     hres = IActiveScript_SetScriptSite(engine, &ActiveScriptSite);
325     ok(hres == S_OK, "SetScriptSite failed: %08x\n", hres);
326
327     hres = IActiveScript_AddNamedItem(engine, testW,
328             SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE|SCRIPTITEM_GLOBALMEMBERS);
329     todo_wine
330     ok(hres == S_OK, "AddNamedItem failed: %08x\n", hres);
331
332     hres = IActiveScript_SetScriptState(engine, SCRIPTSTATE_STARTED);
333     ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_CONNECTED) failed: %08x\n", hres);
334
335     hres = IActiveScriptParse_ParseScriptText(parser, script_str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
336     ok(hres == S_OK, "ParseScriptText failed: %08x\n", hres);
337
338     IActiveScript_Release(engine);
339     IActiveScriptParse_Release(parser);
340 }
341
342 static void parse_script_a(const char *src)
343 {
344     BSTR tmp = a2bstr(src);
345     parse_script(tmp);
346     SysFreeString(tmp);
347 }
348
349 static void run_tests(void)
350 {
351     parse_script_a("");
352     parse_script_a("/* empty */ ;");
353 }
354
355 START_TEST(run)
356 {
357     CoInitialize(NULL);
358
359     run_tests();
360
361     CoUninitialize();
362 }