PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Tools/IronStudio/UnitTests/Program.cs

http://github.com/IronLanguages/main
C# | 147 lines | 86 code | 15 blank | 46 comment | 4 complexity | 0c6f415cacdd97331d641cec6c289361 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  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. * ironpy@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. using System;
  15. using Microsoft.IronStudio;
  16. using IronPython.Hosting;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Text;
  20. using System.Reflection;
  21. using System.Threading;
  22. using Microsoft.Scripting.Hosting;
  23. using System.Linq;
  24. using Microsoft.VisualStudio.TestTools.UnitTesting;
  25. using Microsoft.IronPythonTools.Library.Repl;
  26. using Microsoft.IronStudio.RemoteEvaluation;
  27. namespace UnitTests {
  28. [TestClass]
  29. public partial class Program {
  30. public static void Main(string[] args) {
  31. var inst = new Program();
  32. foreach (var method in typeof(Program).GetMethods()) {
  33. if (method.Name.StartsWith("Scenario_")) {
  34. if (args.Length > 0 && !args.Contains(method.Name)) {
  35. Console.Write("Skipping Scenario {0} ", method.Name);
  36. continue;
  37. }
  38. Console.Write("Scenario {0} ", method.Name);
  39. try {
  40. method.Invoke(inst, new object[0]);
  41. Console.WriteLine("PASSED");
  42. } catch (Exception e) {
  43. Console.WriteLine(e.ToString());
  44. Console.WriteLine("FAILED");
  45. }
  46. }
  47. }
  48. }
  49. [TestMethod]
  50. public void Scenario_RemoteScriptFactory() {
  51. using (var factory = RemotePythonEvaluator.CreateFactory()) {
  52. var runtime = (ScriptRuntime)factory.CreateRuntime(Python.CreateRuntimeSetup(new Dictionary<string, object>()));
  53. StringWriter writer = new StringWriter();
  54. StringWriter errWriter = new StringWriter();
  55. runtime.IO.SetOutput(Stream.Null, writer);
  56. factory.SetConsoleOut(writer);
  57. factory.SetConsoleError(errWriter);
  58. // verify print goes to the correct output
  59. var engine = runtime.GetEngine("Python");
  60. engine.Execute("print 'hello'");
  61. var builder = writer.GetStringBuilder();
  62. AreEqual(builder.ToString(), "hello\r\n");
  63. builder.Clear();
  64. // verify Console.WriteLine is redirected
  65. engine.Execute("import System\nSystem.Console.WriteLine('hello')\n");
  66. AreEqual(builder.ToString(), "hello\r\n");
  67. builder.Clear();
  68. // verify Console.Error.WriteLine is redirected to stderr
  69. var errBuilder = errWriter.GetStringBuilder();
  70. engine.Execute("import System\nSystem.Console.Error.WriteLine('hello')\n");
  71. AreEqual(errBuilder.ToString(), "hello\r\n");
  72. errBuilder.Clear();
  73. // raise an exception, should be propagated back
  74. try {
  75. engine.Execute("import System\nraise System.ArgumentException()\n");
  76. AreEqual(true, false);
  77. } catch (ArgumentException) {
  78. }
  79. /*
  80. // verify that all code runs on the same thread
  81. var scope = engine.CreateScope();
  82. engine.Execute("import System");
  83. List<object> res = new List<object>();
  84. for (int i = 0; i < 100; i++) {
  85. ThreadPool.QueueUserWorkItem(
  86. (x) => {
  87. object value = engine.Execute("System.Threading.Thread.CurrentThread.ManagedThreadId", scope);
  88. lock (res) {
  89. res.Add(value);
  90. }
  91. });
  92. }
  93. while (res.Count != 100) {
  94. Thread.Sleep(100);
  95. }
  96. for (int i = 1; i < res.Count; i++) {
  97. if (!res[i - 1].Equals(res[i])) {
  98. throw new Exception("running on multiple threads");
  99. }
  100. }*/
  101. // create a long running work item, execute it, and then make sure we can continue to execute work items.
  102. ThreadPool.QueueUserWorkItem(x => {
  103. engine.Execute("while True: pass");
  104. });
  105. Thread.Sleep(1000);
  106. factory.Abort();
  107. AreEqual(engine.Execute("42"), 42);
  108. }
  109. // check starting on an MTA thread
  110. using (var factory = new RemoteScriptFactory(ApartmentState.MTA)) {
  111. var runtime = (ScriptRuntime)factory.CreateRuntime(Python.CreateRuntimeSetup(new Dictionary<string, object>()));
  112. var engine = runtime.GetEngine("Python");
  113. AreEqual(engine.Execute("import System\nSystem.Threading.Thread.CurrentThread.ApartmentState == System.Threading.ApartmentState.MTA"), true);
  114. }
  115. // check starting on an STA thread
  116. using (var factory = new RemoteScriptFactory(ApartmentState.STA)) {
  117. var runtime = (ScriptRuntime)factory.CreateRuntime(Python.CreateRuntimeSetup(new Dictionary<string, object>()));
  118. var engine = runtime.GetEngine("Python");
  119. AreEqual(engine.Execute("import System\nSystem.Threading.Thread.CurrentThread.ApartmentState == System.Threading.ApartmentState.STA"), true);
  120. }
  121. }
  122. private static void AreEqual(object x, object y) {
  123. if (!x.Equals(y)) {
  124. throw new Exception(String.Format("Expected {0}, got {1}", y, x));
  125. }
  126. }
  127. }
  128. }