InteropTests.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. using System;
  2. using System.Collections.Generic;
  3. using Jint.Native;
  4. using Jint.Native.Object;
  5. using Jint.Tests.Runtime.Converters;
  6. using Jint.Tests.Runtime.Domain;
  7. using Shapes;
  8. using Xunit;
  9. namespace Jint.Tests.Runtime
  10. {
  11. public class InteropTests : IDisposable
  12. {
  13. private readonly Engine _engine;
  14. public InteropTests()
  15. {
  16. _engine = new Engine(cfg => cfg.AllowClr(typeof(Shape).Assembly))
  17. .SetValue("log", new Action<object>(Console.WriteLine))
  18. .SetValue("assert", new Action<bool>(Assert.True))
  19. .SetValue("equal", new Action<object, object>(Assert.Equal))
  20. ;
  21. }
  22. void IDisposable.Dispose()
  23. {
  24. }
  25. private void RunTest(string source)
  26. {
  27. _engine.Execute(source);
  28. }
  29. [Fact]
  30. public void PrimitiveTypesCanBeSet()
  31. {
  32. _engine.SetValue("x", 10);
  33. _engine.SetValue("y", true);
  34. _engine.SetValue("z", "foo");
  35. RunTest(@"
  36. assert(x === 10);
  37. assert(y === true);
  38. assert(z === 'foo');
  39. ");
  40. }
  41. [Fact]
  42. public void DelegatesCanBeSet()
  43. {
  44. _engine.SetValue("square", new Func<double, double>(x => x * x));
  45. RunTest(@"
  46. assert(square(10) === 100);
  47. ");
  48. }
  49. [Fact]
  50. public void DelegateWithNullableParameterCanBePassedANull()
  51. {
  52. _engine.SetValue("isnull", new Func<double?, bool>(x => x == null));
  53. RunTest(@"
  54. assert(isnull(null) === true);
  55. ");
  56. }
  57. [Fact]
  58. public void DelegateWithObjectParameterCanBePassedANull()
  59. {
  60. _engine.SetValue("isnull", new Func<object, bool>(x => x == null));
  61. RunTest(@"
  62. assert(isnull(null) === true);
  63. ");
  64. }
  65. [Fact]
  66. public void DelegateWithNullableParameterCanBePassedAnUndefined()
  67. {
  68. _engine.SetValue("isnull", new Func<double?, bool>(x => x == null));
  69. RunTest(@"
  70. assert(isnull(undefined) === true);
  71. ");
  72. }
  73. [Fact]
  74. public void DelegateWithObjectParameterCanBePassedAnUndefined()
  75. {
  76. _engine.SetValue("isnull", new Func<object, bool>(x => x == null));
  77. RunTest(@"
  78. assert(isnull(undefined) === true);
  79. ");
  80. }
  81. [Fact]
  82. public void DelegateWithNullableParameterCanBeExcluded()
  83. {
  84. _engine.SetValue("isnull", new Func<double?, bool>(x => x == null));
  85. RunTest(@"
  86. assert(isnull() === true);
  87. ");
  88. }
  89. [Fact]
  90. public void DelegateWithObjectParameterCanBeExcluded()
  91. {
  92. _engine.SetValue("isnull", new Func<object, bool>(x => x == null));
  93. RunTest(@"
  94. assert(isnull() === true);
  95. ");
  96. }
  97. [Fact]
  98. public void ExtraParametersAreIgnored()
  99. {
  100. _engine.SetValue("passNumber", new Func<int, int>(x => x));
  101. RunTest(@"
  102. assert(passNumber(123,'test',{},[],null) === 123);
  103. ");
  104. }
  105. private delegate string callParams(params object[] values);
  106. private delegate string callArgumentAndParams(string firstParam, params object[] values);
  107. [Fact]
  108. public void DelegatesWithParamsParameterCanBeInvoked()
  109. {
  110. var a = new A();
  111. _engine.SetValue("callParams", new callParams(a.Call13));
  112. _engine.SetValue("callArgumentAndParams", new callArgumentAndParams(a.Call14));
  113. RunTest(@"
  114. assert(callParams('1','2','3') === '1,2,3');
  115. assert(callParams('1') === '1');
  116. assert(callParams() === '');
  117. assert(callArgumentAndParams('a','1','2','3') === 'a:1,2,3');
  118. assert(callArgumentAndParams('a','1') === 'a:1');
  119. assert(callArgumentAndParams('a') === 'a:');
  120. assert(callArgumentAndParams() === ':');
  121. ");
  122. }
  123. [Fact]
  124. public void CanGetObjectProperties()
  125. {
  126. var p = new Person
  127. {
  128. Name = "Mickey Mouse"
  129. };
  130. _engine.SetValue("p", p);
  131. RunTest(@"
  132. assert(p.Name === 'Mickey Mouse');
  133. ");
  134. }
  135. [Fact]
  136. public void CanInvokeObjectMethods()
  137. {
  138. var p = new Person
  139. {
  140. Name = "Mickey Mouse"
  141. };
  142. _engine.SetValue("p", p);
  143. RunTest(@"
  144. assert(p.ToString() === 'Mickey Mouse');
  145. ");
  146. }
  147. [Fact]
  148. public void CanInvokeObjectMethodsWithPascalCase()
  149. {
  150. var p = new Person
  151. {
  152. Name = "Mickey Mouse"
  153. };
  154. _engine.SetValue("p", p);
  155. RunTest(@"
  156. assert(p.toString() === 'Mickey Mouse');
  157. ");
  158. }
  159. [Fact]
  160. public void CanSetObjectProperties()
  161. {
  162. var p = new Person
  163. {
  164. Name = "Mickey Mouse"
  165. };
  166. _engine.SetValue("p", p);
  167. RunTest(@"
  168. p.Name = 'Donald Duck';
  169. assert(p.Name === 'Donald Duck');
  170. ");
  171. Assert.Equal("Donald Duck", p.Name);
  172. }
  173. [Fact]
  174. public void CanGetIndexUsingStringKey()
  175. {
  176. var dictionary = new Dictionary<string, Person>();
  177. dictionary.Add("person1", new Person { Name = "Mickey Mouse" });
  178. dictionary.Add("person2", new Person { Name = "Goofy" });
  179. _engine.SetValue("dictionary", dictionary);
  180. RunTest(@"
  181. assert(dictionary['person1'].Name === 'Mickey Mouse');
  182. assert(dictionary['person2'].Name === 'Goofy');
  183. ");
  184. }
  185. [Fact]
  186. public void CanSetIndexUsingStringKey()
  187. {
  188. var dictionary = new Dictionary<string, Person>();
  189. dictionary.Add("person1", new Person { Name = "Mickey Mouse" });
  190. dictionary.Add("person2", new Person { Name = "Goofy" });
  191. _engine.SetValue("dictionary", dictionary);
  192. RunTest(@"
  193. dictionary['person2'].Name = 'Donald Duck';
  194. assert(dictionary['person2'].Name === 'Donald Duck');
  195. ");
  196. Assert.Equal("Donald Duck", dictionary["person2"].Name);
  197. }
  198. [Fact]
  199. public void CanGetIndexUsingIntegerKey()
  200. {
  201. var dictionary = new Dictionary<int, string>();
  202. dictionary.Add(1, "Mickey Mouse");
  203. dictionary.Add(2, "Goofy");
  204. _engine.SetValue("dictionary", dictionary);
  205. RunTest(@"
  206. assert(dictionary[1] === 'Mickey Mouse');
  207. assert(dictionary[2] === 'Goofy');
  208. ");
  209. }
  210. [Fact]
  211. public void CanSetIndexUsingIntegerKey()
  212. {
  213. var dictionary = new Dictionary<int, string>();
  214. dictionary.Add(1, "Mickey Mouse");
  215. dictionary.Add(2, "Goofy");
  216. _engine.SetValue("dictionary", dictionary);
  217. RunTest(@"
  218. dictionary[2] = 'Donald Duck';
  219. assert(dictionary[2] === 'Donald Duck');
  220. ");
  221. Assert.Equal("Mickey Mouse", dictionary[1]);
  222. Assert.Equal("Donald Duck", dictionary[2]);
  223. }
  224. [Fact]
  225. public void CanUseGenericMethods()
  226. {
  227. var dictionary = new Dictionary<int, string>();
  228. dictionary.Add(1, "Mickey Mouse");
  229. _engine.SetValue("dictionary", dictionary);
  230. RunTest(@"
  231. dictionary.Add(2, 'Goofy');
  232. assert(dictionary[2] === 'Goofy');
  233. ");
  234. Assert.Equal("Mickey Mouse", dictionary[1]);
  235. Assert.Equal("Goofy", dictionary[2]);
  236. }
  237. [Fact]
  238. public void CanUseMultiGenericTypes()
  239. {
  240. RunTest(@"
  241. var type = System.Collections.Generic.Dictionary(System.Int32, System.String);
  242. var dictionary = new type();
  243. dictionary.Add(1, 'Mickey Mouse');
  244. dictionary.Add(2, 'Goofy');
  245. assert(dictionary[2] === 'Goofy');
  246. ");
  247. }
  248. [Fact]
  249. public void CanUseIndexOnCollection()
  250. {
  251. var collection = new System.Collections.ObjectModel.Collection<string>();
  252. collection.Add("Mickey Mouse");
  253. collection.Add("Goofy");
  254. _engine.SetValue("dictionary", collection);
  255. RunTest(@"
  256. dictionary[1] = 'Donald Duck';
  257. assert(dictionary[1] === 'Donald Duck');
  258. ");
  259. Assert.Equal("Mickey Mouse", collection[0]);
  260. Assert.Equal("Donald Duck", collection[1]);
  261. }
  262. [Fact]
  263. public void CanUseIndexOnList()
  264. {
  265. var arrayList = new System.Collections.ArrayList(2);
  266. arrayList.Add("Mickey Mouse");
  267. arrayList.Add("Goofy");
  268. _engine.SetValue("dictionary", arrayList);
  269. RunTest(@"
  270. dictionary[1] = 'Donald Duck';
  271. assert(dictionary[1] === 'Donald Duck');
  272. ");
  273. Assert.Equal("Mickey Mouse", arrayList[0]);
  274. Assert.Equal("Donald Duck", arrayList[1]);
  275. }
  276. [Fact]
  277. public void CanAccessAnonymousObject()
  278. {
  279. var p = new
  280. {
  281. Name = "Mickey Mouse",
  282. };
  283. _engine.SetValue("p", p);
  284. RunTest(@"
  285. assert(p.Name === 'Mickey Mouse');
  286. ");
  287. }
  288. [Fact]
  289. public void CanAccessAnonymousObjectProperties()
  290. {
  291. var p = new
  292. {
  293. Address = new
  294. {
  295. City = "Mouseton"
  296. }
  297. };
  298. _engine.SetValue("p", p);
  299. RunTest(@"
  300. assert(p.Address.City === 'Mouseton');
  301. ");
  302. }
  303. [Fact]
  304. public void PocosCanReturnJsValueDirectly()
  305. {
  306. var o = new
  307. {
  308. x = new JsValue(1),
  309. y = new JsValue("string"),
  310. };
  311. _engine.SetValue("o", o);
  312. RunTest(@"
  313. assert(o.x === 1);
  314. assert(o.y === 'string');
  315. ");
  316. }
  317. [Fact]
  318. public void PocosCanReturnObjectInstanceDirectly()
  319. {
  320. var x = new ObjectInstance(_engine) { Extensible = true};
  321. x.Put("foo", new JsValue("bar"), false);
  322. var o = new
  323. {
  324. x
  325. };
  326. _engine.SetValue("o", o);
  327. RunTest(@"
  328. assert(o.x.foo === 'bar');
  329. ");
  330. }
  331. [Fact]
  332. public void DateTimeIsConvertedToDate()
  333. {
  334. var o = new
  335. {
  336. z = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
  337. };
  338. _engine.SetValue("o", o);
  339. RunTest(@"
  340. assert(o.z.valueOf() === 0);
  341. ");
  342. }
  343. [Fact]
  344. public void DateTimeOffsetIsConvertedToDate()
  345. {
  346. var o = new
  347. {
  348. z = new DateTimeOffset(1970, 1, 1, 0, 0, 0, new TimeSpan())
  349. };
  350. _engine.SetValue("o", o);
  351. RunTest(@"
  352. assert(o.z.valueOf() === 0);
  353. ");
  354. }
  355. [Fact]
  356. public void EcmaValuesAreAutomaticallyConvertedWhenSetInPoco()
  357. {
  358. var p = new Person
  359. {
  360. Name = "foo",
  361. };
  362. _engine.SetValue("p", p);
  363. RunTest(@"
  364. assert(p.Name === 'foo');
  365. assert(p.Age === 0);
  366. p.Name = 'bar';
  367. p.Age = 10;
  368. ");
  369. Assert.Equal("bar", p.Name);
  370. Assert.Equal(10, p.Age);
  371. }
  372. [Fact]
  373. public void EcmaValuesAreAutomaticallyConvertedToBestMatchWhenSetInPoco()
  374. {
  375. var p = new Person
  376. {
  377. Name = "foo",
  378. };
  379. _engine.SetValue("p", p);
  380. RunTest(@"
  381. p.Name = 10;
  382. p.Age = '20';
  383. ");
  384. Assert.Equal("10", p.Name);
  385. Assert.Equal(20, p.Age);
  386. }
  387. [Fact]
  388. public void ShouldCallInstanceMethodWithoutArgument()
  389. {
  390. _engine.SetValue("a", new A());
  391. RunTest(@"
  392. assert(a.Call1() === 0);
  393. ");
  394. }
  395. [Fact]
  396. public void ShouldCallInstanceMethodOverloadArgument()
  397. {
  398. _engine.SetValue("a", new A());
  399. RunTest(@"
  400. assert(a.Call1(1) === 1);
  401. ");
  402. }
  403. [Fact]
  404. public void ShouldCallInstanceMethodWithString()
  405. {
  406. var p = new Person();
  407. _engine.SetValue("a", new A());
  408. _engine.SetValue("p", p);
  409. RunTest(@"
  410. p.Name = a.Call2('foo');
  411. assert(p.Name === 'foo');
  412. ");
  413. Assert.Equal("foo", p.Name);
  414. }
  415. [Fact]
  416. public void CanUseTrim()
  417. {
  418. var p = new Person { Name = "Mickey Mouse "};
  419. _engine.SetValue("p", p);
  420. RunTest(@"
  421. assert(p.Name === 'Mickey Mouse ');
  422. p.Name = p.Name.trim();
  423. assert(p.Name === 'Mickey Mouse');
  424. ");
  425. Assert.Equal("Mickey Mouse", p.Name);
  426. }
  427. [Fact]
  428. public void CanUseMathFloor()
  429. {
  430. var p = new Person();
  431. _engine.SetValue("p", p);
  432. RunTest(@"
  433. p.Age = Math.floor(1.6);p
  434. assert(p.Age === 1);
  435. ");
  436. Assert.Equal(1, p.Age);
  437. }
  438. [Fact]
  439. public void CanUseDelegateAsFunction()
  440. {
  441. var even = new Func<int, bool>(x => x % 2 == 0);
  442. _engine.SetValue("even", even);
  443. RunTest(@"
  444. assert(even(2) === true);
  445. ");
  446. }
  447. private class TestClass
  448. {
  449. public int? NullableInt { get; set; }
  450. public DateTime? NullableDate { get; set; }
  451. public bool? NullableBool { get; set; }
  452. }
  453. [Fact]
  454. public void CanSetNullablePropertiesOnPocos()
  455. {
  456. var instance = new TestClass();
  457. _engine.SetValue("instance", instance);
  458. RunTest(@"
  459. instance.NullableInt = 2;
  460. instance.NullableDate = new Date();
  461. instance.NullableBool = true;
  462. assert(instance.NullableInt===2);
  463. assert(instance.NullableDate!=null);
  464. assert(instance.NullableBool===true);
  465. ");
  466. }
  467. [Fact]
  468. public void ShouldConvertArrayToArrayInstance()
  469. {
  470. var result = _engine
  471. .SetValue("values", new[] { 1, 2, 3, 4, 5, 6 })
  472. .Execute("values.filter(function(x){ return x % 2 == 0; })");
  473. var parts = result.GetCompletionValue().ToObject();
  474. Assert.True(parts.GetType().IsArray);
  475. Assert.Equal(3, ((object[])parts).Length);
  476. Assert.Equal(2d, ((object[])parts)[0]);
  477. Assert.Equal(4d, ((object[])parts)[1]);
  478. Assert.Equal(6d, ((object[])parts)[2]);
  479. }
  480. [Fact]
  481. public void ShouldConvertListsToArrayInstance()
  482. {
  483. var result = _engine
  484. .SetValue("values", new List<object> { 1, 2, 3, 4, 5, 6 })
  485. .Execute("new Array(values).filter(function(x){ return x % 2 == 0; })");
  486. var parts = result.GetCompletionValue().ToObject();
  487. Assert.True(parts.GetType().IsArray);
  488. Assert.Equal(3, ((object[])parts).Length);
  489. Assert.Equal(2d, ((object[])parts)[0]);
  490. Assert.Equal(4d, ((object[])parts)[1]);
  491. Assert.Equal(6d, ((object[])parts)[2]);
  492. }
  493. [Fact]
  494. public void ShouldConvertArrayInstanceToArray()
  495. {
  496. var result = _engine.Execute("'[email protected]'.split('@');");
  497. var parts = result.GetCompletionValue().ToObject();
  498. Assert.True(parts.GetType().IsArray);
  499. Assert.Equal(2, ((object[])parts).Length);
  500. Assert.Equal("foo", ((object[])parts)[0]);
  501. Assert.Equal("bar.com", ((object[])parts)[1]);
  502. }
  503. [Fact]
  504. public void ShouldConvertBooleanInstanceToBool()
  505. {
  506. var result = _engine.Execute("new Boolean(true)");
  507. var value = result.GetCompletionValue().ToObject();
  508. Assert.Equal(typeof(bool), value.GetType());
  509. Assert.Equal(true, value);
  510. }
  511. [Fact]
  512. public void ShouldConvertDateInstanceToDateTime()
  513. {
  514. var result = _engine.Execute("new Date(0)");
  515. var value = result.GetCompletionValue().ToObject();
  516. Assert.Equal(typeof(DateTime), value.GetType());
  517. Assert.Equal(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), value);
  518. }
  519. [Fact]
  520. public void ShouldConvertNumberInstanceToDouble()
  521. {
  522. var result = _engine.Execute("new Number(10)");
  523. var value = result.GetCompletionValue().ToObject();
  524. Assert.Equal(typeof(double), value.GetType());
  525. Assert.Equal(10d, value);
  526. }
  527. [Fact]
  528. public void ShouldConvertStringInstanceToString()
  529. {
  530. var result = _engine.Execute("new String('foo')");
  531. var value = result.GetCompletionValue().ToObject();
  532. Assert.Equal(typeof(string), value.GetType());
  533. Assert.Equal("foo", value);
  534. }
  535. [Fact]
  536. public void ShouldConvertObjectInstanceToExpando()
  537. {
  538. _engine.Execute("var o = {a: 1, b: 'foo'}");
  539. var result = _engine.GetValue("o");
  540. dynamic value = result.ToObject();
  541. Assert.Equal(1, value.a);
  542. Assert.Equal("foo", value.b);
  543. var dic = (IDictionary<string, object>)result.ToObject();
  544. Assert.Equal(1d, dic["a"]);
  545. Assert.Equal("foo", dic["b"]);
  546. }
  547. [Fact]
  548. public void ShouldNotTryToConvertCompatibleTypes()
  549. {
  550. _engine.SetValue("a", new A());
  551. RunTest(@"
  552. assert(a.Call3('foo') === 'foo');
  553. assert(a.Call3(1) === '1');
  554. ");
  555. }
  556. [Fact]
  557. public void ShouldNotTryToConvertDerivedTypes()
  558. {
  559. _engine.SetValue("a", new A());
  560. _engine.SetValue("p", new Person { Name = "Mickey" });
  561. RunTest(@"
  562. assert(a.Call4(p) === 'Mickey');
  563. ");
  564. }
  565. [Fact]
  566. public void ShouldExecuteFunctionCallBackAsDelegate()
  567. {
  568. _engine.SetValue("a", new A());
  569. RunTest(@"
  570. assert(a.Call5(function(a,b){ return a+b }) === '1foo');
  571. ");
  572. }
  573. [Fact]
  574. public void ShouldExecuteFunctionCallBackAsFuncAndThisCanBeAssigned()
  575. {
  576. _engine.SetValue("a", new A());
  577. RunTest(@"
  578. assert(a.Call6(function(a,b){ return this+a+b }) === 'bar1foo');
  579. ");
  580. }
  581. [Fact]
  582. public void ShouldExecuteFunctionCallBackAsPredicate()
  583. {
  584. _engine.SetValue("a", new A());
  585. // Func<>
  586. RunTest(@"
  587. assert(a.Call8(function(){ return 'foo'; }) === 'foo');
  588. ");
  589. }
  590. [Fact]
  591. public void ShouldExecuteFunctionWithParameterCallBackAsPredicate()
  592. {
  593. _engine.SetValue("a", new A());
  594. // Func<,>
  595. RunTest(@"
  596. assert(a.Call7('foo', function(a){ return a === 'foo'; }) === true);
  597. ");
  598. }
  599. [Fact]
  600. public void ShouldExecuteActionCallBackAsPredicate()
  601. {
  602. _engine.SetValue("a", new A());
  603. // Action
  604. RunTest(@"
  605. var value;
  606. a.Call9(function(){ value = 'foo'; });
  607. assert(value === 'foo');
  608. ");
  609. }
  610. [Fact]
  611. public void ShouldExecuteActionWithParameterCallBackAsPredicate()
  612. {
  613. _engine.SetValue("a", new A());
  614. // Action<>
  615. RunTest(@"
  616. var value;
  617. a.Call10('foo', function(b){ value = b; });
  618. assert(value === 'foo');
  619. ");
  620. }
  621. [Fact]
  622. public void ShouldExecuteActionWithMultipleParametersCallBackAsPredicate()
  623. {
  624. _engine.SetValue("a", new A());
  625. // Action<,>
  626. RunTest(@"
  627. var value;
  628. a.Call11('foo', 'bar', function(a,b){ value = a + b; });
  629. assert(value === 'foobar');
  630. ");
  631. }
  632. [Fact]
  633. public void ShouldExecuteFunc()
  634. {
  635. _engine.SetValue("a", new A());
  636. // Func<int, int>
  637. RunTest(@"
  638. var result = a.Call12(42, function(a){ return a + a; });
  639. assert(result === 84);
  640. ");
  641. }
  642. [Fact]
  643. public void ShouldExecuteActionCallbackOnEventChanged()
  644. {
  645. var collection = new System.Collections.ObjectModel.ObservableCollection<string>();
  646. Assert.True(collection.Count == 0);
  647. _engine.SetValue("collection", collection);
  648. RunTest(@"
  649. var eventAction;
  650. collection.add_CollectionChanged(function(sender, eventArgs) { eventAction = eventArgs.Action; } );
  651. collection.Add('test');
  652. ");
  653. var eventAction = _engine.GetValue("eventAction").AsNumber();
  654. Assert.True(eventAction == 0);
  655. Assert.True(collection.Count == 1);
  656. }
  657. [Fact]
  658. public void ShouldUseSystemIO()
  659. {
  660. RunTest(@"
  661. var filename = System.IO.Path.GetTempFileName();
  662. var sw = System.IO.File.CreateText(filename);
  663. sw.Write('Hello World');
  664. sw.Dispose();
  665. var content = System.IO.File.ReadAllText(filename);
  666. System.Console.WriteLine(content);
  667. assert(content === 'Hello World');
  668. ");
  669. }
  670. [Fact]
  671. public void ShouldImportNamespace()
  672. {
  673. RunTest(@"
  674. var Shapes = importNamespace('Shapes');
  675. var circle = new Shapes.Circle();
  676. assert(circle.Radius === 0);
  677. assert(circle.Perimeter() === 0);
  678. ");
  679. }
  680. [Fact]
  681. public void ShouldConstructReferenceTypeWithParameters()
  682. {
  683. RunTest(@"
  684. var Shapes = importNamespace('Shapes');
  685. var circle = new Shapes.Circle(1);
  686. assert(circle.Radius === 1);
  687. assert(circle.Perimeter() === Math.PI);
  688. ");
  689. }
  690. [Fact]
  691. public void ShouldConstructValueTypeWithoutParameters()
  692. {
  693. RunTest(@"
  694. var guid = new System.Guid();
  695. assert('00000000-0000-0000-0000-000000000000' === guid.ToString());
  696. ");
  697. }
  698. [Fact]
  699. public void ShouldInvokeAFunctionByName()
  700. {
  701. RunTest(@"
  702. function add(x, y) { return x + y; }
  703. ");
  704. Assert.Equal(3, _engine.Invoke("add", 1, 2));
  705. }
  706. [Fact]
  707. public void ShouldNotInvokeNonFunctionValue()
  708. {
  709. RunTest(@"
  710. var x= 10;
  711. ");
  712. Assert.Throws<ArgumentException>(() => _engine.Invoke("x", 1, 2));
  713. }
  714. [Fact]
  715. public void CanGetField()
  716. {
  717. var o = new ClassWithField
  718. {
  719. Field = "Mickey Mouse"
  720. };
  721. _engine.SetValue("o", o);
  722. RunTest(@"
  723. assert(o.Field === 'Mickey Mouse');
  724. ");
  725. }
  726. [Fact]
  727. public void CanSetField()
  728. {
  729. var o = new ClassWithField();
  730. _engine.SetValue("o", o);
  731. RunTest(@"
  732. o.Field = 'Mickey Mouse';
  733. assert(o.Field === 'Mickey Mouse');
  734. ");
  735. Assert.Equal("Mickey Mouse", o.Field);
  736. }
  737. [Fact]
  738. public void CanGetStaticField()
  739. {
  740. RunTest(@"
  741. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  742. var statics = domain.ClassWithStaticFields;
  743. assert(statics.Get == 'Get');
  744. ");
  745. }
  746. [Fact]
  747. public void CanSetStaticField()
  748. {
  749. RunTest(@"
  750. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  751. var statics = domain.ClassWithStaticFields;
  752. statics.Set = 'hello';
  753. assert(statics.Set == 'hello');
  754. ");
  755. Assert.Equal(ClassWithStaticFields.Set, "hello");
  756. }
  757. [Fact]
  758. public void CanGetStaticAccessor()
  759. {
  760. RunTest(@"
  761. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  762. var statics = domain.ClassWithStaticFields;
  763. assert(statics.Getter == 'Getter');
  764. ");
  765. }
  766. [Fact]
  767. public void CanSetStaticAccessor()
  768. {
  769. RunTest(@"
  770. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  771. var statics = domain.ClassWithStaticFields;
  772. statics.Setter = 'hello';
  773. assert(statics.Setter == 'hello');
  774. ");
  775. Assert.Equal(ClassWithStaticFields.Setter, "hello");
  776. }
  777. [Fact]
  778. public void CantSetStaticReadonly()
  779. {
  780. RunTest(@"
  781. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  782. var statics = domain.ClassWithStaticFields;
  783. statics.Readonly = 'hello';
  784. assert(statics.Readonly == 'Readonly');
  785. ");
  786. Assert.Equal(ClassWithStaticFields.Readonly, "Readonly");
  787. }
  788. [Fact]
  789. public void CanSetCustomConverters()
  790. {
  791. var engine1 = new Engine();
  792. engine1.SetValue("p", new { Test = true });
  793. engine1.Execute("var result = p.Test;");
  794. Assert.True((bool)engine1.GetValue("result").ToObject());
  795. var engine2 = new Engine(o => o.AddObjectConverter(new NegateBoolConverter()));
  796. engine2.SetValue("p", new { Test = true });
  797. engine2.Execute("var result = p.Test;");
  798. Assert.False((bool)engine2.GetValue("result").ToObject());
  799. }
  800. [Fact]
  801. public void CanConvertEnumsToString()
  802. {
  803. var engine1 = new Engine(o => o.AddObjectConverter(new EnumsToStringConverter()))
  804. .SetValue("assert", new Action<bool>(Assert.True));
  805. engine1.SetValue("p", new { Comparison = StringComparison.CurrentCulture });
  806. engine1.Execute("assert(p.Comparison === 'CurrentCulture');");
  807. engine1.Execute("var result = p.Comparison;");
  808. Assert.Equal("CurrentCulture", (string)engine1.GetValue("result").ToObject());
  809. }
  810. [Fact]
  811. public void CanUserIncrementOperator()
  812. {
  813. var p = new Person
  814. {
  815. Age = 1,
  816. };
  817. _engine.SetValue("p", p);
  818. RunTest(@"
  819. assert(++p.Age === 2);
  820. ");
  821. Assert.Equal(2, p.Age);
  822. }
  823. [Fact]
  824. public void CanOverwriteValues()
  825. {
  826. _engine.SetValue("x", 3);
  827. _engine.SetValue("x", 4);
  828. RunTest(@"
  829. assert(x === 4);
  830. ");
  831. }
  832. [Fact]
  833. public void ShouldCreateGenericType()
  834. {
  835. RunTest(@"
  836. var ListOfString = System.Collections.Generic.List(System.String);
  837. var list = new ListOfString();
  838. list.Add('foo');
  839. list.Add(1);
  840. assert(2 === list.Count);
  841. ");
  842. }
  843. [Fact]
  844. public void EnumComparesByName()
  845. {
  846. var o = new
  847. {
  848. r = Colors.Red,
  849. b = Colors.Blue,
  850. g = Colors.Green,
  851. b2 = Colors.Red
  852. };
  853. _engine.SetValue("o", o);
  854. _engine.SetValue("assertFalse", new Action<bool>(Assert.False));
  855. RunTest(@"
  856. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  857. var colors = domain.Colors;
  858. assert(o.r === colors.Red);
  859. assert(o.g === colors.Green);
  860. assert(o.b === colors.Blue);
  861. assertFalse(o.b2 === colors.Blue);
  862. ");
  863. }
  864. [Fact]
  865. public void ShouldSetEnumProperty()
  866. {
  867. var s = new Circle
  868. {
  869. Color = Colors.Red,
  870. };
  871. _engine.SetValue("s", s);
  872. RunTest(@"
  873. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  874. var colors = domain.Colors;
  875. s.Color = colors.Blue;
  876. assert(s.Color === colors.Blue);
  877. ");
  878. _engine.SetValue("s", s);
  879. RunTest(@"
  880. s.Color = colors.Blue | colors.Green;
  881. assert(s.Color === colors.Blue | colors.Green);
  882. ");
  883. Assert.Equal(Colors.Blue | Colors.Green, s.Color);
  884. }
  885. [Fact]
  886. public void EnumIsConvertedToNumber()
  887. {
  888. var o = new
  889. {
  890. r = Colors.Red,
  891. b = Colors.Blue,
  892. g = Colors.Green
  893. };
  894. _engine.SetValue("o", o);
  895. RunTest(@"
  896. assert(o.r === 0);
  897. assert(o.g === 1);
  898. assert(o.b === 10);
  899. ");
  900. }
  901. [Fact]
  902. public void ShouldConvertToEnum()
  903. {
  904. var s = new Circle
  905. {
  906. Color = Colors.Red,
  907. };
  908. _engine.SetValue("s", s);
  909. RunTest(@"
  910. assert(s.Color === 0);
  911. s.Color = 10;
  912. assert(s.Color === 10);
  913. ");
  914. _engine.SetValue("s", s);
  915. RunTest(@"
  916. s.Color = 11;
  917. assert(s.Color === 11);
  918. ");
  919. Assert.Equal(Colors.Blue | Colors.Green, s.Color);
  920. }
  921. [Fact]
  922. public void ShouldUseExplicitPropertyGetter()
  923. {
  924. _engine.SetValue("c", new Company("ACME"));
  925. RunTest(@"
  926. assert(c.Name === 'ACME');
  927. ");
  928. }
  929. [Fact]
  930. public void ShouldUseExplicitIndexerPropertyGetter()
  931. {
  932. var company = new Company("ACME");
  933. ((ICompany)company)["Foo"] = "Bar";
  934. _engine.SetValue("c", company);
  935. RunTest(@"
  936. assert(c.Foo === 'Bar');
  937. ");
  938. }
  939. [Fact]
  940. public void ShouldUseExplicitPropertySetter()
  941. {
  942. _engine.SetValue("c", new Company("ACME"));
  943. RunTest(@"
  944. c.Name = 'Foo';
  945. assert(c.Name === 'Foo');
  946. ");
  947. }
  948. [Fact]
  949. public void ShouldUseExplicitIndexerPropertySetter()
  950. {
  951. var company = new Company("ACME");
  952. ((ICompany)company)["Foo"] = "Bar";
  953. _engine.SetValue("c", company);
  954. RunTest(@"
  955. c.Foo = 'Baz';
  956. assert(c.Foo === 'Baz');
  957. ");
  958. }
  959. [Fact]
  960. public void ShouldUseExplicitMethod()
  961. {
  962. _engine.SetValue("c", new Company("ACME"));
  963. RunTest(@"
  964. assert(0 === c.CompareTo(c));
  965. ");
  966. }
  967. [Fact]
  968. public void ShouldCallInstanceMethodWithParams()
  969. {
  970. _engine.SetValue("a", new A());
  971. RunTest(@"
  972. assert(a.Call13('1','2','3') === '1,2,3');
  973. assert(a.Call13('1') === '1');
  974. assert(a.Call13(1) === '1');
  975. assert(a.Call13() === '');
  976. assert(a.Call14('a','1','2','3') === 'a:1,2,3');
  977. assert(a.Call14('a','1') === 'a:1');
  978. assert(a.Call14('a') === 'a:');
  979. function call13wrapper(){ return a.Call13.apply(a, Array.prototype.slice.call(arguments)); }
  980. assert(call13wrapper('1','2','3') === '1,2,3');
  981. assert(a.Call13('1','2','3') === a.Call13(['1','2','3']));
  982. ");
  983. }
  984. [Fact]
  985. public void ShouldCallInstanceMethodWithJsValueParams()
  986. {
  987. _engine.SetValue("a", new A());
  988. RunTest(@"
  989. assert(a.Call16('1','2','3') === '1,2,3');
  990. assert(a.Call16('1') === '1');
  991. assert(a.Call16(1) === '1');
  992. assert(a.Call16() === '');
  993. assert(a.Call16('1','2','3') === a.Call16(['1','2','3']));
  994. ");
  995. }
  996. [Fact]
  997. public void NullValueAsArgumentShouldWork()
  998. {
  999. _engine.SetValue("a", new A());
  1000. RunTest(@"
  1001. var x = a.Call2(null);
  1002. assert(x === null);
  1003. ");
  1004. }
  1005. [Fact]
  1006. public void ShouldSetPropertyToNull()
  1007. {
  1008. var p = new Person { Name = "Mickey" };
  1009. _engine.SetValue("p", p);
  1010. RunTest(@"
  1011. assert(p.Name != null);
  1012. p.Name = null;
  1013. assert(p.Name == null);
  1014. ");
  1015. Assert.True(p.Name == null);
  1016. }
  1017. [Fact]
  1018. public void ShouldCallMethodWithNull()
  1019. {
  1020. _engine.SetValue("a", new A());
  1021. RunTest(@"
  1022. a.Call15(null);
  1023. var result = a.Call2(null);
  1024. assert(result == null);
  1025. ");
  1026. }
  1027. [Fact]
  1028. public void ShouldReturnUndefinedProperty()
  1029. {
  1030. _engine.SetValue("uo", new { foo = "bar" });
  1031. _engine.SetValue("ud", new Dictionary<string, object>() { {"foo", "bar"} });
  1032. _engine.SetValue("ul", new List<string>() { "foo", "bar" });
  1033. RunTest(@"
  1034. assert(!uo.undefinedProperty);
  1035. assert(!ul[5]);
  1036. assert(!ud.undefinedProperty);
  1037. ");
  1038. }
  1039. [Fact]
  1040. public void ShouldAutomaticallyConvertArraysToFindBestInteropResulution()
  1041. {
  1042. _engine.SetValue("a", new ArrayConverterTestClass());
  1043. _engine.SetValue("item1", new ArrayConverterItem(1));
  1044. _engine.SetValue("item2", new ArrayConverterItem(2));
  1045. RunTest(@"
  1046. assert(a.MethodAcceptsArrayOfInt([false, '1', 2]) === a.MethodAcceptsArrayOfInt([0, 1, 2]));
  1047. assert(a.MethodAcceptsArrayOfStrings(['1', 2]) === a.MethodAcceptsArrayOfStrings([1, 2]));
  1048. assert(a.MethodAcceptsArrayOfBool(['1', 0]) === a.MethodAcceptsArrayOfBool([true, false]));
  1049. assert(a.MethodAcceptsArrayOfStrings([item1, item2]) === a.MethodAcceptsArrayOfStrings(['1', '2']));
  1050. assert(a.MethodAcceptsArrayOfInt([item1, item2]) === a.MethodAcceptsArrayOfInt([1, 2]));
  1051. ");
  1052. }
  1053. [Fact]
  1054. public void ShouldImportNamespaceNestedType()
  1055. {
  1056. RunTest(@"
  1057. var shapes = importNamespace('Shapes.Circle');
  1058. var kinds = shapes.Kind;
  1059. assert(kinds.Unit === 0);
  1060. assert(kinds.Ellipse === 1);
  1061. assert(kinds.Round === 5);
  1062. ");
  1063. }
  1064. [Fact]
  1065. public void ShouldImportNamespaceNestedNestedType()
  1066. {
  1067. RunTest(@"
  1068. var meta = importNamespace('Shapes.Circle.Meta');
  1069. var usages = meta.Usage;
  1070. assert(usages.Public === 0);
  1071. assert(usages.Private === 1);
  1072. assert(usages.Internal === 11);
  1073. ");
  1074. }
  1075. [Fact]
  1076. public void ShouldGetNestedNestedProp()
  1077. {
  1078. RunTest(@"
  1079. var meta = importNamespace('Shapes.Circle');
  1080. var m = new meta.Meta();
  1081. assert(m.Description === 'descp');
  1082. ");
  1083. }
  1084. [Fact]
  1085. public void ShouldSetNestedNestedProp()
  1086. {
  1087. RunTest(@"
  1088. var meta = importNamespace('Shapes.Circle');
  1089. var m = new meta.Meta();
  1090. m.Description = 'hello';
  1091. assert(m.Description === 'hello');
  1092. ");
  1093. }
  1094. [Fact]
  1095. public void CanGetStaticNestedField()
  1096. {
  1097. RunTest(@"
  1098. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1099. var statics = domain.ClassWithStaticFields;
  1100. assert(statics.Get == 'Get');
  1101. ");
  1102. }
  1103. [Fact]
  1104. public void CanSetStaticNestedField()
  1105. {
  1106. RunTest(@"
  1107. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1108. var statics = domain.ClassWithStaticFields;
  1109. statics.Set = 'hello';
  1110. assert(statics.Set == 'hello');
  1111. ");
  1112. Assert.Equal(Nested.ClassWithStaticFields.Set, "hello");
  1113. }
  1114. [Fact]
  1115. public void CanGetStaticNestedAccessor()
  1116. {
  1117. RunTest(@"
  1118. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1119. var statics = domain.ClassWithStaticFields;
  1120. assert(statics.Getter == 'Getter');
  1121. ");
  1122. }
  1123. [Fact]
  1124. public void CanSetStaticNestedAccessor()
  1125. {
  1126. RunTest(@"
  1127. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1128. var statics = domain.ClassWithStaticFields;
  1129. statics.Setter = 'hello';
  1130. assert(statics.Setter == 'hello');
  1131. ");
  1132. Assert.Equal(Nested.ClassWithStaticFields.Setter, "hello");
  1133. }
  1134. [Fact]
  1135. public void CantSetStaticNestedReadonly()
  1136. {
  1137. RunTest(@"
  1138. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1139. var statics = domain.ClassWithStaticFields;
  1140. statics.Readonly = 'hello';
  1141. assert(statics.Readonly == 'Readonly');
  1142. ");
  1143. Assert.Equal(Nested.ClassWithStaticFields.Readonly, "Readonly");
  1144. }
  1145. [Fact]
  1146. public void ShouldExecuteFunctionWithValueTypeParameterCorrectly()
  1147. {
  1148. _engine.SetValue("a", new A());
  1149. // Func<int, int>
  1150. RunTest(@"
  1151. assert(a.Call17(function(value){ return value; }) === 17);
  1152. ");
  1153. }
  1154. [Fact]
  1155. public void ShouldExecuteActionWithValueTypeParameterCorrectly()
  1156. {
  1157. _engine.SetValue("a", new A());
  1158. // Action<int>
  1159. RunTest(@"
  1160. a.Call18(function(value){ assert(value === 18); });
  1161. ");
  1162. }
  1163. [Fact]
  1164. public void ShouldConvertToJsValue()
  1165. {
  1166. RunTest(@"
  1167. var now = System.DateTime.Now;
  1168. assert(new String(now) == now.toString());
  1169. var zero = System.Int32.MaxValue;
  1170. assert(new String(zero) == zero.toString());
  1171. ");
  1172. }
  1173. }
  1174. }