InteropTests.cs 40 KB

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