LSL_EventTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 OpenSimulator 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. using System;
  28. using System.Collections.Generic;
  29. using System.Text.RegularExpressions;
  30. using NUnit.Framework;
  31. using OpenSim.Region.ScriptEngine.Shared.CodeTools;
  32. using OpenSim.Tests.Common;
  33. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  34. {
  35. public class LSL_EventTests : OpenSimTestCase
  36. {
  37. CSCodeGenerator m_cg = new CSCodeGenerator();
  38. [Test]
  39. public void TestBadEvent()
  40. {
  41. TestHelpers.InMethod();
  42. // TestHelpers.EnableLogging();
  43. TestCompile("default { bad() {} }", true);
  44. }
  45. [Test]
  46. public void TestAttachEvent()
  47. {
  48. TestHelpers.InMethod();
  49. // TestHelpers.EnableLogging();
  50. TestKeyArgEvent("attach");
  51. }
  52. [Test]
  53. public void TestObjectRezEvent()
  54. {
  55. TestHelpers.InMethod();
  56. // TestHelpers.EnableLogging();
  57. TestKeyArgEvent("object_rez");
  58. }
  59. [Test]
  60. public void TestMovingEndEvent()
  61. {
  62. TestHelpers.InMethod();
  63. // TestHelpers.EnableLogging();
  64. TestVoidArgEvent("moving_end");
  65. }
  66. [Test]
  67. public void TestMovingStartEvent()
  68. {
  69. TestHelpers.InMethod();
  70. // TestHelpers.EnableLogging();
  71. TestVoidArgEvent("moving_start");
  72. }
  73. [Test]
  74. public void TestNoSensorEvent()
  75. {
  76. TestHelpers.InMethod();
  77. // TestHelpers.EnableLogging();
  78. TestVoidArgEvent("no_sensor");
  79. }
  80. [Test]
  81. public void TestNotAtRotTargetEvent()
  82. {
  83. TestHelpers.InMethod();
  84. // TestHelpers.EnableLogging();
  85. TestVoidArgEvent("not_at_rot_target");
  86. }
  87. [Test]
  88. public void TestNotAtTargetEvent()
  89. {
  90. TestHelpers.InMethod();
  91. // TestHelpers.EnableLogging();
  92. TestVoidArgEvent("not_at_target");
  93. }
  94. [Test]
  95. public void TestStateEntryEvent()
  96. {
  97. TestHelpers.InMethod();
  98. // TestHelpers.EnableLogging();
  99. TestVoidArgEvent("state_entry");
  100. }
  101. [Test]
  102. public void TestStateExitEvent()
  103. {
  104. TestHelpers.InMethod();
  105. // TestHelpers.EnableLogging();
  106. TestVoidArgEvent("state_exit");
  107. }
  108. [Test]
  109. public void TestTimerEvent()
  110. {
  111. TestHelpers.InMethod();
  112. // TestHelpers.EnableLogging();
  113. TestVoidArgEvent("timer");
  114. }
  115. private void TestVoidArgEvent(string eventName)
  116. {
  117. TestCompile("default { " + eventName + "() {} }", false);
  118. TestCompile("default { " + eventName + "(integer n) {} }", true);
  119. }
  120. [Test]
  121. public void TestChangedEvent()
  122. {
  123. TestHelpers.InMethod();
  124. // TestHelpers.EnableLogging();
  125. TestIntArgEvent("changed");
  126. }
  127. [Test]
  128. public void TestCollisionEvent()
  129. {
  130. TestHelpers.InMethod();
  131. // TestHelpers.EnableLogging();
  132. TestIntArgEvent("collision");
  133. }
  134. [Test]
  135. public void TestCollisionStartEvent()
  136. {
  137. TestHelpers.InMethod();
  138. // TestHelpers.EnableLogging();
  139. TestIntArgEvent("collision_start");
  140. }
  141. [Test]
  142. public void TestCollisionEndEvent()
  143. {
  144. TestHelpers.InMethod();
  145. // TestHelpers.EnableLogging();
  146. TestIntArgEvent("collision_end");
  147. }
  148. [Test]
  149. public void TestOnRezEvent()
  150. {
  151. TestHelpers.InMethod();
  152. // TestHelpers.EnableLogging();
  153. TestIntArgEvent("on_rez");
  154. }
  155. [Test]
  156. public void TestRunTimePermissionsEvent()
  157. {
  158. TestHelpers.InMethod();
  159. // TestHelpers.EnableLogging();
  160. TestIntArgEvent("run_time_permissions");
  161. }
  162. [Test]
  163. public void TestSensorEvent()
  164. {
  165. TestHelpers.InMethod();
  166. // TestHelpers.EnableLogging();
  167. TestIntArgEvent("sensor");
  168. }
  169. [Test]
  170. public void TestTouchEvent()
  171. {
  172. TestHelpers.InMethod();
  173. // TestHelpers.EnableLogging();
  174. TestIntArgEvent("touch");
  175. }
  176. [Test]
  177. public void TestTouchStartEvent()
  178. {
  179. TestHelpers.InMethod();
  180. // TestHelpers.EnableLogging();
  181. TestIntArgEvent("touch_start");
  182. }
  183. [Test]
  184. public void TestTouchEndEvent()
  185. {
  186. TestHelpers.InMethod();
  187. // TestHelpers.EnableLogging();
  188. TestIntArgEvent("touch_end");
  189. }
  190. [Test]
  191. public void TestLandCollisionEvent()
  192. {
  193. TestHelpers.InMethod();
  194. // TestHelpers.EnableLogging();
  195. TestVectorArgEvent("land_collision");
  196. }
  197. [Test]
  198. public void TestLandCollisionStartEvent()
  199. {
  200. TestHelpers.InMethod();
  201. // TestHelpers.EnableLogging();
  202. TestVectorArgEvent("land_collision_start");
  203. }
  204. [Test]
  205. public void TestLandCollisionEndEvent()
  206. {
  207. TestHelpers.InMethod();
  208. // TestHelpers.EnableLogging();
  209. TestVectorArgEvent("land_collision_end");
  210. }
  211. [Test]
  212. public void TestAtRotTargetEvent()
  213. {
  214. TestHelpers.InMethod();
  215. // TestHelpers.EnableLogging();
  216. TestIntRotRotArgEvent("at_rot_target");
  217. }
  218. [Test]
  219. public void TestAtTargetEvent()
  220. {
  221. TestHelpers.InMethod();
  222. // TestHelpers.EnableLogging();
  223. TestIntVecVecArgEvent("at_target");
  224. }
  225. [Test]
  226. public void TestControlEvent()
  227. {
  228. TestHelpers.InMethod();
  229. // TestHelpers.EnableLogging();
  230. TestKeyIntIntArgEvent("control");
  231. }
  232. private void TestIntArgEvent(string eventName)
  233. {
  234. TestCompile("default { " + eventName + "(integer n) {} }", false);
  235. TestCompile("default { " + eventName + "{{}} }", true);
  236. TestCompile("default { " + eventName + "(string s) {{}} }", true);
  237. TestCompile("default { " + eventName + "(integer n, integer o) {{}} }", true);
  238. }
  239. private void TestKeyArgEvent(string eventName)
  240. {
  241. TestCompile("default { " + eventName + "(key k) {} }", false);
  242. TestCompile("default { " + eventName + "{{}} }", true);
  243. TestCompile("default { " + eventName + "(string s) {{}} }", true);
  244. TestCompile("default { " + eventName + "(key k, key l) {{}} }", true);
  245. }
  246. private void TestVectorArgEvent(string eventName)
  247. {
  248. TestCompile("default { " + eventName + "(vector v) {} }", false);
  249. TestCompile("default { " + eventName + "{{}} }", true);
  250. TestCompile("default { " + eventName + "(string s) {{}} }", true);
  251. TestCompile("default { " + eventName + "(vector v, vector w) {{}} }", true);
  252. }
  253. private void TestIntRotRotArgEvent(string eventName)
  254. {
  255. TestCompile("default { " + eventName + "(integer n, rotation r, rotation s) {} }", false);
  256. TestCompile("default { " + eventName + "{{}} }", true);
  257. TestCompile("default { " + eventName + "(string s) {{}} }", true);
  258. TestCompile("default { " + eventName + "(integer n, rotation r, rotation s, rotation t) {{}} }", true);
  259. }
  260. private void TestIntVecVecArgEvent(string eventName)
  261. {
  262. TestCompile("default { " + eventName + "(integer n, vector v, vector w) {} }", false);
  263. TestCompile("default { " + eventName + "{{}} }", true);
  264. TestCompile("default { " + eventName + "(string s) {{}} }", true);
  265. TestCompile("default { " + eventName + "(integer n, vector v, vector w, vector x) {{}} }", true);
  266. }
  267. private void TestKeyIntIntArgEvent(string eventName)
  268. {
  269. TestCompile("default { " + eventName + "(key k, integer n, integer o) {} }", false);
  270. TestCompile("default { " + eventName + "{{}} }", true);
  271. TestCompile("default { " + eventName + "(string s) {{}} }", true);
  272. TestCompile("default { " + eventName + "(key k, integer n, integer o, integer p) {{}} }", true);
  273. }
  274. private void TestCompile(string script, bool expectException)
  275. {
  276. bool gotException = false;
  277. Exception ge = null;
  278. try
  279. {
  280. m_cg.Convert(script);
  281. }
  282. catch (Exception e)
  283. {
  284. gotException = true;
  285. ge = e;
  286. }
  287. Assert.That(
  288. gotException,
  289. Is.EqualTo(expectException),
  290. "Failed on {0}, exception {1}", script, ge != null ? ge.ToString() : "n/a");
  291. }
  292. }
  293. }