ScriptTest.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/platform.h"
  25. #include "console/simBase.h"
  26. #include "console/consoleTypes.h"
  27. #include "console/simBase.h"
  28. #include "console/engineAPI.h"
  29. #include "math/mMath.h"
  30. #include "console/stringStack.h"
  31. template<typename T>
  32. inline T Convert(ConsoleValue&);
  33. template<>
  34. inline U32 Convert(ConsoleValue &val)
  35. {
  36. return val.getInt();
  37. }
  38. template<>
  39. inline S32 Convert(ConsoleValue &val)
  40. {
  41. return val.getInt();
  42. }
  43. template<>
  44. inline bool Convert(ConsoleValue &val)
  45. {
  46. return val.getBool();
  47. }
  48. template<>
  49. inline F32 Convert(ConsoleValue &val)
  50. {
  51. return val.getFloat();
  52. }
  53. template<>
  54. inline const char* Convert(ConsoleValue &val)
  55. {
  56. return val.getString();
  57. }
  58. template<>
  59. inline SimObject* Convert(ConsoleValue &val)
  60. {
  61. return Sim::findObject(val);
  62. }
  63. template<typename T>
  64. inline T RunScript(const char* str)
  65. {
  66. return Convert<T>(std::move(Con::evaluate(str, false, NULL)));
  67. }
  68. TEST(Script, Basic_Arithmetic)
  69. {
  70. S32 add = RunScript<S32>(R"(
  71. return 1.0 + 1;
  72. )");
  73. EXPECT_EQ(add, 2);
  74. S32 sub = RunScript<S32>(R"(
  75. return 10 - 1.0;
  76. )");
  77. EXPECT_EQ(sub, 9);
  78. S32 mult = RunScript<S32>(R"(
  79. return 10 * 2.5;
  80. )");
  81. EXPECT_EQ(mult, 25);
  82. S32 div = RunScript<S32>(R"(
  83. return 10.0 / 2;
  84. )");
  85. EXPECT_EQ(div, 5);
  86. }
  87. TEST(Script, Complex_Arithmetic)
  88. {
  89. S32 result = RunScript<S32>(R"(
  90. return 1 * 2 - (0.5 * 2);
  91. )");
  92. EXPECT_EQ(result, 1);
  93. S32 result2 = RunScript<S32>(R"(
  94. return 1 * 2 * 3 % 2;
  95. )");
  96. EXPECT_EQ(result2, 0);
  97. }
  98. TEST(Script, Basic_Concatination)
  99. {
  100. const char* result1 = RunScript<const char*>(R"(
  101. return "a" @ "b";
  102. )");
  103. EXPECT_STREQ(result1, "ab");
  104. const char* result2 = RunScript<const char*>(R"(
  105. return "a" SPC "b";
  106. )");
  107. EXPECT_STREQ(result2, "a b");
  108. const char* result3 = RunScript<const char*>(R"(
  109. return "a" TAB "b";
  110. )");
  111. EXPECT_STREQ(result3, "a\tb");
  112. const char* result4 = RunScript<const char*>(R"(
  113. return "a" NL "b";
  114. )");
  115. EXPECT_STREQ(result4, "a\nb");
  116. const char* complex = RunScript<const char*>(R"(
  117. return "a" @ "b" @ "c" @ "d";
  118. )");
  119. EXPECT_STREQ(complex, "abcd");
  120. }
  121. TEST(Script, Basic_Global_Variable_Tests)
  122. {
  123. S32 value = RunScript<S32>(R"(
  124. $a = 1;
  125. return $a;
  126. )");
  127. EXPECT_EQ(value, 1);
  128. }
  129. TEST(Script, Variable_Chaining_And_Usage)
  130. {
  131. S32 value = RunScript<S32>(R"(
  132. function t()
  133. {
  134. %a = %b = 2;
  135. return %a;
  136. }
  137. return t();
  138. )");
  139. EXPECT_EQ(value, 2);
  140. S32 valueGlobal = RunScript<S32>(R"(
  141. function t()
  142. {
  143. $a = %b = 2;
  144. }
  145. t();
  146. return $a;
  147. )");
  148. EXPECT_EQ(valueGlobal, 2);
  149. S32 value2 = RunScript<S32>(R"(
  150. function t(%a)
  151. {
  152. if ((%b = 2 * %a) != 5)
  153. return %b;
  154. return 5;
  155. }
  156. return t(2);
  157. )");
  158. EXPECT_EQ(value2, 4);
  159. }
  160. TEST(Script, Basic_Function_Call_And_Local_Variable_Testing)
  161. {
  162. S32 value = RunScript<S32>(R"(
  163. function t() { %a = 2; return %a; }
  164. return t();
  165. )");
  166. EXPECT_EQ(value, 2);
  167. S32 value2 = RunScript<S32>(R"(
  168. function add(%a, %b) { return %a + %b; }
  169. return add(2, 4);
  170. )");
  171. EXPECT_EQ(value2, 6);
  172. S32 value3 = RunScript<S32>(R"(
  173. function fib(%a) {
  174. if (%a == 0)
  175. return 0;
  176. if (%a == 1)
  177. return 1;
  178. return fib(%a - 1) + fib(%a - 2);
  179. }
  180. return fib(15);
  181. )");
  182. EXPECT_EQ(value3, 610);
  183. S32 staticCall = RunScript<S32>(R"(
  184. function SimObject::bar(%a, %b) {
  185. return %a + %b;
  186. }
  187. return SimObject::bar(1, 2);
  188. )");
  189. EXPECT_EQ(staticCall, 3);
  190. }
  191. TEST(Script, Basic_Conditional_Statements)
  192. {
  193. S32 value = RunScript<S32>(R"(
  194. $a = "hello";
  195. if ($a $= "hello")
  196. return 1;
  197. return 2;
  198. )");
  199. EXPECT_EQ(value, 1);
  200. const char* ternaryValue = RunScript<const char*>(R"(
  201. return $a $= "hello" ? "World" : "No U";
  202. )");
  203. EXPECT_STREQ(ternaryValue, "World");
  204. }
  205. TEST(Script, Basic_Loop_Statements)
  206. {
  207. S32 whileValue = RunScript<S32>(R"(
  208. $count = 0;
  209. while ($count < 5)
  210. $count++;
  211. return $count;
  212. )");
  213. EXPECT_EQ(whileValue, 5);
  214. const char* forValue = RunScript<const char*>(R"(
  215. function t(%times)
  216. {
  217. %result = "";
  218. for (%i = 0; %i < %times; %i++)
  219. %result = %result @ "a";
  220. return %result;
  221. }
  222. return t(3);
  223. )");
  224. EXPECT_STREQ(forValue, "aaa");
  225. const char* forIfValue = RunScript<const char*>(R"(
  226. function t() {
  227. %str = "";
  228. for (%i = 0; %i < 5; %i++) {
  229. %loopValue = %i;
  230. if (%str $= "")
  231. %str = %loopValue;
  232. else
  233. %str = %str @ "," SPC %loopValue;
  234. }
  235. return %str;
  236. }
  237. return t();
  238. )");
  239. EXPECT_STREQ(forIfValue, "0, 1, 2, 3, 4");
  240. }
  241. TEST(Script, TorqueScript_Array_Testing)
  242. {
  243. S32 value = RunScript<S32>(R"(
  244. function t(%idx) { %a[idx] = 2; return %a[idx]; }
  245. return t(5);
  246. )");
  247. EXPECT_EQ(value, 2);
  248. S32 value2 = RunScript<S32>(R"(
  249. function t(%idx) { %a[idx, 0] = 2; return %a[idx, 0]; }
  250. return t(5);
  251. )");
  252. EXPECT_EQ(value2, 2);
  253. }
  254. TEST(Script, Basic_SimObject)
  255. {
  256. SimObject* object = RunScript<SimObject*>(R"(
  257. return new SimObject(FudgeCollector) {
  258. fudge = "Chocolate";
  259. };
  260. )");
  261. EXPECT_NE(object, (SimObject*)NULL);
  262. const char* propertyValue = RunScript<const char*>(R"(
  263. return FudgeCollector.fudge;
  264. )");
  265. EXPECT_STREQ(propertyValue, "Chocolate");
  266. const char* funcReturn = RunScript<const char*>(R"(
  267. function SimObject::fooFunc(%this)
  268. {
  269. return "Bar";
  270. }
  271. return FudgeCollector.fooFunc();
  272. )");
  273. EXPECT_STREQ(funcReturn, "Bar");
  274. const char* parentFn = RunScript<const char*>(R"(
  275. new SimObject(Hello);
  276. function SimObject::fooFunc2(%this)
  277. {
  278. return "Bar";
  279. }
  280. function Hello::fooFunc2(%this)
  281. {
  282. %bar = Parent::fooFunc2(%this);
  283. return "Foo" @ %bar;
  284. }
  285. return Hello.fooFunc2();
  286. )");
  287. EXPECT_STREQ(parentFn, "FooBar");
  288. }
  289. TEST(Script, Basic_Package)
  290. {
  291. S32 value = RunScript<S32>(R"(
  292. function a() { return 3; }
  293. package overrides {
  294. function a() { return 5; }
  295. };
  296. return a();
  297. )");
  298. EXPECT_EQ(value, 3);
  299. S32 overrideValue = RunScript<S32>(R"(
  300. activatePackage(overrides);
  301. return a();
  302. )");
  303. EXPECT_EQ(overrideValue, 5);
  304. S32 deactivatedValue = RunScript<S32>(R"(
  305. deactivatePackage(overrides);
  306. return a();
  307. )");
  308. EXPECT_EQ(deactivatedValue, 3);
  309. }
  310. #endif