/Runtime/Tests/HostingTest/ObjectOperationsTest.cs

http://github.com/IronLanguages/main · C# · 719 lines · 413 code · 139 blank · 167 comment · 0 complexity · b57aaf7a4b9fac2afe0f19fad4491cfa MD5 · raw file

  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Runtime.Remoting;
  18. using IronPython.Runtime;
  19. using IronPython.Runtime.Exceptions;
  20. using Microsoft.Scripting;
  21. using Microsoft.Scripting.Hosting;
  22. using NUnit.Framework;
  23. namespace HostingTest {
  24. using Assert = NUnit.Framework.Assert;
  25. [TestFixture]
  26. public partial class ObjectOperationsTest : HAPITestBase {
  27. [Test]
  28. //[Ignore] // Bug # 479046
  29. public void Engine_GetAccess() {
  30. // Setup....
  31. ObjectOperations objOps = _testEng.Operations;
  32. ScriptEngine engine = objOps.Engine;
  33. }
  34. /// <summary>
  35. /// Test : Null object
  36. /// Expected : ArgumentNullException
  37. /// </summary>
  38. [Test]
  39. [Negative]
  40. public void IsCallable_NullObjectArgument() {
  41. _testEng.Operations.IsCallable((object)null);
  42. }
  43. [Test]
  44. [Negative]
  45. [ExpectedException(typeof(ArgumentNullException))]
  46. public void IsCallable_NullObjectHandleArgument() {
  47. _testEng.Operations.IsCallable((ObjectHandle)null);
  48. }
  49. /// <summary>
  50. /// Test : Obj of a delegate instance
  51. /// Expected : True
  52. ///
  53. /// Notes : Look for example in existing code.
  54. /// </summary>
  55. [Test]
  56. public void IsCallable_ObjOfDelegateInstance() {
  57. // Setup tests
  58. // Create scope
  59. ScriptScope scope = _testEng.CreateScope();
  60. ScriptSource code = _testEng.CreateScriptSourceFromString(_codeSnippets[CodeType.IsOddFunction], SourceCodeKind.Statements);
  61. // Execute source in scope
  62. code.Execute(scope);
  63. //Get a Delegate Instance using the Func<> Generic declaration and GetVariable
  64. Func<int, bool> isodd = scope.GetVariable<Func<int, bool>>("isodd");
  65. Assert.IsTrue(_testEng.Operations.IsCallable(isodd));
  66. // Call the function to validate IsCallable
  67. Assert.IsTrue(isodd(1));
  68. Assert.IsFalse(isodd(2));
  69. //Get a Delegate Instance using the Func<> Generic declaration and GetVariable
  70. var isodd2 = scope.GetVariable<F1<int, bool>>("isodd");
  71. Assert.IsTrue(_testEng.Operations.IsCallable(isodd2));
  72. // Call the function to validate IsCallable
  73. Assert.IsTrue(isodd2(1));
  74. Assert.IsFalse(isodd2(2));
  75. }
  76. private delegate TRet F1<T1, TRet>(T1 value);
  77. /// <summary>
  78. /// Test : Null object
  79. /// Expected : ArgumentNullException
  80. /// </summary>
  81. [Test]
  82. [Negative]
  83. [ExpectedException(typeof(ArgumentTypeException))]
  84. public void Call_NullObject() {
  85. object[] parameters = new object[]{"-a", "-b", "-c", "foo.py"};
  86. _testEng.Operations.Invoke((object)null,parameters);
  87. }
  88. /// <summary>
  89. /// Test : Null object[]
  90. /// Expected : ArgumentNullException
  91. /// </summary>
  92. [Test]
  93. [Negative]
  94. [ExpectedException(typeof(NullReferenceException))]
  95. public void Call_NullObjectParams() {
  96. String varName = "pyf";
  97. object fooFun = GetVariableValue(_codeSnippets[CodeType.SimpleMethod],
  98. varName);
  99. _testEng.Operations.Invoke(fooFun, (object[])null);
  100. }
  101. [Test]
  102. [Negative]
  103. [ExpectedException(typeof(MissingMemberException))]
  104. public void GetMember_NullObject() {
  105. _testEng.Operations.GetMember((object)null, "foo");
  106. }
  107. [Test]
  108. [Negative]
  109. [ExpectedException(typeof(ArgumentNullException))]
  110. public void GetMember_NullName() {
  111. string varName = "FooClass";
  112. object FooClass = GetVariableValue(_codeSnippets[CodeType.SimpleFooClassDefinition],
  113. varName);
  114. _testEng.Operations.GetMember(FooClass, (string)null);
  115. }
  116. [Test]
  117. [Negative]
  118. [ExpectedException(typeof(MissingMemberException))]
  119. public void GenericGetMember_NullObject() {
  120. _testEng.Operations.GetMember<string>((object)null, "foo");
  121. }
  122. [Test]
  123. [Negative]
  124. [ExpectedException(typeof(ArgumentNullException))]
  125. public void GenericGetMember_NullName() {
  126. String varName = "FooClass";
  127. object fooClasObj = GetVariableValue(_codeSnippets[CodeType.SimpleFooClassDefinition],
  128. varName);
  129. _testEng.Operations.GetMember<string>(fooClasObj, (string)null);
  130. }
  131. [Test]
  132. [Negative]
  133. public void TryGetMember_NullObject() {
  134. // Spec say this should throw NullArgumentException for null object or null name
  135. object outObj;
  136. Assert.IsFalse(_testEng.Operations.TryGetMember((object)null, "foo", out outObj));
  137. }
  138. [Test]
  139. [Negative]
  140. [ExpectedException(typeof(ArgumentNullException))]
  141. public void TryGetMember_NullName() {
  142. String varName = "FooClass";
  143. object fooClassObj = GetVariableValue(_codeSnippets[CodeType.SimpleFooClassDefinition],
  144. varName);
  145. object outObj;
  146. _testEng.Operations.TryGetMember(fooClassObj, (string)null, out outObj);
  147. }
  148. /// <summary>
  149. /// Test : Do a simple opperations that does not exist in the current language
  150. /// Expected : Raise the correct exception
  151. /// </summary>
  152. [Negative]
  153. [Test]
  154. [ExpectedException(typeof(TypeErrorException))]
  155. public void DoOperation_NoOperatorForLanguage() {
  156. object expectedResult = 2;
  157. String varName = "x";
  158. object objectVar = GetVariableValue(_codeSnippets[CodeType.OneLineAssignmentStatement],
  159. varName);
  160. object result = _testEng.Operations.DoOperation(System.Linq.Expressions.ExpressionType.Decrement, objectVar);
  161. Assert.AreEqual(expectedResult, result);
  162. }
  163. /// <summary>
  164. /// Test : GetCallSignatures with null object
  165. /// Expected : Returns empty string array
  166. /// </summary>
  167. [Test]
  168. public void GetCallSignature_NullArg() {
  169. int expectedResult = 0;
  170. string[] result = (string[])_testEng.Operations.GetCallSignatures((object)null);
  171. // Should be empty array of Length zero
  172. Assert.AreEqual(result.Length, expectedResult);
  173. }
  174. [Test]
  175. [Negative]
  176. public void GetMemberNames_NullObject() {
  177. _testEng.Operations.GetMemberNames((object)null);
  178. }
  179. [Test]
  180. public void GetMemberNames_MemberObjects() {
  181. List<string> expectedResult = new List<string>() { "concat", "add", "__doc__", "__module__", "__init__", "f", "someInstanceAttribute" };
  182. String varName = "FooClass";
  183. object tmpFoo = GetVariableValue(_codeSnippets[CodeType.SimpleFooClassDefinition],
  184. varName);
  185. object FooClass = _testEng.Operations.Invoke(tmpFoo); // create new FooClass
  186. // BUG - Not same return value as Spec
  187. List<string> result = new List<string>(_testEng.Operations.GetMemberNames(FooClass));
  188. // Verify the list is equal
  189. Assert.AreEqual(result.Count, expectedResult.Count);
  190. result.ForEach(delegate(string name) {
  191. Assert.IsTrue(expectedResult.Contains(name));
  192. });
  193. }
  194. [Test]
  195. public void GetCallSignature_PassValidClassObject() {
  196. // depending on how the object stores objects
  197. // internally python stores 4 members maybe?
  198. string[] expectedResult = new string[4];
  199. String varName = "FooClass";
  200. object objectVar = GetVariableValue(_codeSnippets[CodeType.SimpleFooClassDefinition],
  201. varName);
  202. object fooClass = _testEng.Operations.Invoke(objectVar); // create new FooClass
  203. string[] result = (string[])_testEng.Operations.GetCallSignatures(fooClass);
  204. Assert.AreEqual(result.Length, expectedResult.Length);
  205. }
  206. [Test]
  207. public void GetCallSignature_PassValidMethodObject() {
  208. string varName = "concat";
  209. string[] expectedResult = new string[] { "concat(a, b, c)" };
  210. object concat = GetVariableValue(_codeSnippets[CodeType.MethodWithThreeArgs],
  211. varName);
  212. ValidateCallSignatures(concat, expectedResult);
  213. }
  214. /// <summary>
  215. /// Test :
  216. /// Expected :
  217. /// </summary>
  218. [Test]
  219. public void ConvertTo_IntegerToDouble() {
  220. // Setup input values
  221. string lookupObjectName = "x";
  222. double expectedValue = 3;
  223. // Setup Input Source, return object
  224. object objectInScope = GetVariableValue(_codeSnippets[CodeType.OneLineAssignmentStatement],
  225. lookupObjectName);
  226. // Verify Convertions with expected value and type
  227. ValidateConvertTo(objectInScope, expectedValue);
  228. }
  229. [Test]
  230. [Negative]// Bug # 478206
  231. [ExpectedException(typeof(TypeErrorException))]
  232. public void GenericConvertTo_NullObject(){
  233. Double newValue = _testEng.Operations.ConvertTo<Double>((object)null);
  234. }
  235. [Test]
  236. [Negative]// Bug # 478206
  237. [ExpectedException(typeof(TypeErrorException))]
  238. public void ConvertTo_NullObject() {
  239. object newValue = _testEng.Operations.ConvertTo((object)null, typeof(int));
  240. }
  241. [Test]
  242. [Negative]
  243. [ExpectedException(typeof(ArgumentNullException))]
  244. public void ConvertTo_NullType() {
  245. // Lookup value in scope
  246. string lookupObjectName = "x";
  247. // Get object from the scope
  248. object ScopeObjectValue = GetVariableValue(_codeSnippets[CodeType.OneLineAssignmentStatement],
  249. lookupObjectName);
  250. // Test Null value arg exception
  251. object newValue = _testEng.Operations.ConvertTo(ScopeObjectValue, (Type)null);
  252. }
  253. /// <summary>
  254. /// Test :
  255. /// Expected :
  256. /// </summary>
  257. [Test]
  258. public void GenericConvertTo_IntegerToDouble() {
  259. string lookupObjectName = "x";
  260. double expectedValue = 3;
  261. object objectInScope = GetVariableValue(_codeSnippets[CodeType.OneLineAssignmentStatement],
  262. lookupObjectName);
  263. ValidateConvertTo<double>(objectInScope, expectedValue);
  264. }
  265. /// <summary>
  266. /// Test : Same cases as ConvertTo<T>
  267. /// Expected : Returns true on successful conversion, and on failure returns false and result of T.default
  268. /// </summary>
  269. [Test]
  270. public void GenericTryConvertTo_IntegerToDouble(){
  271. string lookupObjectName = "x";
  272. double expectedValue = 3;
  273. object objectInScope = GetVariableValue(_codeSnippets[CodeType.OneLineAssignmentStatement],
  274. lookupObjectName);
  275. ValidateTryConvertTo<double>(objectInScope, expectedValue);
  276. }
  277. [Test]
  278. [Negative]
  279. [ExpectedException(typeof(MissingMemberException))]
  280. public void SetMember_NullObjectArg(){
  281. (_testEng.CreateOperations()).SetMember((object)null, "foo", 0);
  282. }
  283. [Test]
  284. [Negative]
  285. [ExpectedException(typeof(ArgumentNullException))]
  286. public void SetMember_NullNameArg() {
  287. (_testEng.CreateOperations()).SetMember("foo", (string)null, 0);
  288. }
  289. /// <summary>
  290. /// Test : For each Operations language and each source language,
  291. /// set an existing and settable member
  292. /// Expected : The member is updated with the new value
  293. ///
  294. /// Note : This code fails for languages that do not have read/write objects!
  295. /// </summary>
  296. [Test]
  297. public void SetMember_BasicMemberTest() {
  298. // BUG - File bug/Investigate either I don't understand or this is a bug.
  299. // Starting over - this method needs a object member to be operated on not just
  300. // any member in the scope.
  301. string lookupClassVarName = "FooClass";
  302. string lookupMemberName = "add";
  303. object objectVar = GetVariableValue(_codeSnippets[CodeType.SimpleFooClassDefinition],
  304. lookupClassVarName);
  305. object fooClass = _testEng.Operations.Invoke(objectVar); // create new FooClass
  306. // Verify that the member function exists
  307. Assert.IsTrue(_testEng.Operations.ContainsMember(fooClass, lookupMemberName));
  308. // Set the member
  309. // Get some new code to put in replace/set.
  310. ScriptScope scope = _testEng.CreateScope();
  311. ScriptSource code = _testEng.CreateScriptSourceFromString("new_add=1", SourceCodeKind.Statements);
  312. code.Execute(scope);
  313. object newObjectValue = scope.GetVariable("new_add");
  314. _testEng.Operations.SetMember(fooClass, lookupMemberName, newObjectValue);
  315. // Verify that the member does not exists anymore
  316. Assert.IsFalse(_testEng.Operations.ContainsMember(fooClass, "new_add"));
  317. }
  318. /// <summary>
  319. /// Test : Lookup the a member of an object
  320. /// Expected : validate that ContainsMember returns true for a known member
  321. /// </summary>
  322. [Test]
  323. public void ContainsMember_BasicLookup() {
  324. string lookupClassVarName = "FooClass";
  325. string lookupMemberName = "add";
  326. object objectVar = GetVariableValue(_codeSnippets[CodeType.SimpleFooClassDefinition],
  327. lookupClassVarName);
  328. object fooClass = _testEng.Operations.Invoke(objectVar); // create new FooClass
  329. Assert.IsTrue(_testEng.Operations.ContainsMember(fooClass, lookupMemberName));
  330. }
  331. /// <summary>
  332. /// Test : Lookup the a member that is not in an object
  333. /// Expected : validate that ContainsMember returns false
  334. /// </summary>
  335. [Negative]
  336. [Test]
  337. public void ContainsMember_LookForMemberThatDoesNotExist() {
  338. string lookupClassVarName = "FooClass";
  339. string lookupMemberName = "__zzzzzkdsloopqqqq___";
  340. object objectVar = GetVariableValue(_codeSnippets[CodeType.SimpleFooClassDefinition],
  341. lookupClassVarName);
  342. object fooClass = _testEng.Operations.Invoke(objectVar); // create new FooClass
  343. Assert.IsFalse(_testEng.Operations.ContainsMember(fooClass, lookupMemberName));
  344. }
  345. /// <summary>
  346. /// Test : Null object
  347. /// Expected : ArgumentNullException
  348. /// </summary>
  349. [Negative]
  350. [Test]
  351. [ExpectedException(typeof(MissingMemberException))]
  352. public void RemoveMember_NullObjectArg(){
  353. _testEng.Operations.RemoveMember((object)null, "x");
  354. }
  355. /// <summary>
  356. /// Test : Null object
  357. /// Expected : ArgumentNullException
  358. /// </summary>
  359. [Negative]
  360. [Test]
  361. [ExpectedException(typeof(ArgumentNullException))]
  362. public void RemoveMember_NullStringNameArg() {
  363. // BUG - investigate/file bug
  364. string lookupName = "x";
  365. object objectInScope = GetVariableValue(_codeSnippets[CodeType.OneLineAssignmentStatement], lookupName);
  366. _testEng.Operations.RemoveMember(objectInScope, (string)null);
  367. }
  368. /// <summary>
  369. /// Test : Create a FooClass object in Hosted script language
  370. /// Expected : Verify that ObjectOperations is removed
  371. ///
  372. /// Notes : This will not work for every language but it should work for
  373. /// Python. The Object Operations RemoveMember would be equivelent to
  374. /// this Python:
  375. /// class FooClass:
  376. /// def inc(self, x):
  377. /// return x+1;
  378. /// N=FooClass()
  379. /// N.inc(4) ==> 5
  380. /// del N.inc(4)
  381. /// N.inc(4) ==> Error
  382. ///
  383. /// </summary>
  384. [Test]
  385. public void RemoveMember_BaiscObjectRemovalFn() {
  386. string lookupClassVarName = "FooClass";
  387. string lookupMemberName = "someInstanceAttribute";
  388. object objectVar = GetVariableValue(_codeSnippets[CodeType.SimpleFooClassDefinition],
  389. lookupClassVarName);
  390. object fooClass = _testEng.Operations.Invoke(objectVar); // create new FooClass
  391. // Verify that the member function exists
  392. Assert.IsTrue(_testEng.Operations.ContainsMember(fooClass, lookupMemberName));
  393. // Remove the member function
  394. _testEng.Operations.RemoveMember(fooClass, lookupMemberName);
  395. // Verify that the member does not exists anymore
  396. Assert.IsFalse(_testEng.Operations.ContainsMember(fooClass, lookupMemberName));
  397. }
  398. /// <summary>
  399. /// Test : remove member from object
  400. /// Expected : object is remove
  401. /// Note : this might not make sense for vars but only class members.
  402. /// </summary>
  403. [Test]
  404. [Ignore] // Bug # 478257 - This might not be valid though IP code like "x=4\ndel x\n" works in IP
  405. public void RemoveMember_BaiscObjectRemovalVar() {
  406. // BUG - investigate/file bug
  407. string varName = "x";
  408. object objectVar = GetVariableValue(_codeSnippets[CodeType.OneLineAssignmentStatement],
  409. varName);
  410. _testEng.Operations.RemoveMember(objectVar, varName);
  411. }
  412. /// <summary>
  413. /// Test : GetMembers of imported module from script
  414. /// Expected : check subset of member names exist
  415. /// </summary>
  416. [Test]
  417. public void GetMemberNames_LocalFromImportedModule() {
  418. // Setup tests
  419. string varName = "date";
  420. // Get sub set of members that are not likely to change
  421. List<string> expectedPyModuleDateTimeMembersSubSet = new List<string>(){
  422. "astimezone", "combine", "ctime", "date", "day", "dst",
  423. "fromordinal", "fromtimestamp", "hour",
  424. "isocalendar", "isoformat", "isoweekday", "max", "microsecond", "min",
  425. "minute", "month", "now", "replace", "resolution",
  426. "second", "strftime", "time", "timetuple", "timetz", "today",
  427. "toordinal", "tzinfo", "tzname", "utcfromtimestamp",
  428. "utcnow", "utcoffset", "utctimetuple", "weekday", "year"};
  429. // Setup the date time dot net object
  430. object dotNetObject = GetVariableValue(_codeSnippets[CodeType.ImportCPythonDateTimeModule],
  431. varName);
  432. // Verify that this is the Spec'd signature
  433. List<string> members = new List<string>(_testEng.Operations.GetMemberNames(dotNetObject));
  434. // Verify a subset exists in members
  435. expectedPyModuleDateTimeMembersSubSet.ForEach(delegate(string name) {
  436. Assert.IsTrue(members.Contains(name));
  437. });
  438. //Assert.AreEqual(result.Length, expectedResult.Length);
  439. }
  440. /// <summary>
  441. /// Test : Pass null value to method
  442. /// Expected : empty string return
  443. /// </summary>
  444. [Test]
  445. [Negative]
  446. [ExpectedException(typeof(NullReferenceException))]
  447. public void GetDocumentation_NullParameter() {
  448. string doc = _testEng.Operations.GetDocumentation((object)null);
  449. Assert.AreEqual(0, doc.Length);
  450. }
  451. [Test]
  452. public void GetDocumentation_AssemblyModule() {
  453. ///Setup
  454. string varName = "date";
  455. // Expected value
  456. string expectedDocs = "datetime(year: int, month: int, day: int, hour: int, minute: int, second: int, microsecond: int, tzinfo: tzinfo)";
  457. object objectVar = GetVariableValue(_codeSnippets[CodeType.ImportCPythonDateTimeModule],
  458. varName);
  459. // Get the associated documentation
  460. string doc = _testEng.Operations.GetDocumentation(objectVar);
  461. // Verify values
  462. Assert.IsTrue( doc.Contains(expectedDocs));
  463. }
  464. [Test]
  465. public void GetDocumentation_FromScriptMethod() {
  466. string varName = "doc";
  467. // Expected value
  468. string expectedDocs = "This function does nothing";
  469. object objectVar = GetVariableValue(_codeSnippets[CodeType.MethodWithDocumentationAttached],
  470. varName);
  471. string doc = _testEng.Operations.GetDocumentation(objectVar);
  472. Assert.AreEqual(expectedDocs, doc);
  473. }
  474. /// <summary>
  475. /// Test : Import .Net DateTime assembly
  476. /// Expected : Get the correct doc string attached
  477. ///
  478. /// Note : Could be a problem if the .Net version changes and this specific doc string is changed
  479. /// </summary>
  480. [Test]
  481. public void GetDocumentation_FromDotNetObject() {
  482. string varName = "DotNetDate";
  483. ScriptScope scope = _testEng.CreateScope();
  484. ScriptSource code = _testEng.CreateScriptSourceFromString(_codeSnippets[CodeType.ImportDotNetAssemblyDateTimeModule], SourceCodeKind.Statements);
  485. code.Execute(scope);
  486. // This could break if the Underlining DotNet documentation changes.
  487. //http://dlr.codeplex.com/WorkItem/View.aspx?WorkItemId=6071
  488. //string expectedDocs = "Represents an instant in time, typically expressed as a date and time of day";
  489. string expectedDocs = "DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, calendar: Calendar, kind: DateTimeKind)";
  490. object testObject = scope.GetVariable(varName);
  491. string doc = _testEng.Operations.GetDocumentation(testObject);
  492. Assert.IsTrue(doc.Contains(expectedDocs));
  493. }
  494. [Test]
  495. public void AddMethod() {
  496. //System.Dynamic.Runtime.Operators.And
  497. object left = 4;
  498. object right = 3;
  499. object expectedResult = (object)(((int)left) + ((int)right));
  500. object result = _testEng.Operations.Add(left, right);
  501. Assert.AreEqual(expectedResult, result );
  502. }
  503. [Test]
  504. public void SubMethod() {
  505. object left = 4;
  506. object right = 3;
  507. object expectedResult = (object)(((int)left) - ((int)right));
  508. object result = _testEng.Operations.Subtract(left, right);
  509. Assert.AreEqual(expectedResult, result);
  510. }
  511. /// <summary>
  512. /// Test : Test env change using engine.CreateOperations with module change in scope
  513. /// Expected : New env change should give correct __future__ division type.
  514. /// </summary>
  515. [Test]
  516. [Ignore] // BUG 476154
  517. public void TestFromFuture_UsingOperations() {
  518. ScriptRuntime sr = CreateRuntime();
  519. ScriptScope futureScope = _testEng.CreateScope();
  520. futureScope.SetVariable("division", true);
  521. sr.Globals.SetVariable("__future__", futureScope);
  522. ScriptSource source = _testEng.CreateScriptSourceFromString(_codeSnippets[CodeType.ImportFutureDiv],
  523. SourceCodeKind.Statements);
  524. ScriptScope localScope = _testEng.CreateScope();
  525. source.Execute(localScope);
  526. ObjectOperations operation = _testEng.CreateOperations(localScope);
  527. // now do div operations and check result
  528. object divResult = operation.Divide(1, 2);
  529. // if this is future style then the result should be 0.5
  530. Assert.AreEqual( 0.5, (double)divResult);
  531. }
  532. // Bug # 466027 - regression test
  533. [Test]
  534. public void FormatException_Test()
  535. {
  536. ScriptEngine engine = _runtime.GetEngine("py");
  537. ExceptionOperations es = engine.GetService<ExceptionOperations>();
  538. OutOfMemoryException e = new OutOfMemoryException();
  539. string result = es.FormatException(e);
  540. Assert.AreNotEqual(e.Message, result);
  541. }
  542. [Test]
  543. public void Create_Op2()
  544. {
  545. ScriptEngine engine = _runtime.GetEngine("py");
  546. ScriptScope scope = engine.CreateScope();
  547. ObjectOperations operation = engine.CreateOperations(scope);
  548. string pyCode = @"class TC(object):
  549. i = -1
  550. def what(self):
  551. return 1";
  552. ScriptSource src = engine.CreateScriptSourceFromString(pyCode, SourceCodeKind.Statements);
  553. src.Execute(scope);
  554. object FooClass = scope.GetVariable("TC");
  555. object[] param = new object[] { };
  556. object newObjectInstance = engine.Operations.CreateInstance(FooClass, param); // create new FooClass
  557. var what = engine.Operations.GetMember<Func<int>>(newObjectInstance, "what");
  558. int n = what();
  559. }
  560. [Test]// BUG # 479046 - regression test
  561. public void Create_Op1(){
  562. ScriptEngine engine = _runtime.GetEngine("py");
  563. ScriptScope scope = engine.CreateScope();
  564. ObjectOperations operation = engine.CreateOperations(scope);
  565. // now do div operations and check result
  566. // "x = object()"
  567. string pyCode = @"class TC(object):
  568. i = -1
  569. def what(self):
  570. return 1";
  571. ScriptSource src = engine.CreateScriptSourceFromString(pyCode, SourceCodeKind.Statements);
  572. src.Execute(scope);
  573. object FooClass = scope.GetVariable("TC");
  574. object[] param = new object[] { };
  575. object testClassInstance0 = engine.Operations.CreateInstance(FooClass, param); // create new FooClass
  576. object testClassInstance1 = engine.Operations.CreateInstance(FooClass, param); // create new FooClass
  577. Assert.AreNotEqual(testClassInstance0, testClassInstance1);
  578. }
  579. }
  580. }