InteropTests.cs 47 KB

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