Skip to content
This repository
Browse code

Merge pull request #26 from HEskandari/master

Adding colored Pass/Fail/Ignore messages to the console
  • Loading branch information...
commit 0c2067120e09e5dcf0f674608a80ef1ef7392c47 2 parents 48b3577 + c6819bb
Jeff Schumacher authored January 30, 2013
1  src/Giles.Core/Giles.Core.csproj
@@ -82,6 +82,7 @@
82 82
     <Compile Include="Runners\TestResult.cs" />
83 83
     <Compile Include="Runners\TestFrameworkResolver.cs" />
84 84
     <Compile Include="UI\DisplayScrubber.cs" />
  85
+    <Compile Include="Utility\CollectionExtensions.cs" />
85 86
     <Compile Include="Utility\TypeLoader.cs" />
86 87
     <Compile Include="UI\ConsoleUserDisplay.cs" />
87 88
     <Compile Include="UI\GrowlUserDisplay.cs" />
2  src/Giles.Core/Runners/BuildRunner.cs
@@ -42,7 +42,7 @@ private static string FormatBuildMessages(Stopwatch watch, ExecutionResult resul
42 42
                                         result.ExitCode == 0 ? "Success" : "Failure");
43 43
 
44 44
             if (result.ExitCode != 0)
45  
-                message += string.Format("\n{0}", result.Output);
  45
+                message += string.Format("\n{0}", result.ErrorMessage);
46 46
             return message;
47 47
         }
48 48
     }
37  src/Giles.Core/Runners/CommandProcessExecutor.cs
@@ -47,16 +47,45 @@ private static ExecutionResult RunExecutable(string fileName, string arguments)
47 47
             return new ExecutionResult
48 48
                        {
49 49
                            ExitCode = exitCode, 
50  
-                           Output = output, 
51  
-                           Runner = new FileInfo(fileName).Name.ToUpper().Replace(".EXE", string.Empty)
  50
+                           Runner = new TestRunnerResult
  51
+                           {
  52
+                               RunnerName = new FileInfo(fileName).Name.ToUpper().Replace(".EXE", string.Empty)
  53
+                           }
52 54
                        };
53 55
         }
54 56
     }
55 57
 
56 58
     public class ExecutionResult
57 59
     {
58  
-        public string Runner { get; set; }
59  
-        public string Output { get; set; }
  60
+        public ExecutionResult()
  61
+        {
  62
+            Runner = new TestRunnerResult();
  63
+        }
  64
+        public TestRunnerResult Runner { get; set; }
  65
+        public string ErrorMessage { get; set; }
60 66
         public int ExitCode { get; set; }
61 67
     }
  68
+
  69
+    public class TestRunnerResult
  70
+    {
  71
+        public TestRunnerResult()
  72
+        {
  73
+            Stats = new TestStatistics();
  74
+        }
  75
+
  76
+        public string RunnerName { get; set; }
  77
+        public TestStatistics Stats { get; set; }
  78
+
  79
+        public override string ToString()
  80
+        {
  81
+            return string.Format("{0} Passed: {1}, Failed: {2}, Ignored: {3}", RunnerName, Stats.Passed, Stats.Failed, Stats.Ignored);
  82
+        }
  83
+    }
  84
+
  85
+    public class TestStatistics
  86
+    {
  87
+        public int Passed { get; set; }
  88
+        public int Failed { get; set; }
  89
+        public int Ignored { get; set; }
  90
+    }
62 91
 }
63  src/Giles.Core/Runners/GilesTestListener.cs
@@ -67,47 +67,42 @@ public void DisplayResults()
67 67
         {
68 68
             if (testRunnerResults.Count == 0)
69 69
             {
70  
-                config.UserDisplay.ToList().ForEach(display => 
71  
-                    display.DisplayResult(new ExecutionResult
72  
-                                              {
73  
-                                                  ExitCode = 1, 
74  
-                                                  Output = "No tests were run. Check your filter names and test assembly options",
75  
-                                                  Runner = ""
76  
-                                              }));
  70
+                config.UserDisplay.ToList().ForEach(display => display.DisplayResult(new ExecutionResult
  71
+                {
  72
+                    ExitCode = 1,
  73
+                    ErrorMessage = "No tests were run. Check your filter names and test assembly options",
  74
+                }));
77 75
                 return;
78 76
             }
79 77
 
80  
-            var messages = AggregateTestRunnerResults();
81  
-
82  
-            var result = new ExecutionResult
83  
-                {
84  
-                    ExitCode = totalResults[TestState.Failed] > 0 ? 1 : 0,
85  
-                    Output = messages.ToString(),
86  
-                    Runner = string.Empty
87  
-                };
88  
-
89  
-            if (config.UserDisplay.Count() == 0)
  78
+            var results = CreateResults();
  79
+            
  80
+            if (config.UserDisplay.IsNullOrEmpty())
90 81
                 config.UserDisplay = new List<IUserDisplay> { new ConsoleUserDisplay() };
91 82
 
92  
-            config.UserDisplay.ToList().ForEach(display => display.DisplayResult(result));
  83
+            config.UserDisplay.ToList().ForEach(display => results.ForEach(display.DisplayResult));
93 84
         }
