ScriptTest.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifdef 0
  23. #ifdef TORQUE_TESTS_ENABLED
  24. #include "testing/unitTesting.h"
  25. #include "platform/platform.h"
  26. #include "console/simBase.h"
  27. #include "console/consoleTypes.h"
  28. #include "console/simBase.h"
  29. #include "console/engineAPI.h"
  30. #include "math/mMath.h"
  31. #include "console/stringStack.h"
  32. template<typename T>
  33. inline T Convert(ConsoleValueRef);
  34. template<>
  35. inline U32 Convert(ConsoleValueRef val)
  36. {
  37. return val.getIntValue();
  38. }
  39. template<>
  40. inline S32 Convert(ConsoleValueRef val)
  41. {
  42. return val.getSignedIntValue();
  43. }
  44. template<>
  45. inline bool Convert(ConsoleValueRef val)
  46. {
  47. return val.getBoolValue();
  48. }
  49. template<>
  50. inline F32 Convert(ConsoleValueRef val)
  51. {
  52. return val.getFloatValue();
  53. }
  54. template<>
  55. inline const char* Convert(ConsoleValueRef val)
  56. {
  57. return val.getStringValue();
  58. }
  59. template<>
  60. inline SimObject* Convert(ConsoleValueRef val)
  61. {
  62. return Sim::findObject(val);
  63. }
  64. template<typename T>
  65. inline T RunScript(const char* str)
  66. {
  67. return Convert<T>(Con::evaluate(str, false, NULL));
  68. }
  69. TEST(Script, Basic_Arithmetic)
  70. {
  71. S32 add = RunScript<S32>(R"(
  72. return 1.0 + 1;
  73. )");
  74. EXPECT_EQ(add, 2);
  75. S32 sub = RunScript<S32>(R"(
  76. return 10 - 1.0;
  77. )");
  78. EXPECT_EQ(sub, 9);
  79. S32 mult = RunScript<S32>(R"(
  80. return 10 * 2.5;
  81. )");
  82. EXPECT_EQ(mult, 25);
  83. S32 div = RunScript<S32>(R"(
  84. return 10.0 / 2;
  85. )");
  86. EXPECT_EQ(div, 5);
  87. }
  88. TEST(Script, Complex_Arithmetic)
  89. {
  90. S32 result = RunScript<S32>(R"(
  91. return 1 * 2 - (0.5 * 2);
  92. )");
  93. EXPECT_EQ(result, 1);
  94. S32 result2 = RunScript<S32>(R"(
  95. return 1 * 2 * 3 % 2;
  96. )");
  97. EXPECT_EQ(result2, 0);
  98. }
  99. TEST(Script, Basic_Concatination)
  100. {
  101. const char* result1 = RunScript<const char*>(R"(
  102. return "a" @ "b";
  103. )");
  104. EXPECT_STREQ(result1, "ab");
  105. const char* result2 = RunScript<const char*>(R"(
  106. return "a" SPC "b";
  107. )");
  108. EXPECT_STREQ(result2, "a b");
  109. const char* result3 = RunScript<const char*>(R"(
  110. return "a" TAB "b";
  111. )");
  112. EXPECT_STREQ(result3, "a\tb");
  113. const char* result4 = RunScript<const char*>(R"(
  114. return "a" NL "b";
  115. )");
  116. EXPECT_STREQ(result4, "a\nb");
  117. const char* complex = RunScript<const char*>(R"(
  118. return "a" @ "b" @ "c" @ "d";
  119. )");
  120. EXPECT_STREQ(complex, "abcd");
  121. }
  122. TEST(Script, Basic_Global_Variable_Tests)
  123. {
  124. S32 value = RunScript<S32>(R"(
  125. $a = 1;
  126. return $a;
  127. )");
  128. EXPECT_EQ(value, 1);
  129. }
  130. TEST(Script, Variable_Chaining_And_Usage)
  131. {
  132. S32 value = RunScript<S32>(R"(
  133. function t()
  134. {
  135. %a = %b = 2;
  136. return %a;
  137. }
  138. return t();
  139. )");
  140. EXPECT_EQ(value, 2);
  141. S32 valueGlobal = RunScript<S32>(R"(
  142. function t()
  143. {
  144. $a = %b = 2;
  145. }
  146. t();
  147. return $a;
  148. )");
  149. EXPECT_EQ(valueGlobal, 2);
  150. S32 value2 = RunScript<S32>(R"(
  151. function t(%a)
  152. {
  153. if ((%b = 2 * %a) != 5)
  154. return %b;
  155. return 5;
  156. }
  157. return t(2);
  158. )");
  159. EXPECT_EQ(value2, 4);
  160. }
  161. TEST(Script, Basic_Function_Call_And_Local_Variable_Testing)
  162. {
  163. S32 value = RunScript<S32>(R"(
  164. function t() { %a = 2; return %a; }
  165. return t();
  166. )");
  167. EXPECT_EQ(value, 2);
  168. S32 value2 = RunScript<S32>(R"(
  169. function add(%a, %b) { return %a + %b; }
  170. return add(2, 4);
  171. )");
  172. EXPECT_EQ(value2, 6);
  173. S32 value3 = RunScript<S32>(R"(
  174. function fib(%a) {
  175. if (%a == 0)
  176. return 0;
  177. if (%a == 1)
  178. return 1;
  179. return fib(%a - 1) + fib(%a - 2);
  180. }
  181. return fib(15);
  182. )");
  183. EXPECT_EQ(value3, 610);
  184. S32 staticCall = RunScript<S32>(R"(
  185. function SimObject::bar(%a, %b) {
  186. return %a + %b;
  187. }
  188. return SimObject::bar(1, 2);
  189. )");
  190. EXPECT_EQ(staticCall, 3);
  191. }
  192. TEST(Script, Basic_Conditional_Statements)
  193. {
  194. S32 value = RunScript<S32>(R"(
  195. $a = "hello";
  196. if ($a $= "hello")
  197. return 1;
  198. return 2;
  199. )");
  200. EXPECT_EQ(value, 1);
  201. const char* ternaryValue = RunScript<const char*>(R"(
  202. return $a $= "hello" ? "World" : "No U";
  203. )");
  204. EXPECT_STREQ(ternaryValue, "World");
  205. }
  206. TEST(Script, Basic_Loop_Statements)
  207. {
  208. S32 whileValue = RunScript<S32>(R"(
  209. $count = 0;
  210. while ($count < 5)
  211. $count++;
  212. return $count;
  213. )");
  214. EXPECT_EQ(whileValue, 5);
  215. const char* forValue = RunScript<const char*>(R"(
  216. function t(%times)
  217. {
  218. %result = "";
  219. for (%i = 0; %i < %times; %i++)
  220. %result = %result @ "a";
  221. return %result;
  222. }
  223. return t(3);
  224. )");
  225. EXPECT_STREQ(forValue, "aaa");
  226. const char* forIfValue = RunScript<const char*>(R"(
  227. function t() {
  228. %str = "";
  229. for (%i = 0; %i < 5; %i++) {
  230. %loopValue = %i;
  231. if (%str $= "")
  232. %str = %loopValue;
  233. else
  234. %str = %str @ "," SPC %loopValue;
  235. }
  236. return %str;
  237. }
  238. return t();
  239. )");
  240. EXPECT_STREQ(forIfValue, "0, 1, 2, 3, 4");
  241. }
  242. TEST(Script, TorqueScript_Array_Testing)
  243. {
  244. S32 value = RunScript<S32>(R"(
  245. function t(%idx) { %a[idx] = 2; return %a[idx]; }
  246. return t(5);
  247. )");
  248. EXPECT_EQ(value, 2);
  249. S32 value2 = RunScript<S32>(R"(
  250. function t(%idx) { %a[idx, 0] = 2; return %a[idx, 0]; }
  251. return t(5);
  252. )");
  253. EXPECT_EQ(value2, 2);
  254. }
  255. TEST(Script, Basic_SimObject)
  256. {
  257. SimObject* object = RunScript<SimObject*>(R"(
  258. return new SimObject(FudgeCollector) {
  259. fudge = "Chocolate";
  260. };
  261. )");
  262. EXPECT_NE(object, (SimObject*)NULL);
  263. const char* propertyValue = RunScript<const char*>(R"(
  264. return FudgeCollector.fudge;
  265. )");
  266. EXPECT_STREQ(propertyValue, "Chocolate");
  267. const char* funcReturn = RunScript<const char*>(R"(
  268. function SimObject::fooFunc(%this)
  269. {
  270. return "Bar";
  271. }
  272. return FudgeCollector.fooFunc();
  273. )");
  274. EXPECT_STREQ(funcReturn, "Bar");
  275. const char* parentFn = RunScript<const char*>(R"(
  276. new SimObject(Hello);
  277. function SimObject::fooFunc2(%this)
  278. {
  279. return "Bar";
  280. }
  281. function Hello::fooFunc2(%this)
  282. {
  283. %bar = Parent::fooFunc2(%this);
  284. return "Foo" @ %bar;
  285. }
  286. return Hello.fooFunc2();
  287. )");
  288. EXPECT_STREQ(parentFn, "FooBar");
  289. }
  290. TEST(Script, Basic_Package)
  291. {
  292. S32 value = RunScript<S32>(R"(
  293. function a() { return 3; }
  294. package overrides {
  295. function a() { return 5; }
  296. };
  297. return a();
  298. )");
  299. EXPECT_EQ(value, 3);
  300. S32 overrideValue = RunScript<S32>(R"(
  301. activatePackage(overrides);
  302. return a();
  303. )");
  304. EXPECT_EQ(overrideValue, 5);
  305. S32 deactivatedValue = RunScript<S32>(R"(
  306. deactivatePackage(overrides);
  307. return a();
  308. )");
  309. EXPECT_EQ(deactivatedValue, 3);
  310. }
  311. #endif
  312. #endif