InteropTests.cs 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. private delegate string callParams(params object[] values);
  65. private delegate string callArgumentAndParams(string firstParam, params object[] values);
  66. [Fact]
  67. public void DelegatesWithParamsParameterCanBeInvoked()
  68. {
  69. var a = new A();
  70. _engine.SetValue("callParams", new callParams(a.Call13));
  71. _engine.SetValue("callArgumentAndParams", new callArgumentAndParams(a.Call14));
  72. RunTest(@"
  73. assert(callParams('1','2','3') === '1,2,3');
  74. assert(callParams('1') === '1');
  75. assert(callParams() === '');
  76. assert(callArgumentAndParams('a','1','2','3') === 'a:1,2,3');
  77. assert(callArgumentAndParams('a','1') === 'a:1');
  78. assert(callArgumentAndParams('a') === 'a:');
  79. ");
  80. }
  81. [Fact]
  82. public void CanGetObjectProperties()
  83. {
  84. var p = new Person
  85. {
  86. Name = "Mickey Mouse"
  87. };
  88. _engine.SetValue("p", p);
  89. RunTest(@"
  90. assert(p.Name === 'Mickey Mouse');
  91. ");
  92. }
  93. [Fact]
  94. public void CanInvokeObjectMethods()
  95. {
  96. var p = new Person
  97. {
  98. Name = "Mickey Mouse"
  99. };
  100. _engine.SetValue("p", p);
  101. RunTest(@"
  102. assert(p.ToString() === 'Mickey Mouse');
  103. ");
  104. }
  105. [Fact]
  106. public void CanSetObjectProperties()
  107. {
  108. var p = new Person
  109. {
  110. Name = "Mickey Mouse"
  111. };
  112. _engine.SetValue("p", p);
  113. RunTest(@"
  114. p.Name = 'Donald Duck';
  115. assert(p.Name === 'Donald Duck');
  116. ");
  117. Assert.Equal("Donald Duck", p.Name);
  118. }
  119. [Fact]
  120. public void CanGetIndexUsingStringKey()
  121. {
  122. var dictionary = new Dictionary<string, Person>();
  123. dictionary.Add("person1", new Person { Name = "Mickey Mouse" });
  124. dictionary.Add("person2", new Person { Name = "Goofy" });
  125. _engine.SetValue("dictionary", dictionary);
  126. RunTest(@"
  127. assert(dictionary['person1'].Name === 'Mickey Mouse');
  128. assert(dictionary['person2'].Name === 'Goofy');
  129. ");
  130. }
  131. [Fact]
  132. public void CanSetIndexUsingStringKey()
  133. {
  134. var dictionary = new Dictionary<string, Person>();
  135. dictionary.Add("person1", new Person { Name = "Mickey Mouse" });
  136. dictionary.Add("person2", new Person { Name = "Goofy" });
  137. _engine.SetValue("dictionary", dictionary);
  138. RunTest(@"
  139. dictionary['person2'].Name = 'Donald Duck';
  140. assert(dictionary['person2'].Name === 'Donald Duck');
  141. ");
  142. Assert.Equal("Donald Duck", dictionary["person2"].Name);
  143. }
  144. [Fact]
  145. public void CanGetIndexUsingIntegerKey()
  146. {
  147. var dictionary = new Dictionary<int, string>();
  148. dictionary.Add(1, "Mickey Mouse");
  149. dictionary.Add(2, "Goofy");
  150. _engine.SetValue("dictionary", dictionary);
  151. RunTest(@"
  152. assert(dictionary[1] === 'Mickey Mouse');
  153. assert(dictionary[2] === 'Goofy');
  154. ");
  155. }
  156. [Fact]
  157. public void CanSetIndexUsingIntegerKey()
  158. {
  159. var dictionary = new Dictionary<int, string>();
  160. dictionary.Add(1, "Mickey Mouse");
  161. dictionary.Add(2, "Goofy");
  162. _engine.SetValue("dictionary", dictionary);
  163. RunTest(@"
  164. dictionary[2] = 'Donald Duck';
  165. assert(dictionary[2] === 'Donald Duck');
  166. ");
  167. Assert.Equal("Mickey Mouse", dictionary[1]);
  168. Assert.Equal("Donald Duck", dictionary[2]);
  169. }
  170. [Fact]
  171. public void CanUseIndexOnCollection()
  172. {
  173. var collection = new System.Collections.ObjectModel.Collection<string>();
  174. collection.Add("Mickey Mouse");
  175. collection.Add("Goofy");
  176. _engine.SetValue("dictionary", collection);
  177. RunTest(@"
  178. dictionary[1] = 'Donald Duck';
  179. assert(dictionary[1] === 'Donald Duck');
  180. ");
  181. Assert.Equal("Mickey Mouse", collection[0]);
  182. Assert.Equal("Donald Duck", collection[1]);
  183. }
  184. [Fact]
  185. public void CanUseIndexOnList()
  186. {
  187. var arrayList = new System.Collections.ArrayList(2);
  188. arrayList.Add("Mickey Mouse");
  189. arrayList.Add("Goofy");
  190. _engine.SetValue("dictionary", arrayList);
  191. RunTest(@"
  192. dictionary[1] = 'Donald Duck';
  193. assert(dictionary[1] === 'Donald Duck');
  194. ");
  195. Assert.Equal("Mickey Mouse", arrayList[0]);
  196. Assert.Equal("Donald Duck", arrayList[1]);
  197. }
  198. [Fact]
  199. public void CanAccessAnonymousObject()
  200. {
  201. var p = new
  202. {
  203. Name = "Mickey Mouse",
  204. };
  205. _engine.SetValue("p", p);
  206. RunTest(@"
  207. assert(p.Name === 'Mickey Mouse');
  208. ");
  209. }
  210. [Fact]
  211. public void CanAccessAnonymousObjectProperties()
  212. {
  213. var p = new
  214. {
  215. Address = new
  216. {
  217. City = "Mouseton"
  218. }
  219. };
  220. _engine.SetValue("p", p);
  221. RunTest(@"
  222. assert(p.Address.City === 'Mouseton');
  223. ");
  224. }
  225. [Fact]
  226. public void PocosCanReturnJsValueDirectly()
  227. {
  228. var o = new
  229. {
  230. x = new JsValue(1),
  231. y = new JsValue("string"),
  232. };
  233. _engine.SetValue("o", o);
  234. RunTest(@"
  235. assert(o.x === 1);
  236. assert(o.y === 'string');
  237. ");
  238. }
  239. [Fact]
  240. public void PocosCanReturnObjectInstanceDirectly()
  241. {
  242. var x = new ObjectInstance(_engine) { Extensible = true};
  243. x.Put("foo", new JsValue("bar"), false);
  244. var o = new
  245. {
  246. x
  247. };
  248. _engine.SetValue("o", o);
  249. RunTest(@"
  250. assert(o.x.foo === 'bar');
  251. ");
  252. }
  253. [Fact]
  254. public void DateTimeIsConvertedToDate()
  255. {
  256. var o = new
  257. {
  258. z = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
  259. };
  260. _engine.SetValue("o", o);
  261. RunTest(@"
  262. assert(o.z.valueOf() === 0);
  263. ");
  264. }
  265. [Fact]
  266. public void DateTimeOffsetIsConvertedToDate()
  267. {
  268. var o = new
  269. {
  270. z = new DateTimeOffset(1970, 1, 1, 0, 0, 0, new TimeSpan())
  271. };
  272. _engine.SetValue("o", o);
  273. RunTest(@"
  274. assert(o.z.valueOf() === 0);
  275. ");
  276. }
  277. [Fact]
  278. public void EcmaValuesAreAutomaticallyConvertedWhenSetInPoco()
  279. {
  280. var p = new Person
  281. {
  282. Name = "foo",
  283. };
  284. _engine.SetValue("p", p);
  285. RunTest(@"
  286. assert(p.Name === 'foo');
  287. assert(p.Age === 0);
  288. p.Name = 'bar';
  289. p.Age = 10;
  290. ");
  291. Assert.Equal("bar", p.Name);
  292. Assert.Equal(10, p.Age);
  293. }
  294. [Fact]
  295. public void EcmaValuesAreAutomaticallyConvertedToBestMatchWhenSetInPoco()
  296. {
  297. var p = new Person
  298. {
  299. Name = "foo",
  300. };
  301. _engine.SetValue("p", p);
  302. RunTest(@"
  303. p.Name = 10;
  304. p.Age = '20';
  305. ");
  306. Assert.Equal("10", p.Name);
  307. Assert.Equal(20, p.Age);
  308. }
  309. [Fact]
  310. public void ShouldCallInstanceMethodWithoutArgument()
  311. {
  312. _engine.SetValue("a", new A());
  313. RunTest(@"
  314. assert(a.Call1() === 0);
  315. ");
  316. }
  317. [Fact]
  318. public void ShouldCallInstanceMethodOverloadArgument()
  319. {
  320. _engine.SetValue("a", new A());
  321. RunTest(@"
  322. assert(a.Call1(1) === 1);
  323. ");
  324. }
  325. [Fact]
  326. public void ShouldCallInstanceMethodWithString()
  327. {
  328. var p = new Person();
  329. _engine.SetValue("a", new A());
  330. _engine.SetValue("p", p);
  331. RunTest(@"
  332. p.Name = a.Call2('foo');
  333. assert(p.Name === 'foo');
  334. ");
  335. Assert.Equal("foo", p.Name);
  336. }
  337. [Fact]
  338. public void CanUseTrim()
  339. {
  340. var p = new Person { Name = "Mickey Mouse "};
  341. _engine.SetValue("p", p);
  342. RunTest(@"
  343. assert(p.Name === 'Mickey Mouse ');
  344. p.Name = p.Name.trim();
  345. assert(p.Name === 'Mickey Mouse');
  346. ");
  347. Assert.Equal("Mickey Mouse", p.Name);
  348. }
  349. [Fact]
  350. public void CanUseMathFloor()
  351. {
  352. var p = new Person();
  353. _engine.SetValue("p", p);
  354. RunTest(@"
  355. p.Age = Math.floor(1.6);p
  356. assert(p.Age === 1);
  357. ");
  358. Assert.Equal(1, p.Age);
  359. }
  360. [Fact]
  361. public void CanUseDelegateAsFunction()
  362. {
  363. var even = new Func<int, bool>(x => x % 2 == 0);
  364. _engine.SetValue("even", even);
  365. RunTest(@"
  366. assert(even(2) === true);
  367. ");
  368. }
  369. [Fact]
  370. public void ShouldConvertArrayToArrayInstance()
  371. {
  372. var result = _engine
  373. .SetValue("values", new[] { 1, 2, 3, 4, 5, 6 })
  374. .Execute("values.filter(function(x){ return x % 2 == 0; })");
  375. var parts = result.GetCompletionValue().ToObject();
  376. Assert.True(parts.GetType().IsArray);
  377. Assert.Equal(3, ((object[])parts).Length);
  378. Assert.Equal(2d, ((object[])parts)[0]);
  379. Assert.Equal(4d, ((object[])parts)[1]);
  380. Assert.Equal(6d, ((object[])parts)[2]);
  381. }
  382. [Fact]
  383. public void ShouldConvertListsToArrayInstance()
  384. {
  385. var result = _engine
  386. .SetValue("values", new List<object> { 1, 2, 3, 4, 5, 6 })
  387. .Execute("new Array(values).filter(function(x){ return x % 2 == 0; })");
  388. var parts = result.GetCompletionValue().ToObject();
  389. Assert.True(parts.GetType().IsArray);
  390. Assert.Equal(3, ((object[])parts).Length);
  391. Assert.Equal(2d, ((object[])parts)[0]);
  392. Assert.Equal(4d, ((object[])parts)[1]);
  393. Assert.Equal(6d, ((object[])parts)[2]);
  394. }
  395. [Fact]
  396. public void ShouldConvertArrayInstanceToArray()
  397. {
  398. var result = _engine.Execute("'[email protected]'.split('@');");
  399. var parts = result.GetCompletionValue().ToObject();
  400. Assert.True(parts.GetType().IsArray);
  401. Assert.Equal(2, ((object[])parts).Length);
  402. Assert.Equal("foo", ((object[])parts)[0]);
  403. Assert.Equal("bar.com", ((object[])parts)[1]);
  404. }
  405. [Fact]
  406. public void ShouldConvertBooleanInstanceToBool()
  407. {
  408. var result = _engine.Execute("new Boolean(true)");
  409. var value = result.GetCompletionValue().ToObject();
  410. Assert.Equal(typeof(bool), value.GetType());
  411. Assert.Equal(true, value);
  412. }
  413. [Fact]
  414. public void ShouldConvertDateInstanceToDateTime()
  415. {
  416. var result = _engine.Execute("new Date(0)");
  417. var value = result.GetCompletionValue().ToObject();
  418. Assert.Equal(typeof(DateTime), value.GetType());
  419. Assert.Equal(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), value);
  420. }
  421. [Fact]
  422. public void ShouldConvertNumberInstanceToDouble()
  423. {
  424. var result = _engine.Execute("new Number(10)");
  425. var value = result.GetCompletionValue().ToObject();
  426. Assert.Equal(typeof(double), value.GetType());
  427. Assert.Equal(10d, value);
  428. }
  429. [Fact]
  430. public void ShouldConvertStringInstanceToString()
  431. {
  432. var result = _engine.Execute("new String('foo')");
  433. var value = result.GetCompletionValue().ToObject();
  434. Assert.Equal(typeof(string), value.GetType());
  435. Assert.Equal("foo", value);
  436. }
  437. [Fact]
  438. public void ShouldConvertObjectInstanceToExpando()
  439. {
  440. _engine.Execute("var o = {a: 1, b: 'foo'}");
  441. var result = _engine.GetValue("o");
  442. dynamic value = result.ToObject();
  443. Assert.Equal(1, value.a);
  444. Assert.Equal("foo", value.b);
  445. var dic = (IDictionary<string, object>)result.ToObject();
  446. Assert.Equal(1d, dic["a"]);
  447. Assert.Equal("foo", dic["b"]);
  448. }
  449. [Fact]
  450. public void ShouldNotTryToConvertCompatibleTypes()
  451. {
  452. _engine.SetValue("a", new A());
  453. RunTest(@"
  454. assert(a.Call3('foo') === 'foo');
  455. assert(a.Call3(1) === '1');
  456. ");
  457. }
  458. [Fact]
  459. public void ShouldNotTryToConvertDerivedTypes()
  460. {
  461. _engine.SetValue("a", new A());
  462. _engine.SetValue("p", new Person { Name = "Mickey" });
  463. RunTest(@"
  464. assert(a.Call4(p) === 'Mickey');
  465. ");
  466. }
  467. [Fact]
  468. public void ShouldExecuteFunctionCallBackAsDelegate()
  469. {
  470. _engine.SetValue("a", new A());
  471. RunTest(@"
  472. assert(a.Call5(function(a,b){ return a+b }) === '1foo');
  473. ");
  474. }
  475. [Fact]
  476. public void ShouldExecuteFunctionCallBackAsFuncAndThisCanBeAssigned()
  477. {
  478. _engine.SetValue("a", new A());
  479. RunTest(@"
  480. assert(a.Call6(function(a,b){ return this+a+b }) === 'bar1foo');
  481. ");
  482. }
  483. [Fact]
  484. public void ShouldExecuteFunctionCallBackAsPredicate()
  485. {
  486. _engine.SetValue("a", new A());
  487. // Func<>
  488. RunTest(@"
  489. assert(a.Call8(function(){ return 'foo'; }) === 'foo');
  490. ");
  491. }
  492. [Fact]
  493. public void ShouldExecuteFunctionWithParameterCallBackAsPredicate()
  494. {
  495. _engine.SetValue("a", new A());
  496. // Func<,>
  497. RunTest(@"
  498. assert(a.Call7('foo', function(a){ return a === 'foo'; }) === true);
  499. ");
  500. }
  501. [Fact]
  502. public void ShouldExecuteActionCallBackAsPredicate()
  503. {
  504. _engine.SetValue("a", new A());
  505. // Action
  506. RunTest(@"
  507. var value;
  508. a.Call9(function(){ value = 'foo'; });
  509. assert(value === 'foo');
  510. ");
  511. }
  512. [Fact]
  513. public void ShouldExecuteActionWithParameterCallBackAsPredicate()
  514. {
  515. _engine.SetValue("a", new A());
  516. // Action<>
  517. RunTest(@"
  518. var value;
  519. a.Call10('foo', function(b){ value = b; });
  520. assert(value === 'foo');
  521. ");
  522. }
  523. [Fact]
  524. public void ShouldExecuteActionWithMultipleParametersCallBackAsPredicate()
  525. {
  526. _engine.SetValue("a", new A());
  527. // Action<,>
  528. RunTest(@"
  529. var value;
  530. a.Call11('foo', 'bar', function(a,b){ value = a + b; });
  531. assert(value === 'foobar');
  532. ");
  533. }
  534. [Fact]
  535. public void ShouldExecuteFunc()
  536. {
  537. _engine.SetValue("a", new A());
  538. // Func<int, int>
  539. RunTest(@"
  540. var result = a.Call12(42, function(a){ return a + a; });
  541. assert(result === 84);
  542. ");
  543. }
  544. [Fact]
  545. public void ShouldExecuteActionCallbackOnEventChanged()
  546. {
  547. var collection = new System.Collections.ObjectModel.ObservableCollection<string>();
  548. Assert.True(collection.Count == 0);
  549. _engine.SetValue("collection", collection);
  550. RunTest(@"
  551. var eventAction;
  552. collection.add_CollectionChanged(function(sender, eventArgs) { eventAction = eventArgs.Action; } );
  553. collection.Add('test');
  554. ");
  555. var eventAction = _engine.GetValue("eventAction").AsNumber();
  556. Assert.True(eventAction == 0);
  557. Assert.True(collection.Count == 1);
  558. }
  559. [Fact]
  560. public void ShouldUseSystemIO()
  561. {
  562. RunTest(@"
  563. var filename = System.IO.Path.GetTempFileName();
  564. var sw = System.IO.File.CreateText(filename);
  565. sw.Write('Hello World');
  566. sw.Dispose();
  567. var content = System.IO.File.ReadAllText(filename);
  568. System.Console.WriteLine(content);
  569. assert(content === 'Hello World');
  570. ");
  571. }
  572. [Fact]
  573. public void ShouldImportNamespace()
  574. {
  575. RunTest(@"
  576. var Shapes = importNamespace('Shapes');
  577. var circle = new Shapes.Circle();
  578. assert(circle.Radius === 0);
  579. assert(circle.Perimeter() === 0);
  580. ");
  581. }
  582. [Fact]
  583. public void ShouldConstructWithParameters()
  584. {
  585. RunTest(@"
  586. var Shapes = importNamespace('Shapes');
  587. var circle = new Shapes.Circle(1);
  588. assert(circle.Radius === 1);
  589. assert(circle.Perimeter() === Math.PI);
  590. ");
  591. }
  592. [Fact]
  593. public void ShouldInvokeAFunctionByName()
  594. {
  595. RunTest(@"
  596. function add(x, y) { return x + y; }
  597. ");
  598. Assert.Equal(3, _engine.Invoke("add", 1, 2));
  599. }
  600. [Fact]
  601. public void ShouldNotInvokeNonFunctionValue()
  602. {
  603. RunTest(@"
  604. var x= 10;
  605. ");
  606. Assert.Throws<ArgumentException>(() => _engine.Invoke("x", 1, 2));
  607. }
  608. [Fact]
  609. public void CanGetField()
  610. {
  611. var o = new ClassWithField
  612. {
  613. Field = "Mickey Mouse"
  614. };
  615. _engine.SetValue("o", o);
  616. RunTest(@"
  617. assert(o.Field === 'Mickey Mouse');
  618. ");
  619. }
  620. [Fact]
  621. public void CanSetField()
  622. {
  623. var o = new ClassWithField();
  624. _engine.SetValue("o", o);
  625. RunTest(@"
  626. o.Field = 'Mickey Mouse';
  627. assert(o.Field === 'Mickey Mouse');
  628. ");
  629. Assert.Equal("Mickey Mouse", o.Field);
  630. }
  631. [Fact]
  632. public void CanGetStaticField()
  633. {
  634. RunTest(@"
  635. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  636. var statics = domain.ClassWithStaticFields;
  637. assert(statics.Get == 'Get');
  638. ");
  639. }
  640. [Fact]
  641. public void CanSetStaticField()
  642. {
  643. RunTest(@"
  644. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  645. var statics = domain.ClassWithStaticFields;
  646. statics.Set = 'hello';
  647. assert(statics.Set == 'hello');
  648. ");
  649. Assert.Equal(ClassWithStaticFields.Set, "hello");
  650. }
  651. [Fact]
  652. public void CanGetStaticAccessor()
  653. {
  654. RunTest(@"
  655. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  656. var statics = domain.ClassWithStaticFields;
  657. assert(statics.Getter == 'Getter');
  658. ");
  659. }
  660. [Fact]
  661. public void CanSetStaticAccessor()
  662. {
  663. RunTest(@"
  664. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  665. var statics = domain.ClassWithStaticFields;
  666. statics.Setter = 'hello';
  667. assert(statics.Setter == 'hello');
  668. ");
  669. Assert.Equal(ClassWithStaticFields.Setter, "hello");
  670. }
  671. [Fact]
  672. public void CantSetStaticReadonly()
  673. {
  674. RunTest(@"
  675. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  676. var statics = domain.ClassWithStaticFields;
  677. statics.Readonly = 'hello';
  678. assert(statics.Readonly == 'Readonly');
  679. ");
  680. Assert.Equal(ClassWithStaticFields.Readonly, "Readonly");
  681. }
  682. [Fact]
  683. public void CanSetCustomConverters()
  684. {
  685. var engine1 = new Engine();
  686. engine1.SetValue("p", new { Test = true });
  687. engine1.Execute("var result = p.Test;");
  688. Assert.True((bool)engine1.GetValue("result").ToObject());
  689. var engine2 = new Engine(o => o.AddObjectConverter(new NegateBoolConverter()));
  690. engine2.SetValue("p", new { Test = true });
  691. engine2.Execute("var result = p.Test;");
  692. Assert.False((bool)engine2.GetValue("result").ToObject());
  693. }
  694. [Fact]
  695. public void CanUserIncrementOperator()
  696. {
  697. var p = new Person
  698. {
  699. Age = 1,
  700. };
  701. _engine.SetValue("p", p);
  702. RunTest(@"
  703. assert(++p.Age === 2);
  704. ");
  705. Assert.Equal(2, p.Age);
  706. }
  707. [Fact]
  708. public void CanOverwriteValues()
  709. {
  710. _engine.SetValue("x", 3);
  711. _engine.SetValue("x", 4);
  712. RunTest(@"
  713. assert(x === 4);
  714. ");
  715. }
  716. [Fact]
  717. public void ShouldCreateGenericType()
  718. {
  719. RunTest(@"
  720. var ListOfString = System.Collections.Generic.List(System.String);
  721. var list = new ListOfString();
  722. list.Add('foo');
  723. list.Add(1);
  724. assert(2 === list.Count);
  725. ");
  726. }
  727. [Fact]
  728. public void EnumComparesByName()
  729. {
  730. var o = new
  731. {
  732. r = Colors.Red,
  733. b = Colors.Blue,
  734. g = Colors.Green,
  735. b2 = Colors.Red
  736. };
  737. _engine.SetValue("o", o);
  738. _engine.SetValue("assertFalse", new Action<bool>(Assert.False));
  739. RunTest(@"
  740. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  741. var colors = domain.Colors;
  742. assert(o.r === colors.Red);
  743. assert(o.g === colors.Green);
  744. assert(o.b === colors.Blue);
  745. assertFalse(o.b2 === colors.Blue);
  746. ");
  747. }
  748. [Fact]
  749. public void ShouldSetEnumProperty()
  750. {
  751. var s = new Circle
  752. {
  753. Color = Colors.Red,
  754. };
  755. _engine.SetValue("s", s);
  756. RunTest(@"
  757. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  758. var colors = domain.Colors;
  759. s.Color = colors.Blue;
  760. assert(s.Color === colors.Blue);
  761. ");
  762. _engine.SetValue("s", s);
  763. RunTest(@"
  764. s.Color = colors.Blue | colors.Green;
  765. assert(s.Color === colors.Blue | colors.Green);
  766. ");
  767. Assert.Equal(Colors.Blue | Colors.Green, s.Color);
  768. }
  769. [Fact]
  770. public void EnumIsConvertedToNumber()
  771. {
  772. var o = new
  773. {
  774. r = Colors.Red,
  775. b = Colors.Blue,
  776. g = Colors.Green
  777. };
  778. _engine.SetValue("o", o);
  779. RunTest(@"
  780. assert(o.r === 0);
  781. assert(o.g === 1);
  782. assert(o.b === 10);
  783. ");
  784. }
  785. [Fact]
  786. public void ShouldConvertToEnum()
  787. {
  788. var s = new Circle
  789. {
  790. Color = Colors.Red,
  791. };
  792. _engine.SetValue("s", s);
  793. RunTest(@"
  794. assert(s.Color === 0);
  795. s.Color = 10;
  796. assert(s.Color === 10);
  797. ");
  798. _engine.SetValue("s", s);
  799. RunTest(@"
  800. s.Color = 11;
  801. assert(s.Color === 11);
  802. ");
  803. Assert.Equal(Colors.Blue | Colors.Green, s.Color);
  804. }
  805. [Fact]
  806. public void ShouldUseExplicitPropertyGetter()
  807. {
  808. _engine.SetValue("c", new Company("ACME"));
  809. RunTest(@"
  810. assert(c.Name === 'ACME');
  811. ");
  812. }
  813. [Fact]
  814. public void ShouldUseExplicitIndexerPropertyGetter()
  815. {
  816. var company = new Company("ACME");
  817. ((ICompany)company)["Foo"] = "Bar";
  818. _engine.SetValue("c", company);
  819. RunTest(@"
  820. assert(c.Foo === 'Bar');
  821. ");
  822. }
  823. [Fact]
  824. public void ShouldUseExplicitPropertySetter()
  825. {
  826. _engine.SetValue("c", new Company("ACME"));
  827. RunTest(@"
  828. c.Name = 'Foo';
  829. assert(c.Name === 'Foo');
  830. ");
  831. }
  832. [Fact]
  833. public void ShouldUseExplicitIndexerPropertySetter()
  834. {
  835. var company = new Company("ACME");
  836. ((ICompany)company)["Foo"] = "Bar";
  837. _engine.SetValue("c", company);
  838. RunTest(@"
  839. c.Foo = 'Baz';
  840. assert(c.Foo === 'Baz');
  841. ");
  842. }
  843. [Fact]
  844. public void ShouldUseExplicitMethod()
  845. {
  846. _engine.SetValue("c", new Company("ACME"));
  847. RunTest(@"
  848. assert(0 === c.CompareTo(c));
  849. ");
  850. }
  851. [Fact]
  852. public void ShouldCallInstanceMethodWithParams()
  853. {
  854. _engine.SetValue("a", new A());
  855. RunTest(@"
  856. assert(a.Call13('1','2','3') === '1,2,3');
  857. assert(a.Call13('1') === '1');
  858. assert(a.Call13(1) === '1');
  859. assert(a.Call13() === '');
  860. assert(a.Call14('a','1','2','3') === 'a:1,2,3');
  861. assert(a.Call14('a','1') === 'a:1');
  862. assert(a.Call14('a') === 'a:');
  863. function call13wrapper(){ return a.Call13.apply(a, Array.prototype.slice.call(arguments)); }
  864. assert(call13wrapper('1','2','3') === '1,2,3');
  865. assert(a.Call13('1','2','3') === a.Call13(['1','2','3']));
  866. ");
  867. }
  868. [Fact]
  869. public void NullValueAsArgumentShouldWork()
  870. {
  871. _engine.SetValue("a", new A());
  872. RunTest(@"
  873. var x = a.Call2(null);
  874. assert(x === null);
  875. ");
  876. }
  877. [Fact]
  878. public void ShouldSetPropertyToNull()
  879. {
  880. var p = new Person { Name = "Mickey" };
  881. _engine.SetValue("p", p);
  882. RunTest(@"
  883. assert(p.Name != null);
  884. p.Name = null;
  885. assert(p.Name == null);
  886. ");
  887. Assert.True(p.Name == null);
  888. }
  889. [Fact]
  890. public void ShouldCallMethodWithNull()
  891. {
  892. _engine.SetValue("a", new A());
  893. RunTest(@"
  894. a.Call15(null);
  895. var result = a.Call2(null);
  896. assert(result == null);
  897. ");
  898. }
  899. [Fact]
  900. public void ShouldReturnUndefinedProperty()
  901. {
  902. _engine.SetValue("uo", new { foo = "bar" });
  903. _engine.SetValue("ud", new Dictionary<string, object>() { {"foo", "bar"} });
  904. _engine.SetValue("ul", new List<string>() { "foo", "bar" });
  905. RunTest(@"
  906. assert(!uo.undefinedProperty);
  907. assert(!ul[5]);
  908. assert(!ud.undefinedProperty);
  909. ");
  910. }
  911. [Fact]
  912. public void ShouldAutomaticallyConvertArraysToFindBestInteropResulution()
  913. {
  914. _engine.SetValue("a", new ArrayConverterTestClass());
  915. _engine.SetValue("item1", new ArrayConverterItem(1));
  916. _engine.SetValue("item2", new ArrayConverterItem(2));
  917. RunTest(@"
  918. assert(a.MethodAcceptsArrayOfInt([false, '1', 2]) === a.MethodAcceptsArrayOfInt([0, 1, 2]));
  919. assert(a.MethodAcceptsArrayOfStrings(['1', 2]) === a.MethodAcceptsArrayOfStrings([1, 2]));
  920. assert(a.MethodAcceptsArrayOfBool(['1', 0]) === a.MethodAcceptsArrayOfBool([true, false]));
  921. assert(a.MethodAcceptsArrayOfStrings([item1, item2]) === a.MethodAcceptsArrayOfStrings(['1', '2']));
  922. assert(a.MethodAcceptsArrayOfInt([item1, item2]) === a.MethodAcceptsArrayOfInt([1, 2]));
  923. ");
  924. }
  925. }
  926. }