aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Shared/CodeTools
diff options
context:
space:
mode:
authorSean Dague2008-09-08 20:34:45 +0000
committerSean Dague2008-09-08 20:34:45 +0000
commitce0a8d7beffccbaeb6b603a96b7729278c4c9e75 (patch)
treefaccd65637c0dab11b2dd787c8985cd7f2c9452b /OpenSim/Region/ScriptEngine/Shared/CodeTools
parentFix component order on a quaternion for the sit target. This caused (diff)
downloadopensim-SC_OLD-ce0a8d7beffccbaeb6b603a96b7729278c4c9e75.zip
opensim-SC_OLD-ce0a8d7beffccbaeb6b603a96b7729278c4c9e75.tar.gz
opensim-SC_OLD-ce0a8d7beffccbaeb6b603a96b7729278c4c9e75.tar.bz2
opensim-SC_OLD-ce0a8d7beffccbaeb6b603a96b7729278c4c9e75.tar.xz
changes to Test directory structure per opensim-dev conversation
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/CodeTools')
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs1605
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs153
2 files changed, 1758 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs
new file mode 100644
index 0000000..fefcada
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs
@@ -0,0 +1,1605 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System.Collections.Generic;
29using System.Text.RegularExpressions;
30using NUnit.Framework;
31using OpenSim.Region.ScriptEngine.Shared.CodeTools;
32
33namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests
34{
35 /// <summary>
36 /// Tests the LSL compiler, both the code generation and transformation.
37 /// Each test has some LSL code as input and C# code as expected output.
38 /// The generated C# code is compared against the expected C# code.
39 /// </summary>
40 [TestFixture]
41 public class CSCodeGeneratorTest
42 {
43 [Test]
44 public void TestDefaultState()
45 {
46 string input = @"default
47{
48 state_entry()
49 {
50 }
51}
52";
53 string expected =
54 "\n public void default_event_state_entry()" +
55 "\n {" +
56 "\n }\n";
57
58 CSCodeGenerator cg = new CSCodeGenerator();
59 string output = cg.Convert(input);
60 Assert.AreEqual(expected, output);
61 }
62
63 [Test]
64 public void TestCustomState()
65 {
66 string input = @"default
67{
68 state_entry()
69 {
70 }
71}
72
73state another_state
74{
75 no_sensor()
76 {
77 }
78}
79";
80 string expected =
81 "\n public void default_event_state_entry()" +
82 "\n {" +
83 "\n }" +
84 "\n public void another_state_event_no_sensor()" +
85 "\n {" +
86 "\n }\n";
87
88 CSCodeGenerator cg = new CSCodeGenerator();
89 string output = cg.Convert(input);
90 Assert.AreEqual(expected, output);
91 }
92
93 [Test]
94 public void TestEventWithArguments()
95 {
96 string input = @"default
97{
98 at_rot_target(integer tnum, rotation targetrot, rotation ourrot)
99 {
100 }
101}
102";
103 string expected =
104 "\n public void default_event_at_rot_target(LSL_Types.LSLInteger tnum, LSL_Types.Quaternion targetrot, LSL_Types.Quaternion ourrot)" +
105 "\n {" +
106 "\n }\n";
107
108 CSCodeGenerator cg = new CSCodeGenerator();
109 string output = cg.Convert(input);
110 Assert.AreEqual(expected, output);
111 }
112
113 [Test]
114 public void TestIntegerDeclaration()
115 {
116 string input = @"default
117{
118 touch_start(integer num_detected)
119 {
120 integer x;
121 }
122}
123";
124 string expected =
125 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
126 "\n {" +
127 "\n LSL_Types.LSLInteger x = 0;" +
128 "\n }\n";
129
130 CSCodeGenerator cg = new CSCodeGenerator();
131 string output = cg.Convert(input);
132 Assert.AreEqual(expected, output);
133 }
134
135 [Test]
136 public void TestAssignments()
137 {
138 string input = @"default
139{
140 touch_start(integer num_detected)
141 {
142 string y;
143 integer x = 14;
144 y = ""Hello"";
145 }
146}
147";
148 string expected =
149 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
150 "\n {" +
151 "\n LSL_Types.LSLString y = \"\";" +
152 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14);" +
153 "\n y = new LSL_Types.LSLString(\"Hello\");" +
154 "\n }\n";
155
156 CSCodeGenerator cg = new CSCodeGenerator();
157 string output = cg.Convert(input);
158 Assert.AreEqual(expected, output);
159 }
160
161 [Test]
162 public void TestAdditionSubtractionOperator()
163 {
164 string input = @"default
165{
166 touch_start(integer num_detected)
167 {
168 integer y = -3;
169 integer x = 14 + 6;
170 y = 12 +45+20+x + 23 + 1 + x + y;
171 y = 12 + -45 + - 20 + x + 23 + -1 + x + y;
172 }
173}
174";
175 string expected =
176 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)\n" +
177 " {\n" +
178 " LSL_Types.LSLInteger y = -new LSL_Types.LSLInteger(3);\n" +
179 " LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);\n" +
180 " y = new LSL_Types.LSLInteger(12) + new LSL_Types.LSLInteger(45) + new LSL_Types.LSLInteger(20) + x + new LSL_Types.LSLInteger(23) + new LSL_Types.LSLInteger(1) + x + y;\n" +
181 " y = new LSL_Types.LSLInteger(12) + -new LSL_Types.LSLInteger(45) + -new LSL_Types.LSLInteger(20) + x + new LSL_Types.LSLInteger(23) + -new LSL_Types.LSLInteger(1) + x + y;\n" +
182 " }\n";
183
184 CSCodeGenerator cg = new CSCodeGenerator();
185 string output = cg.Convert(input);
186 Assert.AreEqual(expected, output);
187 }
188
189 [Test]
190 public void TestStrings()
191 {
192 string input = @"default
193{
194 touch_start(integer num_detected)
195 {
196 llOwnerSay(""Testing, 1, 2, 3"");
197 llSay(0, ""I can hear you!"");
198 some_custom_function(1, 2, 3 +x, 4, ""five"", ""arguments"");
199 }
200}
201";
202 string expected =
203 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
204 "\n {" +
205 "\n llOwnerSay(new LSL_Types.LSLString(\"Testing, 1, 2, 3\"));" +
206 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"I can hear you!\"));" +
207 "\n some_custom_function(new LSL_Types.LSLInteger(1), new LSL_Types.LSLInteger(2), new LSL_Types.LSLInteger(3) + x, new LSL_Types.LSLInteger(4), new LSL_Types.LSLString(\"five\"), new LSL_Types.LSLString(\"arguments\"));" +
208 "\n }" +
209 "\n";
210
211 CSCodeGenerator cg = new CSCodeGenerator();
212 string output = cg.Convert(input);
213 Assert.AreEqual(expected, output);
214 }
215
216 [Test]
217 public void TestBinaryExpression()
218 {
219 string input = @"default
220{
221 touch_start(integer num_detected)
222 {
223 integer y;
224 integer x = 14 + 6;
225 y = 12 - 3;
226 y = 12 * 3;
227 y = 12 / 3;
228 y = 12 | 3;
229 y = 12 & 3;
230 y = 12 % 3;
231 y = 12 + 45 - 20 * x / 23 | 1 & x + y;
232 }
233}
234";
235 string expected =
236 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
237 "\n {" +
238 "\n LSL_Types.LSLInteger y = 0;" +
239 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);" +
240 "\n y = new LSL_Types.LSLInteger(12) - new LSL_Types.LSLInteger(3);" +
241 "\n y = new LSL_Types.LSLInteger(12) * new LSL_Types.LSLInteger(3);" +
242 "\n y = new LSL_Types.LSLInteger(12) / new LSL_Types.LSLInteger(3);" +
243 "\n y = new LSL_Types.LSLInteger(12) | new LSL_Types.LSLInteger(3);" +
244 "\n y = new LSL_Types.LSLInteger(12) & new LSL_Types.LSLInteger(3);" +
245 "\n y = new LSL_Types.LSLInteger(12) % new LSL_Types.LSLInteger(3);" +
246 "\n y = new LSL_Types.LSLInteger(12) + new LSL_Types.LSLInteger(45) - new LSL_Types.LSLInteger(20) * x / new LSL_Types.LSLInteger(23) | new LSL_Types.LSLInteger(1) & x + y;" +
247 "\n }\n";
248
249 CSCodeGenerator cg = new CSCodeGenerator();
250 string output = cg.Convert(input);
251 Assert.AreEqual(expected, output);
252 }
253
254 [Test]
255 public void TestFloatConstants()
256 {
257 string input = @"default
258{
259 touch_start(integer num_detected)
260 {
261 float y = 1.1;
262 y = 1.123E3;
263 y = 1.123e3;
264 y = 1.123E+3;
265 y = 1.123e+3;
266 y = 1.123E-3;
267 y = 1.123e-3;
268 y = .4;
269 y = -1.123E3;
270 y = -1.123e3;
271 y = -1.123E+3;
272 y = -1.123e+3;
273 y = -1.123E-3;
274 y = -1.123e-3;
275 y = -.4;
276 y = 12.3 + -1.45E3 - 1.20e-2;
277 }
278}
279";
280 string expected =
281 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
282 "\n {" +
283 "\n LSL_Types.LSLFloat y = new LSL_Types.LSLFloat(1.1);" +
284 "\n y = new LSL_Types.LSLFloat(1.123E3);" +
285 "\n y = new LSL_Types.LSLFloat(1.123e3);" +
286 "\n y = new LSL_Types.LSLFloat(1.123E+3);" +
287 "\n y = new LSL_Types.LSLFloat(1.123e+3);" +
288 "\n y = new LSL_Types.LSLFloat(1.123E-3);" +
289 "\n y = new LSL_Types.LSLFloat(1.123e-3);" +
290 "\n y = new LSL_Types.LSLFloat(.4);" +
291 "\n y = -new LSL_Types.LSLFloat(1.123E3);" +
292 "\n y = -new LSL_Types.LSLFloat(1.123e3);" +
293 "\n y = -new LSL_Types.LSLFloat(1.123E+3);" +
294 "\n y = -new LSL_Types.LSLFloat(1.123e+3);" +
295 "\n y = -new LSL_Types.LSLFloat(1.123E-3);" +
296 "\n y = -new LSL_Types.LSLFloat(1.123e-3);" +
297 "\n y = -new LSL_Types.LSLFloat(.4);" +
298 "\n y = new LSL_Types.LSLFloat(12.3) + -new LSL_Types.LSLFloat(1.45E3) - new LSL_Types.LSLFloat(1.20e-2);" +
299 "\n }\n";
300
301 CSCodeGenerator cg = new CSCodeGenerator();
302 string output = cg.Convert(input);
303 Assert.AreEqual(expected, output);
304 }
305
306 [Test]
307 public void TestComments()
308 {
309 string input = @"// this test tests comments
310default
311{
312 touch_start(integer num_detected) // this should be stripped
313 {
314 // fill in code here...
315 }
316}
317";
318 string expected =
319 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
320 "\n {" +
321 "\n }\n";
322
323 CSCodeGenerator cg = new CSCodeGenerator();
324 string output = cg.Convert(input);
325 Assert.AreEqual(expected, output);
326 }
327
328 [Test]
329 public void TestStringsWithEscapedQuotesAndComments()
330 {
331 string input = @"// this test tests strings, with escaped quotes and comments in strings
332default
333{
334 touch_start(integer num_detected)
335 {
336 string s1 = ""this is a string."";
337 string s2 = ""this is a string ""+""with an escaped \"" inside it."";
338 s1 = s2+"" and this ""+""is a string with // comments."";
339
340 string onemore = ""[\^@]"";
341
342 string multiline = ""Good evening Sir,
343 my name is Steve.
344 I come from a rough area.
345 I used to be addicted to crack
346 but now I am off it and trying to stay clean.
347 That is why I am selling magazine subscriptions.""; // http://www.imdb.com/title/tt0151804/quotes
348 }
349}
350";
351
352 string expected =
353 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
354 "\n {" +
355 "\n LSL_Types.LSLString s1 = new LSL_Types.LSLString(\"this is a string.\");" +
356 "\n LSL_Types.LSLString s2 = new LSL_Types.LSLString(\"this is a string \") + new LSL_Types.LSLString(\"with an escaped \\\" inside it.\");" +
357 "\n s1 = s2 + new LSL_Types.LSLString(\" and this \") + new LSL_Types.LSLString(\"is a string with // comments.\");" +
358 "\n LSL_Types.LSLString onemore = new LSL_Types.LSLString(\"[\\^@]\");" +
359 "\n LSL_Types.LSLString multiline = new LSL_Types.LSLString(\"Good evening Sir,\\n my name is Steve.\\n I come from a rough area.\\n I used to be addicted to crack\\n but now I am off it and trying to stay clean.\\n That is why I am selling magazine subscriptions.\");" +
360 "\n }\n";
361
362 CSCodeGenerator cg = new CSCodeGenerator();
363 string output = cg.Convert(input);
364 Assert.AreEqual(expected, output);
365 }
366
367 [Test]
368 public void TestGlobalDefinedFunctions()
369 {
370 string input = @"// this test tests custom defined functions
371
372string onefunc()
373{
374 return ""Hi from onefunc()!"";
375}
376
377twofunc(string s)
378{
379 llSay(1000, s);
380}
381
382default
383{
384 touch_start(integer num_detected)
385 {
386 llSay(2000, onefunc());
387 twofunc();
388 }
389}
390";
391 string expected =
392 "\n LSL_Types.LSLString onefunc()" +
393 "\n {" +
394 "\n return new LSL_Types.LSLString(\"Hi from onefunc()!\");" +
395 "\n }" +
396 "\n void twofunc(LSL_Types.LSLString s)" +
397 "\n {" +
398 "\n llSay(new LSL_Types.LSLInteger(1000), s);" +
399 "\n }" +
400 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
401 "\n {" +
402 "\n llSay(new LSL_Types.LSLInteger(2000), onefunc());" +
403 "\n twofunc();" +
404 "\n }\n";
405
406 CSCodeGenerator cg = new CSCodeGenerator();
407 string output = cg.Convert(input);
408 Assert.AreEqual(expected, output);
409 }
410
411 [Test]
412 public void TestGlobalDeclaredVariables()
413 {
414 string input = @"// this test tests custom defined functions and global variables
415
416string globalString;
417integer globalInt = 14;
418integer anotherGlobal = 20 * globalInt;
419
420string onefunc()
421{
422 globalString = "" ...and the global!"";
423 return ""Hi "" +
424 ""from "" +
425 ""onefunc()!"" + globalString;
426}
427
428twofunc(string s)
429{
430 llSay(1000, s);
431}
432
433default
434{
435 touch_start(integer num_detected)
436 {
437 llSay(2000, onefunc());
438 twofunc();
439 }
440}
441";
442 string expected =
443 "\n LSL_Types.LSLString globalString = \"\";" +
444 "\n LSL_Types.LSLInteger globalInt = new LSL_Types.LSLInteger(14);" +
445 "\n LSL_Types.LSLInteger anotherGlobal = new LSL_Types.LSLInteger(20) * globalInt;" +
446 "\n LSL_Types.LSLString onefunc()" +
447 "\n {" +
448 "\n globalString = new LSL_Types.LSLString(\" ...and the global!\");" +
449 "\n return new LSL_Types.LSLString(\"Hi \") + new LSL_Types.LSLString(\"from \") + new LSL_Types.LSLString(\"onefunc()!\") + globalString;" +
450 "\n }" +
451 "\n void twofunc(LSL_Types.LSLString s)" +
452 "\n {" +
453 "\n llSay(new LSL_Types.LSLInteger(1000), s);" +
454 "\n }" +
455 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
456 "\n {" +
457 "\n llSay(new LSL_Types.LSLInteger(2000), onefunc());" +
458 "\n twofunc();" +
459 "\n }\n";
460
461 CSCodeGenerator cg = new CSCodeGenerator();
462 string output = cg.Convert(input);
463 Assert.AreEqual(expected, output);
464 }
465
466 [Test]
467 public void TestMoreAssignments()
468 {
469 string input = @"// this test tests +=, -=, *=, /=, %=
470
471string globalString;
472integer globalInt = 14;
473
474string onefunc(string addition)
475{
476 globalInt -= 2;
477
478 globalString += addition;
479 return ""Hi "" +
480 ""from "" +
481 ""onefunc()! "" + globalString;
482}
483
484default
485{
486 touch_start(integer num_detected)
487 {
488 llSay(2000, onefunc());
489
490 integer x = 2;
491 x *= 3;
492 x /= 14 + -2;
493 x %= 10;
494 }
495}
496";
497 string expected =
498 "\n LSL_Types.LSLString globalString = \"\";" +
499 "\n LSL_Types.LSLInteger globalInt = new LSL_Types.LSLInteger(14);" +
500 "\n LSL_Types.LSLString onefunc(LSL_Types.LSLString addition)" +
501 "\n {" +
502 "\n globalInt -= new LSL_Types.LSLInteger(2);" +
503 "\n globalString += addition;" +
504 "\n return new LSL_Types.LSLString(\"Hi \") + new LSL_Types.LSLString(\"from \") + new LSL_Types.LSLString(\"onefunc()! \") + globalString;" +
505 "\n }" +
506 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
507 "\n {" +
508 "\n llSay(new LSL_Types.LSLInteger(2000), onefunc());" +
509 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(2);" +
510 "\n x *= new LSL_Types.LSLInteger(3);" +
511 "\n x /= new LSL_Types.LSLInteger(14) + -new LSL_Types.LSLInteger(2);" +
512 "\n x %= new LSL_Types.LSLInteger(10);" +
513 "\n }\n";
514
515 CSCodeGenerator cg = new CSCodeGenerator();
516 string output = cg.Convert(input);
517 Assert.AreEqual(expected, output);
518 }
519
520 [Test]
521 public void TestVectorConstantNotation()
522 {
523 string input = @"default
524{
525 touch_start(integer num_detected)
526 {
527 vector y = <1.2, llGetMeAFloat(), 4.4>;
528 rotation x = <0.1, 0.1, one + 2, 0.9>;
529
530 y = <0.1, 0.1, 1.1 - three - two+eight*8>;
531 }
532}
533";
534 string expected =
535 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
536 "\n {" +
537 "\n LSL_Types.Vector3 y = new LSL_Types.Vector3(new LSL_Types.LSLFloat(1.2), llGetMeAFloat(), new LSL_Types.LSLFloat(4.4));" +
538 "\n LSL_Types.Quaternion x = new LSL_Types.Quaternion(new LSL_Types.LSLFloat(0.1), new LSL_Types.LSLFloat(0.1), one + new LSL_Types.LSLInteger(2), new LSL_Types.LSLFloat(0.9));" +
539 "\n y = new LSL_Types.Vector3(new LSL_Types.LSLFloat(0.1), new LSL_Types.LSLFloat(0.1), new LSL_Types.LSLFloat(1.1) - three - two + eight * new LSL_Types.LSLInteger(8));" +
540 "\n }\n";
541
542 CSCodeGenerator cg = new CSCodeGenerator();
543 string output = cg.Convert(input);
544 Assert.AreEqual(expected, output);
545 }
546
547 [Test]
548 public void TestVectorMemberAccess()
549 {
550 string input = @"default
551{
552 touch_start(integer num_detected)
553 {
554 vector y = <1.2, llGetMeAFloat(), 4.4>;
555 x = y.x + 1.1;
556 y.x = 1.1;
557 }
558}
559";
560 string expected =
561 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
562 "\n {" +
563 "\n LSL_Types.Vector3 y = new LSL_Types.Vector3(new LSL_Types.LSLFloat(1.2), llGetMeAFloat(), new LSL_Types.LSLFloat(4.4));" +
564 "\n x = y.x + new LSL_Types.LSLFloat(1.1);" +
565 "\n y.x = new LSL_Types.LSLFloat(1.1);" +
566 "\n }\n";
567
568 CSCodeGenerator cg = new CSCodeGenerator();
569 string output = cg.Convert(input);
570 Assert.AreEqual(expected, output);
571 }
572
573 [Test]
574 public void TestExpressionInParentheses()
575 {
576 string input = @"default
577{
578 touch_start(integer num_detected)
579 {
580 integer y = -3;
581 integer x = 14 + 6;
582 y = 12 +45+20+x + (23 + 1) + x + y;
583 y = (12 + -45 + -20 + x + 23 )+ -1 + x + y;
584 }
585}
586";
587 string expected =
588 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
589 "\n {" +
590 "\n LSL_Types.LSLInteger y = -new LSL_Types.LSLInteger(3);" +
591 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);" +
592 "\n y = new LSL_Types.LSLInteger(12) + new LSL_Types.LSLInteger(45) + new LSL_Types.LSLInteger(20) + x + (new LSL_Types.LSLInteger(23) + new LSL_Types.LSLInteger(1)) + x + y;" +
593 "\n y = (new LSL_Types.LSLInteger(12) + -new LSL_Types.LSLInteger(45) + -new LSL_Types.LSLInteger(20) + x + new LSL_Types.LSLInteger(23)) + -new LSL_Types.LSLInteger(1) + x + y;" +
594 "\n }\n";
595
596 CSCodeGenerator cg = new CSCodeGenerator();
597 string output = cg.Convert(input);
598 Assert.AreEqual(expected, output);
599 }
600
601 [Test]
602 public void TestIncrementDecrementOperator()
603 {
604 string input = @"// here we'll test the ++ and -- operators
605
606default
607{
608 touch_start(integer num_detected)
609 {
610 integer y = -3;
611 integer x = 14 + 6;
612 y = 12 +45+20+x++ + (23 + 1) + ++x + -- y;
613 y = (12 + -45 + -20 + x-- + 23 )+ -1 + x -- + ++y;
614 }
615}
616";
617 string expected =
618 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
619 "\n {" +
620 "\n LSL_Types.LSLInteger y = -new LSL_Types.LSLInteger(3);" +
621 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);" +
622 "\n y = new LSL_Types.LSLInteger(12) + new LSL_Types.LSLInteger(45) + new LSL_Types.LSLInteger(20) + x++ + (new LSL_Types.LSLInteger(23) + new LSL_Types.LSLInteger(1)) + ++x + --y;" +
623 "\n y = (new LSL_Types.LSLInteger(12) + -new LSL_Types.LSLInteger(45) + -new LSL_Types.LSLInteger(20) + x-- + new LSL_Types.LSLInteger(23)) + -new LSL_Types.LSLInteger(1) + x-- + ++y;" +
624 "\n }\n";
625
626 CSCodeGenerator cg = new CSCodeGenerator();
627 string output = cg.Convert(input);
628 Assert.AreEqual(expected, output);
629 }
630
631 [Test]
632 public void TestLists()
633 {
634 string input = @"// testing lists
635
636default
637{
638 touch_start(integer num_detected)
639 {
640 list l = [];
641 list m = [1, two, ""three"", <4.0, 4.0, 4.0>, 5 + 5];
642 llCallSomeFunc(1, llAnotherFunc(), [1, 2, 3]);
643 }
644}
645";
646 string expected =
647 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
648 "\n {" +
649 "\n LSL_Types.list l = new LSL_Types.list();" +
650 "\n LSL_Types.list m = new LSL_Types.list(new LSL_Types.LSLInteger(1), two, new LSL_Types.LSLString(\"three\"), new LSL_Types.Vector3(new LSL_Types.LSLFloat(4.0), new LSL_Types.LSLFloat(4.0), new LSL_Types.LSLFloat(4.0)), new LSL_Types.LSLInteger(5) + new LSL_Types.LSLInteger(5));" +
651 "\n llCallSomeFunc(new LSL_Types.LSLInteger(1), llAnotherFunc(), new LSL_Types.list(new LSL_Types.LSLInteger(1), new LSL_Types.LSLInteger(2), new LSL_Types.LSLInteger(3)));" +
652 "\n }\n";
653
654 CSCodeGenerator cg = new CSCodeGenerator();
655 string output = cg.Convert(input);
656 Assert.AreEqual(expected, output);
657 }
658
659 [Test]
660 public void TestIfStatement()
661 {
662 string input = @"// let's test if statements
663
664default
665{
666 touch_start(integer num_detected)
667 {
668 integer x = 1;
669
670 if (x) llSay(0, ""Hello"");
671 if (1)
672 {
673 llSay(0, ""Hi"");
674 integer r = 3;
675 return;
676 }
677
678 if (f(x)) llSay(0, ""f(x) is true"");
679 else llSay(0, ""f(x) is false"");
680
681 if (x + y) llSay(0, ""x + y is true"");
682 else if (y - x) llSay(0, ""y - x is true"");
683 else llSay(0, ""Who needs x and y anyway?"");
684
685 if (x * y) llSay(0, ""x * y is true"");
686 else if (y / x)
687 {
688 llSay(0, ""uh-oh, y / x is true, exiting"");
689 return;
690 }
691 else llSay(0, ""Who needs x and y anyway?"");
692
693 // and now for my last trick
694 if (x % y) llSay(0, ""x is true"");
695 else if (y & x) llSay(0, ""y is true"");
696 else if (z | x) llSay(0, ""z is true"");
697 else if (a * (b + x)) llSay(0, ""a is true"");
698 else if (b) llSay(0, ""b is true"");
699 else if (v) llSay(0, ""v is true"");
700 else llSay(0, ""Everything is lies!"");
701 }
702}
703";
704 string expected =
705 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
706 "\n {" +
707 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
708 "\n if (x)" +
709 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hello\"));" +
710 "\n if (new LSL_Types.LSLInteger(1))" +
711 "\n {" +
712 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hi\"));" +
713 "\n LSL_Types.LSLInteger r = new LSL_Types.LSLInteger(3);" +
714 "\n return ;" +
715 "\n }" +
716 "\n if (f(x))" +
717 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"f(x) is true\"));" +
718 "\n else" +
719 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"f(x) is false\"));" +
720 "\n if (x + y)" +
721 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x + y is true\"));" +
722 "\n else" +
723 "\n if (y - x)" +
724 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y - x is true\"));" +
725 "\n else" +
726 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
727 "\n if (x * y)" +
728 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x * y is true\"));" +
729 "\n else" +
730 "\n if (y / x)" +
731 "\n {" +
732 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"uh-oh, y / x is true, exiting\"));" +
733 "\n return ;" +
734 "\n }" +
735 "\n else" +
736 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
737 "\n if (x % y)" +
738 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
739 "\n else" +
740 "\n if (y & x)" +
741 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y is true\"));" +
742 "\n else" +
743 "\n if (z | x)" +
744 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"z is true\"));" +
745 "\n else" +
746 "\n if (a * (b + x))" +
747 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"a is true\"));" +
748 "\n else" +
749 "\n if (b)" +
750 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"b is true\"));" +
751 "\n else" +
752 "\n if (v)" +
753 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"v is true\"));" +
754 "\n else" +
755 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Everything is lies!\"));" +
756 "\n }\n";
757
758 CSCodeGenerator cg = new CSCodeGenerator();
759 string output = cg.Convert(input);
760 Assert.AreEqual(expected, output);
761 }
762
763 [Test]
764 public void TestIfElseStatement()
765 {
766 string input = @"// let's test complex logical expressions
767
768default
769{
770 touch_start(integer num_detected)
771 {
772 integer x = 1;
773 integer y = 0;
774
775 if (x && y) llSay(0, ""Hello"");
776 if (x || y)
777 {
778 llSay(0, ""Hi"");
779 integer r = 3;
780 return;
781 }
782
783 if (x && y || z) llSay(0, ""x is true"");
784 else llSay(0, ""x is false"");
785
786 if (x == y) llSay(0, ""x is true"");
787 else if (y < x) llSay(0, ""y is true"");
788 else llSay(0, ""Who needs x and y anyway?"");
789
790 if (x > y) llSay(0, ""x is true"");
791 else if (y <= x)
792 {
793 llSay(0, ""uh-oh, y is true, exiting"");
794 return;
795 }
796 else llSay(0, ""Who needs x and y anyway?"");
797
798 // and now for my last trick
799 if (x >= y) llSay(0, ""x is true"");
800 else if (y != x) llSay(0, ""y is true"");
801 else if (!z) llSay(0, ""z is true"");
802 else if (!(a && b)) llSay(0, ""a is true"");
803 else if (b) llSay(0, ""b is true"");
804 else if (v) llSay(0, ""v is true"");
805 else llSay(0, ""Everything is lies!"");
806 }
807}
808";
809 string expected =
810 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
811 "\n {" +
812 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
813 "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
814 "\n if (x && y)" +
815 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hello\"));" +
816 "\n if (x || y)" +
817 "\n {" +
818 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hi\"));" +
819 "\n LSL_Types.LSLInteger r = new LSL_Types.LSLInteger(3);" +
820 "\n return ;" +
821 "\n }" +
822 "\n if (x && y || z)" +
823 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
824 "\n else" +
825 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is false\"));" +
826 "\n if (x == y)" +
827 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
828 "\n else" +
829 "\n if (y < x)" +
830 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y is true\"));" +
831 "\n else" +
832 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
833 "\n if (x > y)" +
834 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
835 "\n else" +
836 "\n if (y <= x)" +
837 "\n {" +
838 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"uh-oh, y is true, exiting\"));" +
839 "\n return ;" +
840 "\n }" +
841 "\n else" +
842 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
843 "\n if (x >= y)" +
844 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
845 "\n else" +
846 "\n if (y != x)" +
847 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y is true\"));" +
848 "\n else" +
849 "\n if (!z)" +
850 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"z is true\"));" +
851 "\n else" +
852 "\n if (!(a && b))" +
853 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"a is true\"));" +
854 "\n else" +
855 "\n if (b)" +
856 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"b is true\"));" +
857 "\n else" +
858 "\n if (v)" +
859 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"v is true\"));" +
860 "\n else" +
861 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Everything is lies!\"));" +
862 "\n }\n";
863
864 CSCodeGenerator cg = new CSCodeGenerator();
865 string output = cg.Convert(input);
866 Assert.AreEqual(expected, output);
867 }
868
869 [Test]
870 public void TestWhileLoop()
871 {
872 string input = @"// let's test while loops
873
874default
875{
876 touch_start(integer num_detected)
877 {
878 integer x = 1;
879 integer y = 0;
880
881 while (x) llSay(0, ""To infinity, and beyond!"");
882 while (0 || (x && 0))
883 {
884 llSay(0, ""Never say never."");
885 return;
886 }
887 }
888}
889";
890 string expected =
891 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
892 "\n {" +
893 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
894 "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
895 "\n while (x)" +
896 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"To infinity, and beyond!\"));" +
897 "\n while (new LSL_Types.LSLInteger(0) || (x && new LSL_Types.LSLInteger(0)))" +
898 "\n {" +
899 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Never say never.\"));" +
900 "\n return ;" +
901 "\n }" +
902 "\n }\n";
903
904 CSCodeGenerator cg = new CSCodeGenerator();
905 string output = cg.Convert(input);
906 Assert.AreEqual(expected, output);
907 }
908
909 [Test]
910 public void TestDoWhileLoop()
911 {
912 string input = @"// let's test do-while loops
913
914default
915{
916 touch_start(integer num_detected)
917 {
918 integer x = 1;
919 integer y = 0;
920
921 do llSay(0, ""And we're doing..."");
922 while (x);
923
924 do
925 {
926 llSay(0, ""I like it here. I wish we could stay here forever."");
927 y--;
928 } while (y);
929 }
930}
931";
932 string expected =
933 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
934 "\n {" +
935 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
936 "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
937 "\n do" +
938 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"And we're doing...\"));" +
939 "\n while (x);" +
940 "\n do" +
941 "\n {" +
942 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"I like it here. I wish we could stay here forever.\"));" +
943 "\n y--;" +
944 "\n }" +
945 "\n while (y);" +
946 "\n }\n";
947
948 CSCodeGenerator cg = new CSCodeGenerator();
949 string output = cg.Convert(input);
950 Assert.AreEqual(expected, output);
951 }
952
953 [Test]
954 public void TestForLoop()
955 {
956 string input = @"// let's test for loops
957
958default
959{
960 touch_start(integer num_detected)
961 {
962 integer x = 1;
963 integer y = 0;
964
965 for (x = 10; x >= 0; x--)
966 {
967 llOwnerSay(""Launch in T minus "" + x);
968 IncreaseRocketPower();
969 }
970
971 for (x = 0, y = 6; y > 0 && x != y; x++, y--) llOwnerSay(""Hi "" + x + "", "" + y);
972 for (x = 0, y = 6; ! y; x++,y--) llOwnerSay(""Hi "" + x + "", "" + y);
973 }
974}
975";
976 string expected =
977 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
978 "\n {" +
979 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
980 "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
981 "\n for (x = new LSL_Types.LSLInteger(10); x >= new LSL_Types.LSLInteger(0); x--)" +
982 "\n {" +
983 "\n llOwnerSay(new LSL_Types.LSLString(\"Launch in T minus \") + x);" +
984 "\n IncreaseRocketPower();" +
985 "\n }" +
986 "\n for (x = new LSL_Types.LSLInteger(0), y = new LSL_Types.LSLInteger(6); y > new LSL_Types.LSLInteger(0) && x != y; x++, y--)" +
987 "\n llOwnerSay(new LSL_Types.LSLString(\"Hi \") + x + new LSL_Types.LSLString(\", \") + y);" +
988 "\n for (x = new LSL_Types.LSLInteger(0), y = new LSL_Types.LSLInteger(6); !y; x++, y--)" +
989 "\n llOwnerSay(new LSL_Types.LSLString(\"Hi \") + x + new LSL_Types.LSLString(\", \") + y);" +
990 "\n }\n";
991
992 CSCodeGenerator cg = new CSCodeGenerator();
993 string output = cg.Convert(input);
994 Assert.AreEqual(expected, output);
995 }
996
997 [Test]
998 public void TestFloatsWithTrailingDecimal()
999 {
1000 string input = @"// a curious feature of LSL that allows floats to be defined with a trailing dot
1001
1002default
1003{
1004 touch_start(integer num_detected)
1005 {
1006 float y = 1.;
1007 y = 1.E3;
1008 y = 1.e3;
1009 y = 1.E+3;
1010 y = 1.e+3;
1011 y = 1.E-3;
1012 y = 1.e-3;
1013 y = -1.E3;
1014 y = -1.e3;
1015 y = -1.E+3;
1016 y = -1.e+3;
1017 y = -1.E-3;
1018 y = -1.e-3;
1019 y = 12. + -1.E3 - 1.e-2;
1020 vector v = <0.,0.,0.>;
1021 }
1022}
1023";
1024 string expected =
1025 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
1026 "\n {" +
1027 "\n LSL_Types.LSLFloat y = new LSL_Types.LSLFloat(1.0);" +
1028 "\n y = new LSL_Types.LSLFloat(1.0E3);" +
1029 "\n y = new LSL_Types.LSLFloat(1.0e3);" +
1030 "\n y = new LSL_Types.LSLFloat(1.0E+3);" +
1031 "\n y = new LSL_Types.LSLFloat(1.0e+3);" +
1032 "\n y = new LSL_Types.LSLFloat(1.0E-3);" +
1033 "\n y = new LSL_Types.LSLFloat(1.0e-3);" +
1034 "\n y = -new LSL_Types.LSLFloat(1.0E3);" +
1035 "\n y = -new LSL_Types.LSLFloat(1.0e3);" +
1036 "\n y = -new LSL_Types.LSLFloat(1.0E+3);" +
1037 "\n y = -new LSL_Types.LSLFloat(1.0e+3);" +
1038 "\n y = -new LSL_Types.LSLFloat(1.0E-3);" +
1039 "\n y = -new LSL_Types.LSLFloat(1.0e-3);" +
1040 "\n y = new LSL_Types.LSLFloat(12.0) + -new LSL_Types.LSLFloat(1.0E3) - new LSL_Types.LSLFloat(1.0e-2);" +
1041 "\n LSL_Types.Vector3 v = new LSL_Types.Vector3(new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0));" +
1042 "\n }\n";
1043
1044 CSCodeGenerator cg = new CSCodeGenerator();
1045 string output = cg.Convert(input);
1046 Assert.AreEqual(expected, output);
1047 }
1048
1049 [Test]
1050 public void TestUnaryAndBinaryOperators()
1051 {
1052 string input = @"// let's test a few more operators
1053
1054default
1055{
1056 touch_start(integer num_detected)
1057 {
1058 integer x = 2;
1059 integer y = 1;
1060 integer z = x ^ y;
1061 x = ~ z;
1062 x = ~(y && z);
1063 y = x >> z;
1064 z = y << x;
1065 }
1066}
1067";
1068 string expected =
1069 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
1070 "\n {" +
1071 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(2);" +
1072 "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(1);" +
1073 "\n LSL_Types.LSLInteger z = x ^ y;" +
1074 "\n x = ~z;" +
1075 "\n x = ~(y && z);" +
1076 "\n y = x >> z;" +
1077 "\n z = y << x;" +
1078 "\n }\n";
1079
1080 CSCodeGenerator cg = new CSCodeGenerator();
1081 string output = cg.Convert(input);
1082 Assert.AreEqual(expected, output);
1083 }
1084
1085 [Test]
1086 public void TestTypecasts()
1087 {
1088 string input = @"// let's test typecasts
1089
1090default
1091{
1092 touch_start(integer num_detected)
1093 {
1094 string s = """";
1095 integer x = 1;
1096
1097 s = (string) x++;
1098 s = (string) x;
1099 s = (string) <0., 0., 0.>;
1100 s = (string) <1., 1., 1., 1.>;
1101 s = (integer) ""1"";
1102 s = (string) llSomethingThatReturnsInteger();
1103 s = (string) 134;
1104 s = (string) (x ^ y | (z && l)) + (string) (x + y - 13);
1105 llOwnerSay(""s is: "" + s);
1106 }
1107}
1108";
1109 string expected =
1110 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
1111 "\n {" +
1112 "\n LSL_Types.LSLString s = new LSL_Types.LSLString(\"\");" +
1113 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
1114 "\n s = (LSL_Types.LSLString) (x++);" +
1115 "\n s = (LSL_Types.LSLString) (x);" +
1116 "\n s = (LSL_Types.LSLString) (new LSL_Types.Vector3(new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0)));" +
1117 "\n s = (LSL_Types.LSLString) (new LSL_Types.Quaternion(new LSL_Types.LSLFloat(1.0), new LSL_Types.LSLFloat(1.0), new LSL_Types.LSLFloat(1.0), new LSL_Types.LSLFloat(1.0)));" +
1118 "\n s = (LSL_Types.LSLInteger) (new LSL_Types.LSLString(\"1\"));" +
1119 "\n s = (LSL_Types.LSLString) (llSomethingThatReturnsInteger());" +
1120 "\n s = (LSL_Types.LSLString) (new LSL_Types.LSLInteger(134));" +
1121 "\n s = (LSL_Types.LSLString) (x ^ y | (z && l)) + (LSL_Types.LSLString) (x + y - new LSL_Types.LSLInteger(13));" +
1122 "\n llOwnerSay(new LSL_Types.LSLString(\"s is: \") + s);" +
1123 "\n }\n";
1124
1125 CSCodeGenerator cg = new CSCodeGenerator();
1126 string output = cg.Convert(input);
1127 Assert.AreEqual(expected, output);
1128 }
1129
1130 [Test]
1131 public void TestStates()
1132 {
1133 string input = @"// let's test states
1134
1135default
1136{
1137 touch_start(integer num_detected)
1138 {
1139 llSay(0, ""Going to state 'statetwo'"");
1140 state statetwo;
1141 }
1142}
1143
1144state statetwo
1145{
1146 state_entry()
1147 {
1148 llSay(0, ""Going to the default state"");
1149 state default;
1150 }
1151}
1152";
1153 string expected =
1154 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
1155 "\n {" +
1156 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Going to state 'statetwo'\"));" +
1157 "\n state(\"statetwo\");" +
1158 "\n }" +
1159 "\n public void statetwo_event_state_entry()" +
1160 "\n {" +
1161 "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Going to the default state\"));" +
1162 "\n state(\"default\");" +
1163 "\n }\n";
1164
1165 CSCodeGenerator cg = new CSCodeGenerator();
1166 string output = cg.Convert(input);
1167 Assert.AreEqual(expected, output);
1168 }
1169
1170 [Test]
1171 public void TestHexIntegerConstants()
1172 {
1173 string input = @"// let's test hex integers
1174
1175default
1176{
1177 touch_start(integer num_detected)
1178 {
1179 integer x = 0x23;
1180 integer x = 0x2f34B;
1181 integer x = 0x2F34b;
1182 integer x = 0x2F34B;
1183 integer x = 0x2f34b;
1184 }
1185}
1186";
1187 string expected =
1188 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
1189 "\n {" +
1190 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x23);" +
1191 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2f34B);" +
1192 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2F34b);" +
1193 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2F34B);" +
1194 "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2f34b);" +
1195 "\n }\n";
1196
1197 CSCodeGenerator cg = new CSCodeGenerator();
1198 string output = cg.Convert(input);
1199 Assert.AreEqual(expected, output);
1200 }
1201
1202 [Test]
1203 public void TestJumps()
1204 {
1205 string input = @"// let's test jumps
1206
1207default
1208{
1209 touch_start(integer num_detected)
1210 {
1211 jump here;
1212 llOwnerSay(""Uh oh, the jump didn't work"");
1213 @here;
1214 llOwnerSay(""After the jump"");
1215 }
1216}
1217";
1218 string expected =
1219 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
1220 "\n {" +
1221 "\n goto here;" +
1222 "\n llOwnerSay(new LSL_Types.LSLString(\"Uh oh, the jump didn't work\"));" +
1223 "\n here:" +
1224 "\n llOwnerSay(new LSL_Types.LSLString(\"After the jump\"));" +
1225 "\n }\n";
1226
1227 CSCodeGenerator cg = new CSCodeGenerator();
1228 string output = cg.Convert(input);
1229 Assert.AreEqual(expected, output);
1230 }
1231
1232 [Test]
1233 public void TestImplicitVariableInitialization()
1234 {
1235 string input = @"// let's test implicitly initializing variables
1236
1237default
1238{
1239 touch_start(integer num_detected)
1240 {
1241 integer i; integer j = 14;
1242 float f; float g = 14.0;
1243 string s; string t = ""Hi there"";
1244 list l; list m = [1, 2, 3];
1245 vector v; vector w = <1.0, 0.1, 0.5>;
1246 rotation r; rotation u = <0.8, 0.7, 0.6, llSomeFunc()>;
1247 key k; key n = ""ping"";
1248 }
1249}
1250";
1251 string expected =
1252 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
1253 "\n {" +
1254 "\n LSL_Types.LSLInteger i = 0;" +
1255 "\n LSL_Types.LSLInteger j = new LSL_Types.LSLInteger(14);" +
1256 "\n LSL_Types.LSLFloat f = 0.0;" +
1257 "\n LSL_Types.LSLFloat g = new LSL_Types.LSLFloat(14.0);" +
1258 "\n LSL_Types.LSLString s = \"\";" +
1259 "\n LSL_Types.LSLString t = new LSL_Types.LSLString(\"Hi there\");" +
1260 "\n LSL_Types.list l = new LSL_Types.list();" +
1261 "\n LSL_Types.list m = new LSL_Types.list(new LSL_Types.LSLInteger(1), new LSL_Types.LSLInteger(2), new LSL_Types.LSLInteger(3));" +
1262 "\n LSL_Types.Vector3 v = new LSL_Types.Vector3(0.0, 0.0, 0.0);" +
1263 "\n LSL_Types.Vector3 w = new LSL_Types.Vector3(new LSL_Types.LSLFloat(1.0), new LSL_Types.LSLFloat(0.1), new LSL_Types.LSLFloat(0.5));" +
1264 "\n LSL_Types.Quaternion r = new LSL_Types.Quaternion(0.0, 0.0, 0.0, 0.0);" +
1265 "\n LSL_Types.Quaternion u = new LSL_Types.Quaternion(new LSL_Types.LSLFloat(0.8), new LSL_Types.LSLFloat(0.7), new LSL_Types.LSLFloat(0.6), llSomeFunc());" +
1266 "\n LSL_Types.LSLString k = \"\";" +
1267 "\n LSL_Types.LSLString n = new LSL_Types.LSLString(\"ping\");" +
1268 "\n }\n";
1269
1270 CSCodeGenerator cg = new CSCodeGenerator();
1271 string output = cg.Convert(input);
1272 Assert.AreEqual(expected, output);
1273 }
1274
1275 [Test]
1276 public void TestMultipleEqualsExpression()
1277 {
1278 string input = @"// let's test x = y = 5 type expressions
1279
1280default
1281{
1282 touch_start(integer num_detected)
1283 {
1284 integer x;
1285 integer y;
1286 x = y = 5;
1287 x += y -= 5;
1288 llOwnerSay(""x is: "" + (string) x + "", y is: "" + (string) y);
1289 }
1290}
1291";
1292 string expected =
1293 "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
1294 "\n {" +
1295 "\n LSL_Types.LSLInteger x = 0;" +
1296 "\n LSL_Types.LSLInteger y = 0;" +
1297 "\n x = y = new LSL_Types.LSLInteger(5);" +
1298 "\n x += y -= new LSL_Types.LSLInteger(5);" +
1299 "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x) + new LSL_Types.LSLString(\", y is: \") + (LSL_Types.LSLString) (y));" +
1300 "\n }\n";
1301
1302 CSCodeGenerator cg = new CSCodeGenerator();
1303 string output = cg.Convert(input);
1304 Assert.AreEqual(expected, output);
1305 }
1306
1307 [Test]
1308 public void TestUnaryExpressionLastInVectorConstant()
1309 {
1310 string input = @"// let's test unary expressions some more
1311
1312default
1313{
1314 state_entry()
1315 {
1316 vector v = <x,y,-0.5>;
1317 }
1318}
1319";
1320 string expected =
1321 "\n public void default_event_state_entry()" +
1322 "\n {" +
1323 "\n LSL_Types.Vector3 v = new LSL_Types.Vector3(x, y, -new LSL_Types.LSLFloat(0.5));" +
1324 "\n }\n";
1325
1326 CSCodeGenerator cg = new CSCodeGenerator();
1327 string output = cg.Convert(input);
1328 Assert.AreEqual(expected, output);
1329 }
1330
1331 [Test]
1332 public void TestVectorMemberPlusEquals()
1333 {
1334 string input = @"// let's test unary expressions some more
1335
1336default
1337{
1338 state_entry()
1339 {
1340 vector v = llGetPos();
1341 v.z += 4;
1342 v.z -= 4;
1343 v.z *= 4;
1344 v.z /= 4;
1345 v.z %= 4;
1346 }
1347}
1348";
1349 string expected =
1350 "\n public void default_event_state_entry()" +
1351 "\n {" +
1352 "\n LSL_Types.Vector3 v = llGetPos();" +
1353 "\n v.z += new LSL_Types.LSLInteger(4);" +
1354 "\n v.z -= new LSL_Types.LSLInteger(4);" +
1355 "\n v.z *= new LSL_Types.LSLInteger(4);" +
1356 "\n v.z /= new LSL_Types.LSLInteger(4);" +
1357 "\n v.z %= new LSL_Types.LSLInteger(4);" +
1358 "\n }\n";
1359
1360 CSCodeGenerator cg = new CSCodeGenerator();
1361 string output = cg.Convert(input);
1362 Assert.AreEqual(expected, output);
1363 }
1364
1365 [Test]
1366 public void TestWhileLoopWithNoBody()
1367 {
1368 string input = @"default
1369{
1370 state_entry()
1371 {
1372 while (1<0);
1373 }
1374}";
1375
1376 string expected =
1377 "\n public void default_event_state_entry()" +
1378 "\n {" +
1379 "\n while (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))" +
1380 "\n ;" +
1381 "\n }\n";
1382
1383 CSCodeGenerator cg = new CSCodeGenerator();
1384 string output = cg.Convert(input);
1385 Assert.AreEqual(expected, output);
1386 }
1387
1388 [Test]
1389 public void TestDoWhileLoopWithNoBody()
1390 {
1391 string input = @"default
1392{
1393 state_entry()
1394 {
1395 do;
1396 while (1<0);
1397 }
1398}";
1399
1400 string expected =
1401 "\n public void default_event_state_entry()" +
1402 "\n {" +
1403 "\n do" +
1404 "\n ;" +
1405 "\n while (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0));" +
1406 "\n }\n";
1407
1408 CSCodeGenerator cg = new CSCodeGenerator();
1409 string output = cg.Convert(input);
1410 Assert.AreEqual(expected, output);
1411 }
1412
1413 [Test]
1414 public void TestIfWithNoBody()
1415 {
1416 string input = @"default
1417{
1418 state_entry()
1419 {
1420 if (1<0);
1421 }
1422}";
1423
1424 string expected =
1425 "\n public void default_event_state_entry()" +
1426 "\n {" +
1427 "\n if (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))" +
1428 "\n ;" +
1429 "\n }\n";
1430
1431 CSCodeGenerator cg = new CSCodeGenerator();
1432 string output = cg.Convert(input);
1433 Assert.AreEqual(expected, output);
1434 }
1435
1436 [Test]
1437 public void TestIfElseWithNoBody()
1438 {
1439 string input = @"default
1440{
1441 state_entry()
1442 {
1443 if (1<0);
1444 else;
1445 }
1446}";
1447
1448 string expected =
1449 "\n public void default_event_state_entry()" +
1450 "\n {" +
1451 "\n if (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))" +
1452 "\n ;" +
1453 "\n else" +
1454 "\n ;" +
1455 "\n }\n";
1456
1457 CSCodeGenerator cg = new CSCodeGenerator();
1458 string output = cg.Convert(input);
1459 Assert.AreEqual(expected, output);
1460 }
1461
1462 [Test]
1463 public void TestForLoopWithNoBody()
1464 {
1465 string input = @"default
1466{
1467 state_entry()
1468 {
1469 for (x = 4; 1<0; x += 2);
1470 }
1471}";
1472
1473 string expected =
1474 "\n public void default_event_state_entry()" +
1475 "\n {" +
1476 "\n for (x = new LSL_Types.LSLInteger(4); new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0); x += new LSL_Types.LSLInteger(2))" +
1477 "\n ;" +
1478 "\n }\n";
1479
1480 CSCodeGenerator cg = new CSCodeGenerator();
1481 string output = cg.Convert(input);
1482 Assert.AreEqual(expected, output);
1483 }
1484
1485 [Test]
1486 public void TestAssignmentInIfWhileDoWhile()
1487 {
1488 string input = @"default
1489{
1490 state_entry()
1491 {
1492 integer x;
1493
1494 while (x = 14) llOwnerSay(""x is: "" + (string) x);
1495
1496 if (x = 24) llOwnerSay(""x is: "" + (string) x);
1497
1498 do
1499 llOwnerSay(""x is: "" + (string) x);
1500 while (x = 44);
1501 }
1502}";
1503
1504 string expected =
1505 "\n public void default_event_state_entry()" +
1506 "\n {" +
1507 "\n LSL_Types.LSLInteger x = 0;" +
1508 "\n while (x = new LSL_Types.LSLInteger(14))" +
1509 "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x));" +
1510 "\n if (x = new LSL_Types.LSLInteger(24))" +
1511 "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x));" +
1512 "\n do" +
1513 "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x));" +
1514 "\n while (x = new LSL_Types.LSLInteger(44));" +
1515 "\n }\n";
1516
1517 CSCodeGenerator cg = new CSCodeGenerator();
1518 string output = cg.Convert(input);
1519 Assert.AreEqual(expected, output);
1520 }
1521
1522 [Test]
1523 public void TestLSLListHack()
1524 {
1525 string input = @"default
1526{
1527 state_entry()
1528 {
1529 list l = [""hello""];
1530 l = (l=[]) + l + ""world"";
1531 }
1532}";
1533
1534 string expected =
1535 "\n public void default_event_state_entry()" +
1536 "\n {" +
1537 "\n LSL_Types.list l = new LSL_Types.list(new LSL_Types.LSLString(\"hello\"));" +
1538 "\n l = (l = new LSL_Types.list()) + l + new LSL_Types.LSLString(\"world\");" +
1539 "\n }\n";
1540
1541 CSCodeGenerator cg = new CSCodeGenerator();
1542 string output = cg.Convert(input);
1543 Assert.AreEqual(expected, output);
1544 }
1545
1546 [Test]
1547 [ExpectedException(typeof(Tools.CSToolsException))]
1548 public void TestSyntaxError()
1549 {
1550 string input = @"default
1551{
1552 state_entry()
1553 {
1554 integer y
1555 }
1556}
1557";
1558 try
1559 {
1560 CSCodeGenerator cg = new CSCodeGenerator();
1561 cg.Convert(input);
1562 }
1563 catch (Tools.CSToolsException e)
1564 {
1565 // The syntax error is on line 6, char 5 (expected ';', found
1566 // '}').
1567 Regex r = new Regex("Line ([0-9]+), char ([0-9]+)");
1568 Match m = r.Match(e.Message);
1569 Assert.AreEqual("6", m.Groups[1].Value);
1570 Assert.AreEqual("5", m.Groups[2].Value);
1571
1572 throw;
1573 }
1574 }
1575
1576 [Test]
1577 [ExpectedException(typeof(Tools.CSToolsException))]
1578 public void TestSyntaxErrorDeclaringVariableInForLoop()
1579 {
1580 string input = @"default
1581{
1582 state_entry()
1583 {
1584 for (integer x = 0; x < 10; x++) llOwnerSay(""x is: "" + (string) x);
1585 }
1586}
1587";
1588 try
1589 {
1590 CSCodeGenerator cg = new CSCodeGenerator();
1591 cg.Convert(input);
1592 }
1593 catch (Tools.CSToolsException e)
1594 {
1595 // The syntax error is on line 5, char 14 (Syntax error)
1596 Regex r = new Regex("Line ([0-9]+), char ([0-9]+)");
1597 Match m = r.Match(e.Message);
1598 Assert.AreEqual("5", m.Groups[1].Value);
1599 Assert.AreEqual("14", m.Groups[2].Value);
1600
1601 throw;
1602 }
1603 }
1604 }
1605}
diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs
new file mode 100644
index 0000000..7725d8d
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CompilerTest.cs
@@ -0,0 +1,153 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System.IO;
29using System.CodeDom.Compiler;
30using System.Collections.Generic;
31using Microsoft.CSharp;
32using NUnit.Framework;
33using OpenSim.Region.ScriptEngine.Shared.CodeTools;
34
35namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests
36{
37 /// <summary>
38 /// Tests the LSL compiler. Among other things, test that error messages
39 /// generated by the C# compiler can be mapped to prper lines/columns in
40 /// the LSL source.
41 /// </summary>
42 [TestFixture]
43 public class CompilerTest
44 {
45 private string m_testDir;
46 private CSharpCodeProvider m_CSCodeProvider;
47 private CompilerParameters m_compilerParameters;
48 private CompilerResults m_compilerResults;
49
50 /// <summary>
51 /// Creates a temporary directory where build artifacts are stored.
52 /// </summary>
53 [TestFixtureSetUp]
54 public void Init()
55 {
56 m_testDir = Path.Combine(Path.GetTempPath(), "opensim_compilerTest_" + Path.GetRandomFileName());
57
58 if (!Directory.Exists(m_testDir))
59 {
60 // Create the temporary directory for housing build artifacts.
61 Directory.CreateDirectory(m_testDir);
62 }
63
64 // Create a CSCodeProvider and CompilerParameters.
65 m_CSCodeProvider = new CSharpCodeProvider();
66 m_compilerParameters = new CompilerParameters();
67
68 string rootPath = Path.Combine(Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory), "bin");
69 m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.dll"));
70 m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll"));
71 m_compilerParameters.GenerateExecutable = false;
72 }
73
74 /// <summary>
75 /// Removes the temporary build directory and any build artifacts
76 /// inside it.
77 /// </summary>
78 [TestFixtureTearDown]
79 public void CleanUp()
80 {
81 if (Directory.Exists(m_testDir))
82 {
83 // Blow away the temporary directory with artifacts.
84 Directory.Delete(m_testDir, true);
85 }
86 }
87
88 /// <summary>
89 /// Test the C# compiler error message can be mapped to the correct
90 /// line/column in the LSL source when an undeclared variable is used.
91 /// </summary>
92 //[Test]
93 public void TestUseUndeclaredVariable()
94 {
95 m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");
96
97 string input = @"default
98{
99 state_entry()
100 {
101 integer y = x + 3;
102 }
103}";
104
105 CSCodeGenerator cg = new CSCodeGenerator();
106 string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" +
107 "namespace SecondLife { " +
108 "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" +
109 "public Script() { } " +
110 cg.Convert(input) +
111 "} }\n";
112 Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> positionMap = cg.PositionMap;
113
114 m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output);
115
116 Assert.AreEqual(new KeyValuePair<int, int>(5, 21),
117 positionMap[new KeyValuePair<int, int>(m_compilerResults.Errors[0].Line, m_compilerResults.Errors[0].Column)]);
118 }
119
120 /// <summary>
121 /// Test that a string can be cast to string and another string
122 /// concatenated.
123 /// </summary>
124 //[Test]
125 public void TestCastAndConcatString()
126 {
127 m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");
128
129 string input = @"string s = "" a string"";
130
131default
132{
133 state_entry()
134 {
135 key gAvatarKey = llDetectedKey(0);
136 string tmp = (string) gAvatarKey + s;
137 llSay(0, tmp);
138 }
139}";
140
141 CSCodeGenerator cg = new CSCodeGenerator();
142 string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" +
143 "namespace SecondLife { " +
144 "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" +
145 "public Script() { } " +
146 cg.Convert(input) +
147 "} }\n";
148 m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output);
149
150 Assert.AreEqual(0, m_compilerResults.Errors.Count);
151 }
152 }
153}