UserDataMethodsTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MoonSharp.Interpreter.Interop;
  6. using NUnit.Framework;
  7. namespace MoonSharp.Interpreter.Tests.EndToEnd
  8. {
  9. [TestFixture]
  10. public class UserDataMethodsTests
  11. {
  12. public class SomeClass : IComparable
  13. {
  14. public string ConcatNums(int p1, int p2)
  15. {
  16. return string.Format("{0}%{1}", p1, p2);
  17. }
  18. public int SomeMethodWithLongName(int i)
  19. {
  20. return i * 2;
  21. }
  22. public static StringBuilder SetComplexRecursive(List<int[]> intList)
  23. {
  24. StringBuilder sb = new StringBuilder();
  25. foreach (int[] arr in intList)
  26. {
  27. sb.Append(string.Join(",", arr.Select(s => s.ToString()).ToArray()));
  28. sb.Append("|");
  29. }
  30. return sb;
  31. }
  32. public static StringBuilder SetComplexTypes(List<string> strlist, IList<int> intlist, Dictionary<string, int> map,
  33. string[] strarray, int[] intarray)
  34. {
  35. StringBuilder sb = new StringBuilder();
  36. sb.Append(string.Join(",", strlist.ToArray()));
  37. sb.Append("|");
  38. sb.Append(string.Join(",", intlist.Select(i => i.ToString()).ToArray()));
  39. sb.Append("|");
  40. sb.Append(string.Join(",", map.Keys.OrderBy(x => x).Select(i => i.ToString()).ToArray()));
  41. sb.Append("|");
  42. sb.Append(string.Join(",", map.Values.OrderBy(x => x).Select(i => i.ToString()).ToArray()));
  43. sb.Append("|");
  44. sb.Append(string.Join(",", strarray));
  45. sb.Append("|");
  46. sb.Append(string.Join(",", intarray.Select(i => i.ToString()).ToArray()));
  47. return sb;
  48. }
  49. public static StringBuilder ConcatS(int p1, string p2, IComparable p3, bool p4, List<object> p5, IEnumerable<object> p6,
  50. StringBuilder p7, Dictionary<object, object> p8, SomeClass p9, int p10 = 1994)
  51. {
  52. p7.Append(p1);
  53. p7.Append(p2);
  54. p7.Append(p3);
  55. p7.Append(p4);
  56. p7.Append("|");
  57. foreach (var o in p5) p7.Append(o);
  58. p7.Append("|");
  59. foreach (var o in p6) p7.Append(o);
  60. p7.Append("|");
  61. foreach (var o in p8.Keys.OrderBy(x => x.ToString())) p7.Append(o);
  62. p7.Append("|");
  63. foreach (var o in p8.Values.OrderBy(x => x.ToString())) p7.Append(o);
  64. p7.Append("|");
  65. p7.Append(p9);
  66. p7.Append(p10);
  67. return p7;
  68. }
  69. public StringBuilder ConcatI(Script s, int p1, string p2, IComparable p3, bool p4, List<object> p5, IEnumerable<object> p6,
  70. StringBuilder p7, Dictionary<object, object> p8, SomeClass p9, int p10 = 1912)
  71. {
  72. Assert.IsNotNull(s);
  73. return ConcatS(p1, p2, p3, p4, p5, p6, p7, p8, this, p10);
  74. }
  75. public override string ToString()
  76. {
  77. return "!SOMECLASS!";
  78. }
  79. public List<int> MkList(int from, int to)
  80. {
  81. List<int> l = new List<int>();
  82. for (int i = from; i <= to; i++)
  83. l.Add(i);
  84. return l;
  85. }
  86. public int CompareTo(object obj)
  87. {
  88. throw new NotImplementedException();
  89. }
  90. }
  91. public interface Interface1
  92. {
  93. string Test1();
  94. }
  95. public interface Interface2
  96. {
  97. string Test2();
  98. }
  99. public class SomeOtherClass
  100. {
  101. public string Test1()
  102. {
  103. return "Test1";
  104. }
  105. public string Test2()
  106. {
  107. return "Test2";
  108. }
  109. }
  110. public class SomeOtherClassCustomDescriptor
  111. {
  112. }
  113. public class CustomDescriptor : IUserDataDescriptor
  114. {
  115. public string Name
  116. {
  117. get { return "ciao"; }
  118. }
  119. public Type Type
  120. {
  121. get { return typeof(SomeOtherClassCustomDescriptor); }
  122. }
  123. public DynValue Index(Script script, object obj, DynValue index, bool dummy)
  124. {
  125. return DynValue.NewNumber(index.Number * 4);
  126. }
  127. public bool SetIndex(Script script, object obj, DynValue index, DynValue value, bool dummy)
  128. {
  129. throw new NotImplementedException();
  130. }
  131. public string AsString(object obj)
  132. {
  133. return null;
  134. }
  135. public DynValue MetaIndex(Script script, object obj, string metaname)
  136. {
  137. throw new NotImplementedException();
  138. }
  139. }
  140. public class SelfDescribingClass : IUserDataType
  141. {
  142. public DynValue Index(Script script, DynValue index, bool isNameIndex)
  143. {
  144. return DynValue.NewNumber(index.Number * 3);
  145. }
  146. public bool SetIndex(Script script, DynValue index, DynValue value, bool isNameIndex)
  147. {
  148. throw new NotImplementedException();
  149. }
  150. public DynValue MetaIndex(Script script, string metaname)
  151. {
  152. throw new NotImplementedException();
  153. }
  154. }
  155. public class SomeOtherClassWithDualInterfaces : Interface1, Interface2
  156. {
  157. public string Test1()
  158. {
  159. return "Test1";
  160. }
  161. public string Test2()
  162. {
  163. return "Test2";
  164. }
  165. }
  166. public void Test_ConcatMethodStaticComplexCustomConv(InteropAccessMode opt)
  167. {
  168. try
  169. {
  170. UserData.UnregisterType<SomeClass>();
  171. string script = @"
  172. strlist = { 'ciao', 'hello', 'aloha' };
  173. intlist = { };
  174. dictry = { ciao = 39, hello = 78, aloha = 128 };
  175. x = static.SetComplexTypes(strlist, intlist, dictry, strlist, intlist);
  176. return x;";
  177. Script S = new Script();
  178. SomeClass obj = new SomeClass();
  179. UserData.UnregisterType<SomeClass>();
  180. UserData.RegisterType<SomeClass>(opt);
  181. Script.GlobalOptions.CustomConverters.Clear();
  182. Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(List<string>),
  183. v => null);
  184. Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(IList<int>),
  185. v => new List<int>() { 42, 77, 125, 13 });
  186. Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(int[]),
  187. v => new int[] { 43, 78, 126, 14 });
  188. Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<StringBuilder>(
  189. v => DynValue.NewString(v.ToString().ToUpper()));
  190. S.Globals.Set("static", UserData.CreateStatic<SomeClass>());
  191. S.Globals.Set("myobj", UserData.Create(obj));
  192. DynValue res = S.DoString(script);
  193. Assert.AreEqual(DataType.String, res.Type);
  194. Assert.AreEqual("CIAO,HELLO,ALOHA|42,77,125,13|ALOHA,CIAO,HELLO|39,78,128|CIAO,HELLO,ALOHA|43,78,126,14", res.String);
  195. }
  196. finally
  197. {
  198. Script.GlobalOptions.CustomConverters.Clear();
  199. }
  200. }
  201. public void Test_ConcatMethodStaticComplex(InteropAccessMode opt)
  202. {
  203. UserData.UnregisterType<SomeClass>();
  204. string script = @"
  205. strlist = { 'ciao', 'hello', 'aloha' };
  206. intlist = { 42, 77, 125, 13 };
  207. dictry = { ciao = 39, hello = 78, aloha = 128 };
  208. x = static.SetComplexTypes(strlist, intlist, dictry, strlist, intlist);
  209. return x;";
  210. Script S = new Script();
  211. SomeClass obj = new SomeClass();
  212. UserData.UnregisterType<SomeClass>();
  213. UserData.RegisterType<SomeClass>(opt);
  214. S.Globals.Set("static", UserData.CreateStatic<SomeClass>());
  215. S.Globals.Set("myobj", UserData.Create(obj));
  216. DynValue res = S.DoString(script);
  217. Assert.AreEqual(DataType.String, res.Type);
  218. Assert.AreEqual("ciao,hello,aloha|42,77,125,13|aloha,ciao,hello|39,78,128|ciao,hello,aloha|42,77,125,13", res.String);
  219. }
  220. public void Test_ConcatMethodStaticComplexRec(InteropAccessMode opt)
  221. {
  222. UserData.UnregisterType<SomeClass>();
  223. string script = @"
  224. array = { { 1, 2, 3 }, { 11, 35, 77 }, { 16, 42, 64 }, {99, 76, 17 } };
  225. x = static.SetComplexRecursive(array);
  226. return x;";
  227. Script S = new Script();
  228. SomeClass obj = new SomeClass();
  229. UserData.UnregisterType<SomeClass>();
  230. UserData.RegisterType<SomeClass>(opt);
  231. S.Globals.Set("static", UserData.CreateStatic<SomeClass>());
  232. S.Globals.Set("myobj", UserData.Create(obj));
  233. DynValue res = S.DoString(script);
  234. Assert.AreEqual(DataType.String, res.Type);
  235. Assert.AreEqual("1,2,3|11,35,77|16,42,64|99,76,17|", res.String);
  236. }
  237. public void Test_ConcatMethodStatic(InteropAccessMode opt)
  238. {
  239. UserData.UnregisterType<SomeClass>();
  240. string script = @"
  241. t = { 'asd', 'qwe', 'zxc', ['x'] = 'X', ['y'] = 'Y' };
  242. x = static.ConcatS(1, 'ciao', myobj, true, t, t, 'eheh', t, myobj);
  243. return x;";
  244. Script S = new Script();
  245. SomeClass obj = new SomeClass();
  246. UserData.UnregisterType<SomeClass>();
  247. UserData.RegisterType<SomeClass>(opt);
  248. S.Globals.Set("static", UserData.CreateStatic<SomeClass>());
  249. S.Globals.Set("myobj", UserData.Create(obj));
  250. DynValue res = S.DoString(script);
  251. Assert.AreEqual(DataType.String, res.Type);
  252. Assert.AreEqual("eheh1ciao!SOMECLASS!True|asdqwezxc|asdqwezxc|123xy|asdqweXYzxc|!SOMECLASS!1994", res.String);
  253. }
  254. public void Test_ConcatMethod(InteropAccessMode opt)
  255. {
  256. UserData.UnregisterType<SomeClass>();
  257. string script = @"
  258. t = { 'asd', 'qwe', 'zxc', ['x'] = 'X', ['y'] = 'Y' };
  259. x = myobj.ConcatI(1, 'ciao', myobj, true, t, t, 'eheh', t, myobj);
  260. return x;";
  261. Script S = new Script();
  262. SomeClass obj = new SomeClass();
  263. UserData.UnregisterType<SomeClass>();
  264. UserData.RegisterType<SomeClass>(opt);
  265. S.Globals.Set("myobj", UserData.Create(obj));
  266. DynValue res = S.DoString(script);
  267. Assert.AreEqual(DataType.String, res.Type);
  268. Assert.AreEqual("eheh1ciao!SOMECLASS!True|asdqwezxc|asdqwezxc|123xy|asdqweXYzxc|!SOMECLASS!1912", res.String);
  269. }
  270. public void Test_ConcatMethodSemicolon(InteropAccessMode opt)
  271. {
  272. UserData.UnregisterType<SomeClass>();
  273. string script = @"
  274. t = { 'asd', 'qwe', 'zxc', ['x'] = 'X', ['y'] = 'Y' };
  275. x = myobj:ConcatI(1, 'ciao', myobj, true, t, t, 'eheh', t, myobj);
  276. return x;";
  277. Script S = new Script();
  278. SomeClass obj = new SomeClass();
  279. UserData.UnregisterType<SomeClass>();
  280. UserData.RegisterType<SomeClass>(opt);
  281. S.Globals.Set("myobj", UserData.Create(obj));
  282. DynValue res = S.DoString(script);
  283. Assert.AreEqual(DataType.String, res.Type);
  284. Assert.AreEqual("eheh1ciao!SOMECLASS!True|asdqwezxc|asdqwezxc|123xy|asdqweXYzxc|!SOMECLASS!1912", res.String);
  285. }
  286. public void Test_ConstructorAndConcatMethodSemicolon(InteropAccessMode opt)
  287. {
  288. UserData.UnregisterType<SomeClass>();
  289. string script = @"
  290. myobj = mytype.__new();
  291. t = { 'asd', 'qwe', 'zxc', ['x'] = 'X', ['y'] = 'Y' };
  292. x = myobj:ConcatI(1, 'ciao', myobj, true, t, t, 'eheh', t, myobj);
  293. return x;";
  294. Script S = new Script();
  295. SomeClass obj = new SomeClass();
  296. UserData.UnregisterType<SomeClass>();
  297. UserData.RegisterType<SomeClass>(opt);
  298. S.Globals["mytype"] = typeof(SomeClass);
  299. DynValue res = S.DoString(script);
  300. Assert.AreEqual(DataType.String, res.Type);
  301. Assert.AreEqual("eheh1ciao!SOMECLASS!True|asdqwezxc|asdqwezxc|123xy|asdqweXYzxc|!SOMECLASS!1912", res.String);
  302. }
  303. public void Test_ConcatMethodStaticSimplifiedSyntax(InteropAccessMode opt)
  304. {
  305. UserData.UnregisterType<SomeClass>();
  306. string script = @"
  307. t = { 'asd', 'qwe', 'zxc', ['x'] = 'X', ['y'] = 'Y' };
  308. x = static.ConcatS(1, 'ciao', myobj, true, t, t, 'eheh', t, myobj);
  309. return x;";
  310. Script S = new Script();
  311. SomeClass obj = new SomeClass();
  312. UserData.UnregisterType<SomeClass>();
  313. UserData.RegisterType<SomeClass>(opt);
  314. S.Globals["static"] = typeof(SomeClass);
  315. S.Globals["myobj"] = obj;
  316. DynValue res = S.DoString(script);
  317. Assert.AreEqual(DataType.String, res.Type);
  318. Assert.AreEqual("eheh1ciao!SOMECLASS!True|asdqwezxc|asdqwezxc|123xy|asdqweXYzxc|!SOMECLASS!1994", res.String);
  319. }
  320. public void Test_DelegateMethod(InteropAccessMode opt)
  321. {
  322. UserData.UnregisterType<SomeClass>();
  323. string script = @"
  324. x = concat(1, 2);
  325. return x;";
  326. Script S = new Script();
  327. SomeClass obj = new SomeClass();
  328. S.Globals["concat"] = CallbackFunction.FromDelegate(S, (Func<int, int, string>)obj.ConcatNums, opt);
  329. DynValue res = S.DoString(script);
  330. Assert.AreEqual(DataType.String, res.Type);
  331. Assert.AreEqual("1%2", res.String);
  332. }
  333. public void Test_ListMethod(InteropAccessMode opt)
  334. {
  335. UserData.UnregisterType<SomeClass>();
  336. string script = @"
  337. x = mklist(1, 4);
  338. sum = 0;
  339. for _, v in ipairs(x) do
  340. sum = sum + v;
  341. end
  342. return sum;";
  343. Script S = new Script();
  344. SomeClass obj = new SomeClass();
  345. S.Globals["mklist"] = CallbackFunction.FromDelegate(S, (Func<int, int, List<int>>)obj.MkList, opt);
  346. DynValue res = S.DoString(script);
  347. Assert.AreEqual(DataType.Number, res.Type);
  348. Assert.AreEqual(10, res.Number);
  349. }
  350. [Test]
  351. public void Interop_ConcatMethod_None()
  352. {
  353. Test_ConcatMethod(InteropAccessMode.Reflection);
  354. }
  355. [Test]
  356. public void Interop_ConcatMethod_Lazy()
  357. {
  358. Test_ConcatMethod(InteropAccessMode.LazyOptimized);
  359. }
  360. [Test]
  361. public void Interop_ConcatMethod_Precomputed()
  362. {
  363. Test_ConcatMethod(InteropAccessMode.Preoptimized);
  364. }
  365. [Test]
  366. public void Interop_ConcatMethodSemicolon_None()
  367. {
  368. Test_ConcatMethodSemicolon(InteropAccessMode.Reflection);
  369. }
  370. [Test]
  371. public void Interop_ConcatMethodSemicolon_Lazy()
  372. {
  373. Test_ConcatMethodSemicolon(InteropAccessMode.LazyOptimized);
  374. }
  375. [Test]
  376. public void Interop_ConcatMethodSemicolon_Precomputed()
  377. {
  378. Test_ConcatMethodSemicolon(InteropAccessMode.Preoptimized);
  379. }
  380. [Test]
  381. public void Interop_ConstructorAndConcatMethodSemicolon_None()
  382. {
  383. Test_ConstructorAndConcatMethodSemicolon(InteropAccessMode.Reflection);
  384. }
  385. [Test]
  386. public void Interop_ConstructorAndConcatMethodSemicolon_Lazy()
  387. {
  388. Test_ConstructorAndConcatMethodSemicolon(InteropAccessMode.LazyOptimized);
  389. }
  390. [Test]
  391. public void Interop_ConstructorAndConcatMethodSemicolon_Precomputed()
  392. {
  393. Test_ConstructorAndConcatMethodSemicolon(InteropAccessMode.Preoptimized);
  394. }
  395. [Test]
  396. public void Interop_ConcatMethodStaticCplxCustomConv_None()
  397. {
  398. Test_ConcatMethodStaticComplexCustomConv(InteropAccessMode.Reflection);
  399. }
  400. [Test]
  401. public void Interop_ConcatMethodStaticCplxCustomConv_Lazy()
  402. {
  403. Test_ConcatMethodStaticComplexCustomConv(InteropAccessMode.LazyOptimized);
  404. }
  405. [Test]
  406. public void Interop_ConcatMethodStaticCplxCustomConv_Precomputed()
  407. {
  408. Test_ConcatMethodStaticComplexCustomConv(InteropAccessMode.Preoptimized);
  409. }
  410. [Test]
  411. public void Interop_ConcatMethodStaticCplx_None()
  412. {
  413. Test_ConcatMethodStaticComplex(InteropAccessMode.Reflection);
  414. }
  415. [Test]
  416. public void Interop_ConcatMethodStaticCplx_Lazy()
  417. {
  418. Test_ConcatMethodStaticComplex(InteropAccessMode.LazyOptimized);
  419. }
  420. [Test]
  421. public void Interop_ConcatMethodStaticCplx_Precomputed()
  422. {
  423. Test_ConcatMethodStaticComplex(InteropAccessMode.Preoptimized);
  424. }
  425. [Test]
  426. public void Interop_ConcatMethodStaticCplxRec_None()
  427. {
  428. Test_ConcatMethodStaticComplexRec(InteropAccessMode.Reflection);
  429. }
  430. [Test]
  431. public void Interop_ConcatMethodStaticCplxRec_Lazy()
  432. {
  433. Test_ConcatMethodStaticComplexRec(InteropAccessMode.LazyOptimized);
  434. }
  435. [Test]
  436. public void Interop_ConcatMethodStaticCplxRec_Precomputed()
  437. {
  438. Test_ConcatMethodStaticComplexRec(InteropAccessMode.Preoptimized);
  439. }
  440. [Test]
  441. public void Interop_ConcatMethodStatic_None()
  442. {
  443. Test_ConcatMethodStatic(InteropAccessMode.Reflection);
  444. }
  445. [Test]
  446. public void Interop_ConcatMethodStatic_Lazy()
  447. {
  448. Test_ConcatMethodStatic(InteropAccessMode.LazyOptimized);
  449. }
  450. [Test]
  451. public void Interop_ConcatMethodStatic_Precomputed()
  452. {
  453. Test_ConcatMethodStatic(InteropAccessMode.Preoptimized);
  454. }
  455. [Test]
  456. public void Interop_ConcatMethodStaticSimplifiedSyntax_None()
  457. {
  458. Test_ConcatMethodStaticSimplifiedSyntax(InteropAccessMode.Reflection);
  459. }
  460. [Test]
  461. public void Interop_ConcatMethodStaticSimplifiedSyntax_Lazy()
  462. {
  463. Test_ConcatMethodStaticSimplifiedSyntax(InteropAccessMode.LazyOptimized);
  464. }
  465. [Test]
  466. public void Interop_ConcatMethodStaticSimplifiedSyntax_Precomputed()
  467. {
  468. Test_ConcatMethodStaticSimplifiedSyntax(InteropAccessMode.Preoptimized);
  469. }
  470. [Test]
  471. public void Interop_DelegateMethod_None()
  472. {
  473. Test_DelegateMethod(InteropAccessMode.Reflection);
  474. }
  475. [Test]
  476. public void Interop_DelegateMethod_Lazy()
  477. {
  478. Test_DelegateMethod(InteropAccessMode.LazyOptimized);
  479. }
  480. [Test]
  481. public void Interop_DelegateMethod_Precomputed()
  482. {
  483. Test_DelegateMethod(InteropAccessMode.Preoptimized);
  484. }
  485. [Test]
  486. public void Interop_ListMethod_None()
  487. {
  488. Test_ListMethod(InteropAccessMode.Reflection);
  489. }
  490. [Test]
  491. public void Interop_ListMethod_Lazy()
  492. {
  493. Test_ListMethod(InteropAccessMode.LazyOptimized);
  494. }
  495. [Test]
  496. public void Interop_ListMethod_Precomputed()
  497. {
  498. Test_ListMethod(InteropAccessMode.Preoptimized);
  499. }
  500. [Test]
  501. public void Interop_TestAutoregisterPolicy()
  502. {
  503. try
  504. {
  505. string script = @"return myobj:Test1()";
  506. UserData.RegistrationPolicy = InteropRegistrationPolicy.Automatic;
  507. Script S = new Script();
  508. SomeOtherClass obj = new SomeOtherClass();
  509. S.Globals.Set("myobj", UserData.Create(obj));
  510. DynValue res = S.DoString(script);
  511. Assert.AreEqual(DataType.String, res.Type);
  512. Assert.AreEqual("Test1", res.String);
  513. }
  514. finally
  515. {
  516. UserData.RegistrationPolicy = InteropRegistrationPolicy.Explicit;
  517. }
  518. }
  519. [Test]
  520. public void Interop_TestAutoregisterPolicyWithDualInterfaces()
  521. {
  522. string script = @"return myobj:Test1() .. myobj:Test2()";
  523. Script S = new Script();
  524. UserData.UnregisterType<Interface1>();
  525. UserData.UnregisterType<Interface2>();
  526. UserData.RegisterType<Interface1>();
  527. UserData.RegisterType<Interface2>();
  528. SomeOtherClassWithDualInterfaces obj = new SomeOtherClassWithDualInterfaces();
  529. S.Globals.Set("myobj", UserData.Create(obj));
  530. DynValue res = S.DoString(script);
  531. Assert.AreEqual(DataType.String, res.Type);
  532. Assert.AreEqual("Test1Test2", res.String);
  533. }
  534. [Test]
  535. public void Interop_TestNamesCamelized()
  536. {
  537. UserData.UnregisterType<SomeClass>();
  538. string script = @"
  539. a = myobj:SomeMethodWithLongName(1);
  540. b = myobj:someMethodWithLongName(2);
  541. c = myobj:some_method_with_long_name(3);
  542. d = myobj:Some_method_withLong_name(4);
  543. return a + b + c + d;
  544. ";
  545. Script S = new Script();
  546. SomeClass obj = new SomeClass();
  547. UserData.UnregisterType<SomeClass>();
  548. UserData.RegisterType<SomeClass>();
  549. S.Globals.Set("myobj", UserData.Create(obj));
  550. DynValue res = S.DoString(script);
  551. Assert.AreEqual(DataType.Number, res.Type);
  552. Assert.AreEqual(20, res.Number);
  553. }
  554. [Test]
  555. public void Interop_TestSelfDescribingType()
  556. {
  557. UserData.UnregisterType<SelfDescribingClass>();
  558. string script = @"
  559. a = myobj[1];
  560. b = myobj[2];
  561. c = myobj[3];
  562. return a + b + c;
  563. ";
  564. Script S = new Script();
  565. SelfDescribingClass obj = new SelfDescribingClass();
  566. UserData.UnregisterType<SelfDescribingClass>();
  567. UserData.RegisterType<SelfDescribingClass>();
  568. S.Globals.Set("myobj", UserData.Create(obj));
  569. DynValue res = S.DoString(script);
  570. Assert.AreEqual(DataType.Number, res.Type);
  571. Assert.AreEqual(18, res.Number);
  572. }
  573. [Test]
  574. public void Interop_TestCustomDescribedType()
  575. {
  576. UserData.UnregisterType<SomeOtherClassCustomDescriptor>();
  577. string script = @"
  578. a = myobj[1];
  579. b = myobj[2];
  580. c = myobj[3];
  581. return a + b + c;
  582. ";
  583. Script S = new Script();
  584. SomeOtherClassCustomDescriptor obj = new SomeOtherClassCustomDescriptor();
  585. UserData.RegisterType<SomeOtherClassCustomDescriptor>(new CustomDescriptor());
  586. S.Globals.Set("myobj", UserData.Create(obj));
  587. DynValue res = S.DoString(script);
  588. Assert.AreEqual(DataType.Number, res.Type);
  589. Assert.AreEqual(24, res.Number);
  590. }
  591. }
  592. }