InteropTests.cs 26 KB

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