Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TextUI.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_IOS || SILICONSTUDIO_PLATFORM_ANDROID
4 // ***********************************************************************
5 // Copyright (c) 2007 Charlie Poole
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 // ***********************************************************************
26 
27 using System;
28 using System.Collections.Generic;
29 using System.IO;
30 using System.Reflection;
31 using NUnit.Framework.Api;
32 using NUnit.Framework.Api;
33 using NUnit.Framework.Internal;
34 using NUnit.Framework.Internal.Filters;
35 using NUnitLite.Runner;
36 
37 namespace SiliconStudio.Paradox.Graphics.Regression
38 {
39  /// <summary>
40  /// TextUI is a general purpose class that runs tests and
41  /// outputs to a TextWriter.
42  ///
43  /// Call it from your Main like this:
44  /// new TextUI(textWriter).Execute(args);
45  /// OR
46  /// new TextUI().Execute(args);
47  /// The provided TextWriter is used by default, unless the
48  /// arguments to Execute override it using -out. The second
49  /// form uses the Console, provided it exists on the platform.
50  ///
51  /// NOTE: When running on a platform without a Console, such
52  /// as Windows Phone, the results will simply not appear if
53  /// you fail to specify a file in the call itself or as an option.
54  /// </summary>
55  public class TextUI : ITestListener
56  {
57  private CommandLineOptions commandLineOptions;
58 
59  private List<Assembly> assemblies = new List<Assembly>();
60 
61  private TextWriter writer;
62 
63  private ITestAssemblyRunner runner;
64 
65  #region Constructors
66 
67  /// <summary>
68  /// Initializes a new instance of the <see cref="TextUI"/> class.
69  /// </summary>
70  public TextUI() : this(ConsoleWriter.Out) { }
71 
72  /// <summary>
73  /// Initializes a new instance of the <see cref="TextUI"/> class.
74  /// </summary>
75  /// <param name="writer">The TextWriter to use.</param>
76  public TextUI(TextWriter writer)
77  {
78  // Set the default writer - may be overridden by the args specified
79  this.writer = writer;
80  this.runner = new NUnitLiteTestAssemblyRunner(new NamespaceAssemblyBuilder(new NUnitLiteTestAssemblyBuilder()));
81  }
82  #endregion
83 
84  #region Public Methods
85  /// <summary>
86  /// Execute a test run based on the aruments passed
87  /// from Main.
88  /// </summary>
89  /// <param name="args">An array of arguments</param>
90  public void Execute(string[] args)
91  {
92  // NOTE: Execute must be directly called from the
93  // test assembly in order for the mechanism to work.
94  Assembly callingAssembly = Assembly.GetCallingAssembly();
95 
96  this.commandLineOptions = new CommandLineOptions();
97  commandLineOptions.Parse(args);
98 
99  if (commandLineOptions.OutFile != null)
100  this.writer = new StreamWriter(commandLineOptions.OutFile);
101 
102  if (!commandLineOptions.NoHeader)
103  WriteHeader(this.writer);
104 
105  if (commandLineOptions.ShowHelp)
106  writer.Write(commandLineOptions.HelpText);
107  else if (commandLineOptions.Error)
108  {
109  writer.WriteLine(commandLineOptions.ErrorMessage);
110  writer.WriteLine(commandLineOptions.HelpText);
111  }
112  else
113  {
114  WriteRuntimeEnvironment(this.writer);
115 
116  if (commandLineOptions.Wait && commandLineOptions.OutFile != null)
117  writer.WriteLine("Ignoring /wait option - only valid for Console");
118 
119  // We only have one commandline option that has to be passed
120  // to the runner, so we do it here for convenience.
121  var runnerSettings = new Dictionary<string, object>();
122  Randomizer.InitialSeed = commandLineOptions.InitialSeed;
123 
124  TestFilter filter = commandLineOptions.TestCount > 0
125  ? new SimpleNameFilter(commandLineOptions.Tests)
126  : TestFilter.Empty;
127 
128  try
129  {
130  foreach (string name in commandLineOptions.Parameters)
131  assemblies.Add(Assembly.Load(name));
132 
133  if (assemblies.Count == 0)
134  assemblies.Add(callingAssembly);
135 
136  // TODO: For now, ignore all but first assembly
137  Assembly assembly = assemblies[0];
138 
139  //Randomizer.InitialSeed = commandLineOptions.InitialSeed;
140 
141  if (runner.Load(assembly, runnerSettings) == null)
142  {
143  var assemblyName = AssemblyHelper.GetAssemblyName(assembly);
144  Console.WriteLine("No tests found in assembly {0}", assemblyName.Name);
145  return;
146  }
147 
148  if (commandLineOptions.Explore)
149  ExploreTests();
150  else
151  {
152  if (commandLineOptions.Include != null && commandLineOptions.Include != string.Empty)
153  {
154  TestFilter includeFilter = new SimpleCategoryExpression(commandLineOptions.Include).Filter;
155 
156  if (filter.IsEmpty)
157  filter = includeFilter;
158  else
159  filter = new AndFilter(filter, includeFilter);
160  }
161 
162  if (commandLineOptions.Exclude != null && commandLineOptions.Exclude != string.Empty)
163  {
164  TestFilter excludeFilter = new NotFilter(new SimpleCategoryExpression(commandLineOptions.Exclude).Filter);
165 
166  if (filter.IsEmpty)
167  filter = excludeFilter;
168  else if (filter is AndFilter)
169  ((AndFilter)filter).Add(excludeFilter);
170  else
171  filter = new AndFilter(filter, excludeFilter);
172  }
173 
174  RunTests(filter);
175  }
176  }
177  catch (FileNotFoundException ex)
178  {
179  writer.WriteLine(ex.Message);
180  }
181  catch (Exception ex)
182  {
183  writer.WriteLine(ex.ToString());
184  }
185  finally
186  {
187  if (commandLineOptions.OutFile == null)
188  {
189  if (commandLineOptions.Wait)
190  {
191  Console.WriteLine("Press Enter key to continue . . .");
192  Console.ReadLine();
193  }
194  }
195  else
196  {
197  writer.Close();
198  }
199  }
200  }
201  }
202 
203  #endregion
204 
205  #region Helper Methods
206 
207  private void RunTests(ITestFilter filter)
208  {
209  DateTime now = DateTime.Now;
210  ITestResult result = runner.Run(this, filter);
211  new ResultReporter(result, writer).ReportResults();
212 
213  string resultFile = commandLineOptions.ResultFile;
214  string resultFormat = commandLineOptions.ResultFormat;
215  if (resultFile != null || commandLineOptions.ResultFormat != null)
216  {
217  if (resultFile == null)
218  resultFile = "TestResult.xml";
219 
220  if (resultFormat == "nunit2")
221  new NUnit2XmlOutputWriter(now).WriteResultFile(result, resultFile);
222  else
223  new NUnit3XmlOutputWriter(now).WriteResultFile(result, resultFile);
224  Console.WriteLine();
225  Console.WriteLine("Results saved as {0}.", resultFile);
226  }
227  }
228 
229  private void ExploreTests()
230  {
231  XmlNode testNode = runner.LoadedTest.ToXml(true);
232 
233  string listFile = commandLineOptions.ExploreFile;
234  TextWriter textWriter = listFile != null && listFile.Length > 0
235  ? new StreamWriter(listFile)
236  : Console.Out;
237 
238  System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
239  settings.Indent = true;
240  settings.Encoding = System.Text.Encoding.UTF8;
241  System.Xml.XmlWriter testWriter = System.Xml.XmlWriter.Create(textWriter, settings);
242 
243  testNode.WriteTo(testWriter);
244  testWriter.Close();
245 
246  Console.WriteLine();
247  Console.WriteLine("Test info saved as {0}.", listFile);
248  }
249 
250  /// <summary>
251  /// Writes the header.
252  /// </summary>
253  /// <param name="writer">The writer.</param>
254  public static void WriteHeader(TextWriter writer)
255  {
256  Assembly executingAssembly = Assembly.GetExecutingAssembly();
257  AssemblyName assemblyName = AssemblyHelper.GetAssemblyName(executingAssembly);
258 #if NUNITLITE
259  string title = "NUnitLite";
260 #else
261  string title = "NUNit Framework";
262 #endif
263  System.Version version = assemblyName.Version;
264  string copyright = "Copyright (C) 2012, Charlie Poole";
265  string build = "";
266 
267  object[] attrs = executingAssembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
268  if (attrs.Length > 0)
269  {
270  AssemblyTitleAttribute titleAttr = (AssemblyTitleAttribute)attrs[0];
271  title = titleAttr.Title;
272  }
273 
274  attrs = executingAssembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
275  if (attrs.Length > 0)
276  {
277  AssemblyCopyrightAttribute copyrightAttr = (AssemblyCopyrightAttribute)attrs[0];
278  copyright = copyrightAttr.Copyright;
279  }
280 
281  attrs = executingAssembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false);
282  if (attrs.Length > 0)
283  {
284  AssemblyConfigurationAttribute configAttr = (AssemblyConfigurationAttribute)attrs[0];
285  build = string.Format("({0})", configAttr.Configuration);
286  }
287 
288  writer.WriteLine(String.Format("{0} {1} {2}", title, version.ToString(3), build));
289  writer.WriteLine(copyright);
290  writer.WriteLine();
291  }
292 
293  /// <summary>
294  /// Writes the runtime environment.
295  /// </summary>
296  /// <param name="writer">The writer.</param>
297  public static void WriteRuntimeEnvironment(TextWriter writer)
298  {
299  string clrPlatform = Type.GetType("Mono.Runtime", false) == null ? ".NET" : "Mono";
300  writer.WriteLine("Runtime Environment -");
301  writer.WriteLine(" OS Version: {0}", Environment.OSVersion);
302  writer.WriteLine(" {0} Version: {1}", clrPlatform, Environment.Version);
303  writer.WriteLine();
304  }
305 
306 
307  #endregion
308 
309  #region ITestListener Members
310 
311  /// <summary>
312  /// Called when a test has just started
313  /// </summary>
314  /// <param name="test">The test that is starting</param>
315  public void TestStarted(ITest test)
316  {
317  if (commandLineOptions.LabelTestsInOutput)
318  writer.WriteLine("***** {0}", test.Name);
319  }
320 
321  /// <summary>
322  /// Called when a test has finished
323  /// </summary>
324  /// <param name="result">The result of the test</param>
325  public void TestFinished(ITestResult result)
326  {
327  }
328 
329  /// <summary>
330  /// Called when the test creates text output.
331  /// </summary>
332  /// <param name="testOutput">A console message</param>
333  public void TestOutput(TestOutput testOutput)
334  {
335  }
336 
337  #endregion
338  }
339 }
340 #endif
Search for out only dependencies.
System.Console Console
SiliconStudio.Paradox.Graphics.Regression.TextUI TextUI