InteropTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. using System;
  2. using System.Collections.Generic;
  3. using Jint.Native;
  4. using Jint.Native.Object;
  5. using Xunit;
  6. namespace Jint.Tests.Runtime
  7. {
  8. public class InteropTests : IDisposable
  9. {
  10. private readonly Engine _engine;
  11. public InteropTests()
  12. {
  13. _engine = new Engine()
  14. .SetValue("log", new Action<object>(Console.WriteLine))
  15. .SetValue("assert", new Action<bool>(Assert.True))
  16. ;
  17. }
  18. void IDisposable.Dispose()
  19. {
  20. }
  21. private void RunTest(string source)
  22. {
  23. _engine.Execute(source);
  24. }
  25. [Fact]
  26. public void PrimitiveTypesCanBeSet()
  27. {
  28. _engine.SetValue("x", 10);
  29. _engine.SetValue("y", true);
  30. _engine.SetValue("z", "foo");
  31. RunTest(@"
  32. assert(x === 10);
  33. assert(y === true);
  34. assert(z === 'foo');
  35. ");
  36. }
  37. [Fact]
  38. public void DelegatesCanBeSet()
  39. {
  40. _engine.SetValue("square", new Func<double, double>(x => x * x));
  41. RunTest(@"
  42. assert(square(10) === 100);
  43. ");
  44. }
  45. [Fact]
  46. public void CanGetObjectProperties()
  47. {
  48. var p = new Person
  49. {
  50. Name = "Mickey Mouse"
  51. };
  52. _engine.SetValue("p", p);
  53. RunTest(@"
  54. assert(p.Name === 'Mickey Mouse');
  55. ");
  56. }
  57. [Fact]
  58. public void CanInvokeObjectMethods()
  59. {
  60. var p = new Person
  61. {
  62. Name = "Mickey Mouse"
  63. };
  64. _engine.SetValue("p", p);
  65. RunTest(@"
  66. assert(p.ToString() === 'Mickey Mouse');
  67. ");
  68. }
  69. [Fact]
  70. public void CanSetObjectProperties()
  71. {
  72. var p = new Person
  73. {
  74. Name = "Mickey Mouse"
  75. };
  76. _engine.SetValue("p", p);
  77. RunTest(@"
  78. p.Name = 'Donald Duck';
  79. assert(p.Name === 'Donald Duck');
  80. ");
  81. Assert.Equal("Donald Duck", p.Name);
  82. }
  83. [Fact]
  84. public void CanAccessAnonymousObject()
  85. {
  86. var p = new
  87. {
  88. Name = "Mickey Mouse",
  89. };
  90. _engine.SetValue("p", p);
  91. RunTest(@"
  92. assert(p.Name === 'Mickey Mouse');
  93. ");
  94. }
  95. [Fact]
  96. public void CanAccessAnonymousObjectProperties()
  97. {
  98. var p = new
  99. {
  100. Address = new
  101. {
  102. City = "Mouseton"
  103. }
  104. };
  105. _engine.SetValue("p", p);
  106. RunTest(@"
  107. assert(p.Address.City === 'Mouseton');
  108. ");
  109. }
  110. [Fact]
  111. public void PocosCanReturnJsValueDirectly()
  112. {
  113. var o = new
  114. {
  115. x = new JsValue(1),
  116. y = new JsValue("string"),
  117. };
  118. _engine.SetValue("o", o);
  119. RunTest(@"
  120. assert(o.x === 1);
  121. assert(o.y === 'string');
  122. ");
  123. }
  124. [Fact]
  125. public void PocosCanReturnObjectInstanceDirectly()
  126. {
  127. var x = new ObjectInstance(_engine) { Extensible = true};
  128. x.Put("foo", new JsValue("bar"), false);
  129. var o = new
  130. {
  131. x
  132. };
  133. _engine.SetValue("o", o);
  134. RunTest(@"
  135. assert(o.x.foo === 'bar');
  136. ");
  137. }
  138. [Fact]
  139. public void DateTimeIsConvertedToDate()
  140. {
  141. var o = new
  142. {
  143. z = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
  144. };
  145. _engine.SetValue("o", o);
  146. RunTest(@"
  147. assert(o.z.valueOf() === 0);
  148. ");
  149. }
  150. [Fact]
  151. public void EcmaValuesAreAutomaticallyConvertedWhenSetInPoco()
  152. {
  153. var p = new Person
  154. {
  155. Name = "foo",
  156. };
  157. _engine.SetValue("p", p);
  158. RunTest(@"
  159. assert(p.Name === 'foo');
  160. assert(p.Age === 0);
  161. p.Name = 'bar';
  162. p.Age = 10;
  163. ");
  164. Assert.Equal("bar", p.Name);
  165. Assert.Equal(10, p.Age);
  166. }
  167. [Fact]
  168. public void EcmaValuesAreAutomaticallyConvertedToBestMatchWhenSetInPoco()
  169. {
  170. var p = new Person
  171. {
  172. Name = "foo",
  173. };
  174. _engine.SetValue("p", p);
  175. RunTest(@"
  176. p.Name = 10;
  177. p.Age = '20';
  178. ");
  179. Assert.Equal("10", p.Name);
  180. Assert.Equal(20, p.Age);
  181. }
  182. [Fact]
  183. public void ShouldCallInstanceMethodWithoutArgument()
  184. {
  185. _engine.SetValue("a", new A());
  186. RunTest(@"
  187. assert(a.Call1() === 0);
  188. ");
  189. }
  190. [Fact]
  191. public void ShouldCallInstanceMethodOverloadArgument()
  192. {
  193. _engine.SetValue("a", new A());
  194. RunTest(@"
  195. assert(a.Call1(1) === 1);
  196. ");
  197. }
  198. [Fact]
  199. public void ShouldCallInstanceMethodWithString()
  200. {
  201. var p = new Person();
  202. _engine.SetValue("a", new A());
  203. _engine.SetValue("p", p);
  204. RunTest(@"
  205. p.Name = a.Call2('foo');
  206. assert(p.Name === 'foo');
  207. ");
  208. Assert.Equal("foo", p.Name);
  209. }
  210. [Fact]
  211. public void CanUseTrim()
  212. {
  213. var p = new Person { Name = "Mickey Mouse "};
  214. _engine.SetValue("p", p);
  215. RunTest(@"
  216. assert(p.Name === 'Mickey Mouse ');
  217. p.Name = p.Name.trim();
  218. assert(p.Name === 'Mickey Mouse');
  219. ");
  220. Assert.Equal("Mickey Mouse", p.Name);
  221. }
  222. [Fact]
  223. public void CanUseMathFloor()
  224. {
  225. var p = new Person();
  226. _engine.SetValue("p", p);
  227. RunTest(@"
  228. p.Age = Math.floor(1.6);p
  229. assert(p.Age === 1);
  230. ");
  231. Assert.Equal(1, p.Age);
  232. }
  233. [Fact]
  234. public void CanUseDelegateAsFunction()
  235. {
  236. var even = new Func<int, bool>(x => x % 2 == 0);
  237. _engine.SetValue("even", even);
  238. RunTest(@"
  239. assert(even(2) === true);
  240. ");
  241. }
  242. [Fact]
  243. public void ShouldConvertArrayToArrayInstance()
  244. {
  245. var result = _engine
  246. .SetValue("values", new [] {1,2,3,4,5,6})
  247. .Execute("values.filter(function(x){ return x % 2 == 0; })");
  248. var parts = result.ToObject();
  249. Assert.True(parts.GetType().IsArray);
  250. Assert.Equal(3, ((object[])parts).Length);
  251. Assert.Equal(2d, ((object[])parts)[0]);
  252. Assert.Equal(4d, ((object[])parts)[1]);
  253. Assert.Equal(6d, ((object[])parts)[2]);
  254. }
  255. [Fact]
  256. public void ShouldConvertArrayInstanceToArray()
  257. {
  258. var result = _engine.Execute("'[email protected]'.split('@');");
  259. var parts = result.ToObject();
  260. Assert.True(parts.GetType().IsArray);
  261. Assert.Equal(2, ((object[])parts).Length);
  262. Assert.Equal("foo", ((object[])parts)[0]);
  263. Assert.Equal("bar.com", ((object[])parts)[1]);
  264. }
  265. [Fact]
  266. public void ShouldConvertBooleanInstanceToBool()
  267. {
  268. var result = _engine.Execute("new Boolean(true)");
  269. var value = result.ToObject();
  270. Assert.Equal(typeof(bool), value.GetType());
  271. Assert.Equal(true, value);
  272. }
  273. [Fact]
  274. public void ShouldConvertDateInstanceToDateTime()
  275. {
  276. var result = _engine.Execute("new Date(0)");
  277. var value = result.ToObject();
  278. Assert.Equal(typeof(DateTime), value.GetType());
  279. Assert.Equal(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), value);
  280. }
  281. [Fact]
  282. public void ShouldConvertNumberInstanceToDouble()
  283. {
  284. var result = _engine.Execute("new Number(10)");
  285. var value = result.ToObject();
  286. Assert.Equal(typeof(double), value.GetType());
  287. Assert.Equal(10d, value);
  288. }
  289. [Fact]
  290. public void ShouldConvertStringInstanceToString()
  291. {
  292. var result = _engine.Execute("new String('foo')");
  293. var value = result.ToObject();
  294. Assert.Equal(typeof(string), value.GetType());
  295. Assert.Equal("foo", value);
  296. }
  297. [Fact]
  298. public void ShouldConvertObjectInstanceToExpando()
  299. {
  300. _engine.Execute("var o = {a: 1, b: 'foo'}");
  301. var result = _engine.GetGlobalValue("o");
  302. dynamic value = result.ToObject();
  303. Assert.Equal(1, value.a);
  304. Assert.Equal("foo", value.b);
  305. var dic = (IDictionary<string, object>)result.ToObject();
  306. Assert.Equal(1d, dic["a"]);
  307. Assert.Equal("foo", dic["b"]);
  308. }
  309. [Fact]
  310. public void ShouldNotTryToConvertCompatibleTypes()
  311. {
  312. _engine.SetValue("a", new A());
  313. RunTest(@"
  314. assert(a.Call3('foo') === 'foo');
  315. assert(a.Call3(1) === '1');
  316. ");
  317. }
  318. [Fact]
  319. public void ShouldNotTryToConvertDerivedTypes()
  320. {
  321. _engine.SetValue("a", new A());
  322. _engine.SetValue("p", new Person { Name = "Mickey" });
  323. RunTest(@"
  324. assert(a.Call4(p) === 'Mickey');
  325. ");
  326. }
  327. [Fact]
  328. public void ShouldExecuteFunctionCallBackAsDelegate()
  329. {
  330. _engine.SetValue("a", new A());
  331. RunTest(@"
  332. assert(a.Call5(function(a,b){ return a+b }) === '1foo');
  333. ");
  334. }
  335. [Fact]
  336. public void ShouldExecuteFunctionCallBackAsFuncAndThisCanBeAssigned()
  337. {
  338. _engine.SetValue("a", new A());
  339. RunTest(@"
  340. assert(a.Call6(function(a,b){ return this+a+b }) === 'bar1foo');
  341. ");
  342. }
  343. public interface IPerson
  344. {
  345. string Name { get; }
  346. }
  347. public class Person : IPerson
  348. {
  349. public string Name { get; set; }
  350. public int Age { get; set; }
  351. public override string ToString()
  352. {
  353. return Name;
  354. }
  355. }
  356. public class A
  357. {
  358. public int Call1()
  359. {
  360. return 0;
  361. }
  362. public int Call1(int x)
  363. {
  364. return x;
  365. }
  366. public string Call2(string x)
  367. {
  368. return x;
  369. }
  370. public string Call3(object x)
  371. {
  372. return x.ToString();
  373. }
  374. public string Call4(IPerson x)
  375. {
  376. return x.ToString();
  377. }
  378. public string Call5(Delegate callback)
  379. {
  380. var thisArg = JsValue.Undefined;
  381. var arguments = new JsValue[] { 1, "foo" };
  382. return callback.DynamicInvoke(thisArg, arguments).ToString();
  383. }
  384. public string Call6(Func<JsValue, JsValue[], JsValue> callback)
  385. {
  386. var thisArg = new JsValue("bar");
  387. var arguments = new JsValue[] { 1, "foo" };
  388. return callback(thisArg, arguments).ToString();
  389. }
  390. }
  391. }
  392. }