94 85
 
95  
-        StringBuilder AggregateTestRunnerResults()
  86
+        private List<ExecutionResult> CreateResults()
96 87
         {
97  
-            var messages = new StringBuilder();
98  
-            testRunnerResults.ToList().ForEach(x => messages.Append(
99  
-                string.Format(
100  
-                    "{0} Results: Passed: {1}, Failed: {2}, Ignored: {3}\n",
101  
-                    x.Key,
102  
-                    x.Value[TestState.Passed],
103  
-                    x.Value[TestState.Failed],
104  
-                    x.Value[TestState.Ignored])));
105  
-
106  
-            messages.Append(string.Format("Total Passed: {0}, Failed: {1}, Ignored: {2}",
107  
-                                          totalResults[TestState.Passed],
108  
-                                          totalResults[TestState.Failed],
109  
-                                          totalResults[TestState.Ignored]));
110  
-            return messages;
  88
+            var result = new List<ExecutionResult>();
  89
+
  90
+            testRunnerResults.ToList().ForEach(x => result.Add(new ExecutionResult
  91
+            {
  92
+                Runner = new TestRunnerResult
  93
+                {
  94
+                    RunnerName = x.Key,
  95
+                    Stats = new TestStatistics
  96
+                    {
  97
+                        Failed = x.Value[TestState.Failed],
  98
+                        Passed = x.Value[TestState.Passed],
  99
+                        Ignored = x.Value[TestState.Ignored]
  100
+                    }
  101
+                },
  102
+                ExitCode = x.Value[TestState.Failed] > 0 ? 1 : 0,
  103
+            }));
  104
+
  105
+            return result;
111 106
         }
112 107
 
113 108
         public void DisplayVerboseResults()
@@ -118,7 +113,7 @@ public void DisplayVerboseResults()
118 113
 
119 114
         public void DisplayErrors()
