1 module tests;
2 
3 import structuredrpc;
4 import treeserial;
5 
6 import std.stdio;
7 import std.exception;
8 import std.conv;
9 
10 unittest {
11 	pragma(msg, "Compiling Test Receive");
12 	writeln("Running Test Receive");
13 	
14 	enum Src;
15 	
16 	string lastMsg = "";
17 	class A {
18 		@RPC!Src
19 		void msg(int x) {
20 			lastMsg = "msg: "~x.to!string;
21 		}
22 		@RPC!Src
23 		void msg2(float x) {
24 			lastMsg = "msg2: "~x.to!string;
25 		}
26 		mixin MakeRPCReceive!Src;
27 	}
28 	A a = new A;
29 	a.msg(5);
30 	assert(lastMsg == "msg: 5");
31 	a.rpcRecv!Src(0 ~ serialize(1));
32 	assert(lastMsg == "msg: 1");
33 }
34 unittest {
35 	pragma(msg, "Compiling Test Receive w/ Connection");
36 	writeln("Running Test Receive w/ Connection");
37 	
38 	enum Src;
39 	class Connection {}
40 	
41 	string lastMsg = "";
42 	class A {
43 		@RPC!Src
44 		void msg(int x) {
45 			lastMsg = "msg: "~x.to!string;
46 		}
47 		void msg2(float x) {
48 			lastMsg = "msg2: "~x.to!string;
49 		}
50 		@RPC!Src
51 		void msg2(float x, Connection con) {
52 			lastMsg = "msg2: remote - "~x.to!string;
53 		}
54 		@RPC!Src
55 		void msg3(Connection con, float x) {
56 			lastMsg = "msg3: remote - "~x.to!string;
57 		}
58 		@RPC!Src
59 		void msg4(float x) {
60 			lastMsg = "msg4: remote - "~x.to!string;
61 		}
62 		mixin MakeRPCReceive!(Src, Connection);
63 	}
64 	A a = new A;
65 	a.msg(5);
66 	assert(lastMsg == "msg: 5");
67 	a.rpcRecv!Src(0 ~ serialize(1));
68 	assert(lastMsg == "msg: 1");
69 	assertThrown!Throwable(a.rpcRecv!Src(1 ~ serialize(1.5f)));
70 	a.rpcRecv!Src(new Connection(), 1 ~ serialize(1f));
71 	assert(lastMsg == "msg2: remote - 1");
72 	a.rpcRecv!Src(new Connection(), 2 ~ serialize(1f));
73 	assert(lastMsg == "msg3: remote - 1");
74 	a.rpcRecv!Src(new Connection(), 3 ~ serialize(1f));
75 	assert(lastMsg == "msg4: remote - 1");
76 }
77 unittest {
78 	pragma(msg, "Compiling Test Split-src Receive");
79 	writeln("Running Test Split-src Receive");
80 	
81 	enum SrcClient;
82 	enum SrcServer;
83 	class Client {}
84 	
85 	string lastMsg = "";
86 	class A {
87 		@RPC!SrcClient
88 		void msg(int x, Client client) {
89 			lastMsg = "msg: "~x.to!string;
90 		}
91 		@RPC!SrcServer
92 		void msg2(int x) {
93 			lastMsg = "msg2: "~x.to!string;
94 		}
95 		@RPC!SrcClient @RPC!SrcServer
96 		void msg3(int x) {
97 			lastMsg = "msg3: "~x.to!string;
98 		}
99 		mixin MakeRPCReceive!(SrcClient, Client);
100 		mixin MakeRPCReceive!(SrcServer);
101 	}
102 	A a = new A;
103 	a.rpcRecv!SrcClient(new Client, 0 ~ serialize(1));
104 	assert(lastMsg == "msg: 1");
105 	a.rpcRecv!SrcClient(new Client, 1 ~ serialize(1));
106 	assert(lastMsg == "msg3: 1");
107 	a.rpcRecv!SrcServer(0 ~ serialize(1));
108 	assert(lastMsg == "msg2: 1");
109 	a.rpcRecv!SrcServer(1 ~ serialize(1));
110 	assert(lastMsg == "msg3: 1");
111 }
112 unittest {
113 	pragma(msg, "Compiling Test Send");
114 	writeln("Running Test Send");
115 	
116 	enum Src;
117 	
118 	string lastMsg = "";
119 	const(ubyte)[] lastSendData = [];
120 	class A {
121 		@RPCSend!Src
122 		void rpcSend(const(ubyte)[] data) {
123 			lastSendData = data;
124 		}
125 		@RPC!Src
126 		void msg(int x) {
127 			lastMsg = "msg: "~x.to!string;
128 		}
129 		mixin MakeRPCReceive!Src;
130 		mixin MakeRPCSend!Src;
131 	}
132 	A a = new A;
133 	a.msg_send!Src(5);
134 	assert(lastSendData == [0,5,0,0,0]);
135 }
136 unittest {
137 	pragma(msg, "Compiling Test Split-src Send");
138 	writeln("Running Test Split-src Send");
139 	
140 	enum SrcClient;
141 	enum SrcServer;
142 	class Client {}
143 	
144 	string lastMsg = "";
145 	const(ubyte)[] lastSendData = [];
146 	class A {
147 		@RPC!SrcClient
148 		void msg(int x, Client client) {
149 			lastMsg = "msg: "~x.to!string;
150 		}
151 		@RPC!SrcServer
152 		void msg2(int x) {
153 			lastMsg = "msg2: "~x.to!string;
154 		}
155 		@RPC!SrcClient @RPC!SrcServer
156 		void msg3(int x) {
157 			lastMsg = "msg3: "~x.to!string;
158 		}
159 		mixin MakeRPCReceive!(SrcClient, Client);
160 		mixin MakeRPCReceive!(SrcServer);
161 		@RPCSend!SrcClient
162 		void rpcSend(Client[] clients, const(ubyte)[] data) {
163 			lastSendData = 0~data;
164 		}
165 		@RPCSend!SrcServer
166 		void rpcSend(const(ubyte)[] data) {
167 			lastSendData = 1~data;
168 		}
169 		mixin MakeRPCSend!(SrcClient, Client);
170 		mixin MakeRPCSend!(SrcServer);
171 	}
172 	A a = new A;
173 	a.msg_send!SrcClient([new Client], 1);
174 	assert(lastSendData == [0, 0,1,0,0,0]);
175 	
176 	a.msg_send!SrcClient(new Client, 5);
177 	assert(lastSendData == [0, 0,5,0,0,0]);
178 	a.msg2_send!SrcServer(5);
179 	assert(lastSendData == [1, 0,5,0,0,0]);
180 	a.msg3_send!SrcClient(new Client, 5);
181 	assert(lastSendData == [0, 1,5,0,0,0]);
182 	a.msg3_send!SrcServer(5);
183 	assert(lastSendData == [1, 1,5,0,0,0]);
184 	
185 	a.rpcRecv!SrcClient(new Client, 0 ~ serialize(1));
186 	assert(lastMsg == "msg: 1");
187 	a.rpcRecv!SrcClient(new Client, 1 ~ serialize(1));
188 	assert(lastMsg == "msg3: 1");
189 	a.rpcRecv!SrcServer(0 ~ serialize(1));
190 	assert(lastMsg == "msg2: 1");
191 	a.rpcRecv!SrcServer(1 ~ serialize(1));
192 	assert(lastMsg == "msg3: 1");
193 }
194 unittest {
195 	pragma(msg, "Compiling Test Different Send");
196 	writeln("Running Test Different Send");
197 	
198 	enum Src;
199 	class Client {}
200 	
201 	string lastMsg = "";
202 	const(ubyte)[] lastSendData = [];
203 	class ClientClass() {
204 		@RPC!Src
205 		void msg(int x) {
206 			lastMsg = "msg: "~x.to!string;
207 		}
208 		mixin MakeRPCReceive!(Src);
209 		
210 		@RPCSend!Src
211 		void rpcSend(const(ubyte)[] data) {
212 			lastSendData = 0~data;
213 		}
214 		mixin MakeRPCSendTo!(ServerClass!(), Src, Client);
215 	}
216 	class ServerClass() {
217 		@RPC!Src
218 		void msg2(int x, Client client) {
219 			lastMsg = "msg2: "~x.to!string;
220 		}
221 		mixin MakeRPCReceive!(Src, Client);
222 		
223 		@RPCSend!Src
224 		void rpcSend(Client[] clients, const(ubyte)[] data) {
225 			lastSendData = 1~data;
226 		}
227 		mixin MakeRPCSendTo!(ClientClass!(), Src);
228 	}
229 	
230 	ClientClass!() c = new ClientClass!();
231 	ServerClass!() s = new ServerClass!();
232 	
233 	s.msg_send!Src([new Client], 1);
234 	assert(lastSendData == [1, 0,1,0,0,0]);
235 	c.rpcRecv!Src(lastSendData[1..$]);
236 	assert(lastMsg == "msg: 1");
237 	
238 	s.msg_send!Src(new Client, 5);
239 	assert(lastSendData == [1, 0,5,0,0,0]);
240 	c.rpcRecv!Src(lastSendData[1..$]);
241 	assert(lastMsg == "msg: 5");
242 	
243 	c.msg2_send!Src(5);
244 	assert(lastSendData == [0, 0,5,0,0,0]);
245 	s.rpcRecv!Src(new Client(), lastSendData[1..$]);
246 	assert(lastMsg == "msg2: 5");
247 }
248 unittest {
249 	pragma(msg, "Compiling Test Template-src Receive");
250 	writeln("Running Test Template-src Receive");
251 	
252 	enum SrcA;
253 	enum SrcB;
254 	
255 	string lastMsg = "";
256 	class A {
257 		@RPC!SrcA
258 		@RPC!SrcB
259 		void msg(Src)(int x) {
260 			static if (is(Src==SrcA))
261 				lastMsg = "msg: a - "~x.to!string;
262 			static if (is(Src==SrcB))
263 				lastMsg = "msg: b - "~x.to!string;
264 		}
265 		mixin MakeRPCReceive!SrcA;
266 		mixin MakeRPCReceive!SrcB;
267 	}
268 	A a = new A;
269 	a.rpcRecv!SrcA(0 ~ serialize(1));
270 	assert(lastMsg == "msg: a - 1");
271 	a.rpcRecv!SrcB(0 ~ serialize(1));
272 	assert(lastMsg == "msg: b - 1");
273 }
274 unittest {
275 	pragma(msg, "Compiling Test Send in Receive");
276 	writeln("Running Test Send in Receive");
277 	
278 	enum Src;
279 	
280 	string lastMsg = "";
281 	const(ubyte)[] lastSendData = [];
282 	class A {
283 		@RPCSend!Src
284 		void rpcSend(const(ubyte)[] data) {
285 			lastSendData = data;
286 		}
287 		@RPC!Src
288 		void msg(int x) {
289 			lastMsg = "msg: "~x.to!string;
290 			msg_send!Src(5);
291 		}
292 		mixin MakeRPCReceive!Src;
293 		mixin MakeRPCSend!Src;
294 	}
295 	A a = new A;
296 	a.rpcRecv!Src(0 ~ serialize(1));
297 	assert(lastMsg == "msg: 1");
298 	assert(lastSendData == [0,5,0,0,0]);
299 }
300 unittest {
301 	pragma(msg, "Compiling Test Send in Template Receive");
302 	writeln("Running Test Send in Template Receive");
303 	
304 	enum Src;
305 	
306 	string lastMsg = "";
307 	const(ubyte)[] lastSendData = [];
308 	class A {
309 		@RPCSend!Src
310 		void rpcSend(const(ubyte)[] data) {
311 			lastSendData = data;
312 		}
313 		@RPC!Src
314 		void msg(S)(int x) {
315 			lastMsg = "msg: "~x.to!string;
316 			msg_send!Src(5);
317 		}
318 		mixin MakeRPCReceive!Src;
319 		mixin MakeRPCSend!Src;
320 	}
321 	A a = new A;
322 	a.rpcRecv!Src(0 ~ serialize(1));
323 	assert(lastMsg == "msg: 1");
324 	assert(lastSendData == [0,5,0,0,0]);
325 }
326 unittest {
327 	pragma(msg, "Compiling Test Different Send in Template Receive");
328 	writeln("Running Test Different Send in Template Receive");
329 	
330 	enum Src;
331 	class Client {}
332 	
333 	string lastMsg = "";
334 	const(ubyte)[] lastSendData = [];
335 	class ClientClass() {
336 		@RPC!Src
337 		void msg(S)(int x) {
338 			lastMsg = "msg: "~x.to!string;
339 			msg2_send!Src(x+1);
340 		}
341 		mixin MakeRPCReceive!(Src);
342 		
343 		@RPCSend!Src
344 		void rpcSend(const(ubyte)[] data) {
345 			lastSendData = 0~data;
346 		}
347 		mixin MakeRPCSendTo!(ServerClass!(), Src, Client);
348 	}
349 	class ServerClass() {
350 		@RPC!Src
351 		void msg2(S)(int x, Client client) {
352 			lastMsg = "msg2: "~x.to!string;
353 			msg_send!Src([client], x+1);
354 		}
355 		mixin MakeRPCReceive!(Src, Client);
356 		
357 		@RPCSend!Src
358 		void rpcSend(Client[] clients, const(ubyte)[] data) {
359 			lastSendData = 1~data;
360 		}
361 		mixin MakeRPCSendTo!(ClientClass!(), Src);
362 	}
363 	
364 	ClientClass!() c = new ClientClass!();
365 	ServerClass!() s = new ServerClass!();
366 	
367 	c.rpcRecv!Src([0,1,0,0,0]);
368 	assert(lastMsg == "msg: 1");
369 	assert(lastSendData == [0, 0,2,0,0,0]);
370 	
371 	s.rpcRecv!Src(new Client, [0,3,0,0,0]);
372 	assert(lastMsg == "msg2: 3");
373 	assert(lastSendData == [1, 0,4,0,0,0]);
374 }
375 
376