InteropTests.cs 29 KB

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