[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunitlite / TeamCityEventListener.cs
1 // ***********************************************************************
2 // Copyright (c) 2014 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 #if !SILVERLIGHT && !NETCF
30 using System;
31 using System.Globalization;
32 using System.IO;
33 using NUnit.Framework.Interfaces;
34
35 namespace NUnitLite
36 {
37     /// <summary>
38     /// TeamCityEventListener class handles ITestListener events
39     /// by issuing TeamCity service messages on the Console.
40     /// </summary>
41     public class TeamCityEventListener : ITestListener
42     {
43         readonly TextWriter _outWriter;
44
45         /// <summary>
46         /// Default constructor using Console.Out
47         /// </summary>
48         /// <remarks>
49         /// This constructor must be called before Console.Out is
50         /// redirected in order to work correctly under TeamCity.
51         /// </remarks>
52         public TeamCityEventListener() : this(Console.Out) { }
53
54         /// <summary>
55         /// Construct a TeamCityEventListener specifying a TextWriter. Used for testing.
56         /// </summary>
57         /// <param name="outWriter">The TextWriter to receive normal messages.</param>
58         public TeamCityEventListener(TextWriter outWriter)
59         {
60             _outWriter = outWriter;
61         }
62
63         /// <summary>
64         /// Called when a test has just started
65         /// </summary>
66         /// <param name="test">The test that is starting</param>
67         public void TestStarted(ITest test)
68         {
69             if (test.IsSuite)
70                 TC_TestSuiteStarted(test.Name);
71             else
72                 TC_TestStarted(test.Name);
73         }
74
75         /// <summary>
76         /// Called when a test has finished
77         /// </summary>
78         /// <param name="result">The result of the test</param>
79         public void TestFinished(ITestResult result)
80         {
81             string testName = result.Test.Name;
82
83             if (result.Test.IsSuite)
84                 TC_TestSuiteFinished(testName);
85             else
86                 switch (result.ResultState.Status)
87                 {
88                     case TestStatus.Passed:
89                         TC_TestFinished(testName, result.Duration);
90                         break;
91                     case TestStatus.Inconclusive:
92                         TC_TestIgnored(testName, "Inconclusive");
93                         break;
94                     case TestStatus.Skipped:
95                         TC_TestIgnored(testName, result.Message);
96                         break;
97                     case TestStatus.Failed:
98                         TC_TestFailed(testName, result.Message, result.StackTrace);
99                         TC_TestFinished(testName, result.Duration);
100                         break;
101                 }
102         }
103
104         /// <summary>
105         /// Called when a test produces output for immediate display
106         /// </summary>
107         /// <param name="output">A TestOutput object containing the text to display</param>
108         public void TestOutput(TestOutput output) { }
109
110         #region Helper Methods
111
112         private void TC_TestSuiteStarted(string name)
113         {
114             _outWriter.WriteLine("##teamcity[testSuiteStarted name='{0}']", Escape(name));
115         }
116
117         private void TC_TestSuiteFinished(string name)
118         {
119             _outWriter.WriteLine("##teamcity[testSuiteFinished name='{0}']", Escape(name));
120         }
121
122         private void TC_TestStarted(string name)
123         {
124             _outWriter.WriteLine("##teamcity[testStarted name='{0}' captureStandardOutput='true']", Escape(name));
125         }
126
127         private void TC_TestFinished(string name, double duration)
128         {
129             // TeamCity expects the duration to be in milliseconds
130             int milliseconds = (int)(duration * 1000d);
131             _outWriter.WriteLine("##teamcity[testFinished name='{0}' duration='{1}']", Escape(name), milliseconds);
132         }
133
134         private void TC_TestIgnored(string name, string reason)
135         {
136             _outWriter.WriteLine("##teamcity[testIgnored name='{0}' message='{1}']", Escape(name), Escape(reason));
137         }
138
139         private void TC_TestFailed(string name, string message, string details)
140         {
141             _outWriter.WriteLine("##teamcity[testFailed name='{0}' message='{1}' details='{2}']", Escape(name), Escape(message), Escape(details));
142         }
143
144         private static string Escape(string input)
145         {
146             return input != null
147                 ? input.Replace("|", "||")
148                        .Replace("'", "|'")
149                        .Replace("\n", "|n")
150                        .Replace("\r", "|r")
151                        .Replace(char.ConvertFromUtf32(int.Parse("0086", NumberStyles.HexNumber)), "|x")
152                        .Replace(char.ConvertFromUtf32(int.Parse("2028", NumberStyles.HexNumber)), "|l")
153                        .Replace(char.ConvertFromUtf32(int.Parse("2029", NumberStyles.HexNumber)), "|p")
154                        .Replace("[", "|[")
155                        .Replace("]", "|]")
156                 : null;
157         }
158
159         #endregion
160     }
161 }
162 #endif