InteropTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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 ShouldUseSystemIO()
  452. {
  453. RunTest(@"
  454. var filename = System.IO.Path.GetTempFileName();
  455. var sw = System.IO.File.CreateText(filename);
  456. sw.Write('Hello World');
  457. sw.Dispose();
  458. var content = System.IO.File.ReadAllText(filename);
  459. System.Console.WriteLine(content);
  460. assert(content === 'Hello World');
  461. ");
  462. }
  463. [Fact]
  464. public void ShouldImportNamespace()
  465. {
  466. RunTest(@"
  467. var Shapes = importNamespace('Shapes');
  468. var circle = new Shapes.Circle();
  469. assert(circle.Radius === 0);
  470. assert(circle.Perimeter() === 0);
  471. ");
  472. }
  473. [Fact]
  474. public void ShouldConstructWithParameters()
  475. {
  476. RunTest(@"
  477. var Shapes = importNamespace('Shapes');
  478. var circle = new Shapes.Circle(1);
  479. assert(circle.Radius === 1);
  480. assert(circle.Perimeter() === Math.PI);
  481. ");
  482. }
  483. [Fact]
  484. public void ShouldInvokeAFunctionByName()
  485. {
  486. RunTest(@"
  487. function add(x, y) { return x + y; }
  488. ");
  489. Assert.Equal(3, _engine.Invoke("add", 1, 2));
  490. }
  491. [Fact]
  492. public void ShouldNotInvokeNonFunctionValue()
  493. {
  494. RunTest(@"
  495. var x= 10;
  496. ");
  497. Assert.Throws<ArgumentException>(() => _engine.Invoke("x", 1, 2));
  498. }
  499. [Fact]
  500. public void CanGetField()
  501. {
  502. var o = new ClassWithField
  503. {
  504. Field = "Mickey Mouse"
  505. };
  506. _engine.SetValue("o", o);
  507. RunTest(@"
  508. assert(o.Field === 'Mickey Mouse');
  509. ");
  510. }
  511. [Fact]
  512. public void CanSetField()
  513. {
  514. var o = new ClassWithField();
  515. _engine.SetValue("o", o);
  516. RunTest(@"
  517. o.Field = 'Mickey Mouse';
  518. assert(o.Field === 'Mickey Mouse');
  519. ");
  520. Assert.Equal("Mickey Mouse", o.Field);
  521. }
  522. [Fact]
  523. public void CanGetStaticField()
  524. {
  525. RunTest(@"
  526. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  527. var statics = domain.ClassWithStaticFields;
  528. assert(statics.Get == 'Get');
  529. ");
  530. }
  531. [Fact]
  532. public void CanSetStaticField()
  533. {
  534. RunTest(@"
  535. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  536. var statics = domain.ClassWithStaticFields;
  537. statics.Set = 'hello';
  538. assert(statics.Set == 'hello');
  539. ");
  540. Assert.Equal(ClassWithStaticFields.Set, "hello");
  541. }
  542. [Fact]
  543. public void CanGetStaticAccessor()
  544. {
  545. RunTest(@"
  546. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  547. var statics = domain.ClassWithStaticFields;
  548. assert(statics.Getter == 'Getter');
  549. ");
  550. }
  551. [Fact]
  552. public void CanSetStaticAccessor()
  553. {
  554. RunTest(@"
  555. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  556. var statics = domain.ClassWithStaticFields;
  557. statics.Setter = 'hello';
  558. assert(statics.Setter == 'hello');
  559. ");
  560. Assert.Equal(ClassWithStaticFields.Setter, "hello");
  561. }
  562. [Fact]
  563. public void CantSetStaticReadonly()
  564. {
  565. RunTest(@"
  566. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  567. var statics = domain.ClassWithStaticFields;
  568. statics.Readonly = 'hello';
  569. assert(statics.Readonly == 'Readonly');
  570. ");
  571. Assert.Equal(ClassWithStaticFields.Readonly, "Readonly");
  572. }
  573. [Fact]
  574. public void CanSetCustomConverters()
  575. {
  576. var engine1 = new Engine();
  577. engine1.SetValue("p", new { Test = true });
  578. engine1.Execute("var result = p.Test;");
  579. Assert.True((bool)engine1.GetValue("result").ToObject());
  580. var engine2 = new Engine(o => o.AddObjectConverter(new NegateBoolConverter()));
  581. engine2.SetValue("p", new { Test = true });
  582. engine2.Execute("var result = p.Test;");
  583. Assert.False((bool)engine2.GetValue("result").ToObject());
  584. }
  585. [Fact]
  586. public void CanUserIncrementOperator()
  587. {
  588. var p = new Person
  589. {
  590. Age = 1,
  591. };
  592. _engine.SetValue("p", p);
  593. RunTest(@"
  594. assert(++p.Age === 2);
  595. ");
  596. Assert.Equal(2, p.Age);
  597. }
  598. [Fact]
  599. public void CanOverwriteValues()
  600. {
  601. _engine.SetValue("x", 3);
  602. _engine.SetValue("x", 4);
  603. RunTest(@"
  604. assert(x === 4);
  605. ");
  606. }
  607. [Fact]
  608. public void ShouldCreateGenericType()
  609. {
  610. RunTest(@"
  611. var ListOfString = System.Collections.Generic.List(System.String);
  612. var list = new ListOfString();
  613. list.Add('foo');
  614. list.Add(1);
  615. assert(2 === list.Count);
  616. ");
  617. }
  618. [Fact]
  619. public void EnumComparesByName()
  620. {
  621. var o = new
  622. {
  623. r = Colors.Red,
  624. b = Colors.Blue,
  625. g = Colors.Green,
  626. b2 = Colors.Red
  627. };
  628. _engine.SetValue("o", o);
  629. _engine.SetValue("assertFalse", new Action<bool>(Assert.False));
  630. RunTest(@"
  631. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  632. var colors = domain.Colors;
  633. assert(o.r === colors.Red);
  634. assert(o.g === colors.Green);
  635. assert(o.b === colors.Blue);
  636. assertFalse(o.b2 === colors.Blue);
  637. ");
  638. }
  639. [Fact]
  640. public void ShouldSetEnumProperty()
  641. {
  642. var s = new Circle
  643. {
  644. Color = Colors.Red,
  645. };
  646. _engine.SetValue("s", s);
  647. RunTest(@"
  648. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  649. var colors = domain.Colors;
  650. s.Color = colors.Blue;
  651. assert(s.Color === colors.Blue);
  652. ");
  653. _engine.SetValue("s", s);
  654. RunTest(@"
  655. s.Color = colors.Blue | colors.Green;
  656. assert(s.Color === colors.Blue | colors.Green);
  657. ");
  658. Assert.Equal(Colors.Blue | Colors.Green, s.Color);
  659. }
  660. [Fact]
  661. public void EnumIsConvertedToNumber()
  662. {
  663. var o = new
  664. {
  665. r = Colors.Red,
  666. b = Colors.Blue,
  667. g = Colors.Green
  668. };
  669. _engine.SetValue("o", o);
  670. RunTest(@"
  671. assert(o.r === 0);
  672. assert(o.g === 1);
  673. assert(o.b === 10);
  674. ");
  675. }
  676. [Fact]
  677. public void ShouldConvertToEnum()
  678. {
  679. var s = new Circle
  680. {
  681. Color = Colors.Red,
  682. };
  683. _engine.SetValue("s", s);
  684. RunTest(@"
  685. assert(s.Color === 0);
  686. s.Color = 10;
  687. assert(s.Color === 10);
  688. ");
  689. _engine.SetValue("s", s);
  690. RunTest(@"
  691. s.Color = 11;
  692. assert(s.Color === 11);
  693. ");
  694. Assert.Equal(Colors.Blue | Colors.Green, s.Color);
  695. }
  696. }
  697. }