120 115
         {
121  
-            var messages = new StringBuilder(string.Format("\n\nTest Run Errors ({0})\n", testRunnerFailures.ToList().Count));
  116
+            var messages = new StringBuilder(string.Format("\n\nTest Run Errors ({0})\n", testRunnerFailures.Count()));
122 117
             testRunnerFailures.Each(x =>
123 118
                                         {
124 119
                                             messages.AppendLine("-------------------");
64  src/Giles.Core/UI/ConsoleUserDisplay.cs
@@ -11,6 +11,7 @@ public ConsoleUserDisplay()
11 11
         {
12 12
             defaultConsoleColor = Console.ForegroundColor;
13 13
         }
  14
+
14 15
         public void DisplayMessage(string message, params object[] parameters)
15 16
         {
16 17
             Console.WriteLine(message.ScrubDisplayStringForFormatting(), parameters);
@@ -18,13 +19,66 @@ public void DisplayMessage(string message, params object[] parameters)
18 19
 
19 20
         public void DisplayResult(ExecutionResult result)
20 21
         {
21  
-            Console.WriteLine("\n\n======= {0} TEST RUNNER RESULTS =======", result.Runner);
22  
-            Console.ForegroundColor = result.ExitCode != 0 ?
23  
-                                      ConsoleColor.Red : defaultConsoleColor;
  22
+            try
  23
+            {
  24
+                Console.WriteLine("\n\n======= {0} TEST RUNNER RESULTS =======", result.Runner.RunnerName);
  25
+
  26
+                using(new ChangedColorContext(GetPassedColor(result)))
  27
+                {
  28
+                    Console.Write(string.Format("Passed: {0}", result.Runner.Stats.Passed));
  29
+                }
  30
+
  31
+                Console.Write(", ");
  32
+
  33
+                using (new ChangedColorContext(GetFailedColor(result)))
  34
+                {
  35
+                    Console.Write(string.Format("Failed: {0}", result.Runner.Stats.Failed));
  36
+                }
  37
+
  38
+                Console.Write(", ");
  39
+
  40
+                using (new ChangedColorContext(GetIgnoredColor(result)))
  41
+                {
  42
+                    Console.Write(string.Format("Ignored: {0}", result.Runner.Stats.Ignored));
  43
+                }
  44
+
  45
+                Console.Write(Environment.NewLine);
  46
+            }
  47
+            finally
  48
+            {
  49
+                Console.ForegroundColor = defaultConsoleColor;
  50
+            }
  51
+        }
  52
+
  53
+        public ConsoleColor GetPassedColor(ExecutionResult result)
  54
+        {
  55
+            return result.Runner.Stats.Passed > 0 ? ConsoleColor.Green : defaultConsoleColor;
  56
+        }
  57
+
  58
+        public ConsoleColor GetFailedColor(ExecutionResult result)
  59
+        {
  60
+            return result.Runner.Stats.Failed > 0 ? ConsoleColor.Red : defaultConsoleColor;
  61
+        }
  62
+
  63
+        public ConsoleColor GetIgnoredColor(ExecutionResult result)
  64
+        {
  65
+            return result.Runner.Stats.Ignored > 0 ? ConsoleColor.Yellow : defaultConsoleColor;
  66
+        }
  67
+
  68
+        private class ChangedColorContext : IDisposable
  69
+        {
  70
+            private readonly ConsoleColor defaultConsoleColor;
24 71
 
25  
-            Console.WriteLine(result.Output);
  72
+            public ChangedColorContext(ConsoleColor color)
  73
+            {
  74
+                defaultConsoleColor = Console.ForegroundColor;
  75
+                Console.ForegroundColor = color;
  76
+            }
26 77
 
27  
-            Console.ForegroundColor = defaultConsoleColor;
  78
+            public void Dispose()
  79
+            {
  80
+                Console.ForegroundColor = defaultConsoleColor;
  81
+            }
28 82
         }
29 83
     }
30 84
 }
2  src/Giles.Core/UI/GrowlUserDisplay.cs
@@ -45,7 +45,7 @@ public void DisplayResult(ExecutionResult result)
45 45
         {
46 46
             var title = result.ExitCode == 0 ? "Success!" : "Failures!";
47 47
             Resource icon = result.ExitCode == 0 ? LoadImage(successImage) : LoadImage(failureImage);
48  
-            var notification = new Notification(application.Name, notificationType.Name, DateTime.Now.Ticks.ToString(), title, result.Output) { Icon = icon };
  48
+            var notification = new Notification(application.Name, notificationType.Name, DateTime.Now.Ticks.ToString(), title, result.Runner.ToString()) { Icon = icon };
49 49
             growl.Notify(notification);
50 50
         }
51 51
 
12  src/Giles.Core/Utility/CollectionExtensions.cs
... ...
@@ -0,0 +1,12 @@
  1
+using System.Collections.Generic;
  2
+
  3
+namespace Giles.Core.Utility
  4
+{
  5
+    public static class CollectionExtensions
  6
+    {
  7
+        public static bool IsNullOrEmpty<T>(this IList<T> list)
  8
+        {
  9
+            return list == null || list.Count == 0;
  10
+        }
  11
+    }
  12
+}
6  src/Giles.Specs/Core/Runners/GilesTestListenerSpecs.cs
@@ -70,13 +70,13 @@ public class when_displaying_the_results : with_a_giles_test_listener
70 70
             fakeUserDisplay.DisplayResultsReceived.ShouldNotBeEmpty();
71 71
 
72 72
         It should_display_the_number_of_successful_tests = () =>
73  
-            fakeUserDisplay.DisplayResultsReceived.Count(x => x.Output.Contains("Passed: 0")).ShouldEqual(1);
  73
+            fakeUserDisplay.DisplayResultsReceived.Count(x => x.Runner.ToString().Contains("Passed: 0")).ShouldEqual(1);
74 74
 
75 75
         It should_display_the_number_of_failed_tests = () =>
76  
-            fakeUserDisplay.DisplayResultsReceived.Count(x => x.Output.Contains("Failed: 1")).ShouldEqual(1);
  76
+            fakeUserDisplay.DisplayResultsReceived.Count(x => x.Runner.ToString().Contains("Failed: 1")).ShouldEqual(1);
77 77
 
78 78
         It should_display_the_number_of_ignored_tests = () =>
79  
-            fakeUserDisplay.DisplayResultsReceived.Count(x => x.Output.Contains("Ignored: 0")).ShouldEqual(1);
  79
+            fakeUserDisplay.DisplayResultsReceived.Count(x => x.Runner.ToString().Contains("Ignored: 0")).ShouldEqual(1);
80 80
 
81 81
     }
82 82
 

0 notes on commit 0c20671

Please sign in to comment.
Something went wrong with that request. Please try again.