Pārlūkot izejas kodu

Add ToString to CommandResult

Olivier Coanet 2 gadi atpakaļ
vecāks
revīzija
549cf49c31

+ 29 - 0
src/Abc.Zebus.Tests/CommandResultTests.cs

@@ -47,5 +47,34 @@ namespace Abc.Zebus.Tests
 
             cmdResult.GetErrorMessageFromEnum<FakeEnumErrorCode>().ShouldBeEmpty();
         }
+
+        [TestCase(0, null, "Success")]
+        [TestCase(1, null, "Error, ErrorCode: 1")]
+        [TestCase(256, null, "Error, ErrorCode: 256")]
+        [TestCase(256, "Expected message", "Error, ErrorCode: 256, ResponseMessage: [Expected message]")]
+        public void should_get_string_from_result(int errorCode, string responseMessage, string expectedText)
+        {
+            var commandResult = new CommandResult(errorCode, responseMessage, null);
+
+            commandResult.ToString().ShouldEqual(expectedText);
+        }
+
+        [TestCase(0, null, "Success, Response: [Response!]")]
+        [TestCase(256, null, "Error, ErrorCode: 256, Response: [Response!]")]
+        [TestCase(256, "Expected message", "Error, ErrorCode: 256, ResponseMessage: [Expected message], Response: [Response!]")]
+        public void should_get_string_from_result_with_response(int errorCode, string responseMessage, string expectedText)
+        {
+            var commandResult = new CommandResult(errorCode, responseMessage, new Response());
+
+            commandResult.ToString().ShouldEqual(expectedText);
+        }
+
+        private class Response
+        {
+            public override string ToString()
+            {
+                return $"Response!";
+            }
+        }
     }
 }

+ 31 - 0
src/Abc.Zebus/CommandResult.cs

@@ -1,10 +1,17 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Text;
 using Abc.Zebus.Util.Extensions;
 
 namespace Abc.Zebus
 {
+    /// <summary>
+    /// Contains the result of a bus command.
+    /// </summary>
+    /// <remarks>
+    /// <see cref="CommandResult"/> should probably not be instantiated by user code outside unit tests.
+    /// </remarks>
     public class CommandResult
     {
         [Obsolete("Use the constructor with the responseMessage parameter")]
@@ -36,11 +43,35 @@ namespace Abc.Zebus
             return string.Format(((Enum)(object)value).GetAttributeDescription(), formatValues);
         }
 
+        public static CommandResult Success(object? response = null)
+            => new CommandResult(0, null, response);
+
+        public static CommandResult Error(int errorCode = 1, string? responseMessage = null)
+        {
+            if (errorCode == 0)
+                throw new ArgumentException("error code cannot be zero", nameof(errorCode));
+
+            return new CommandResult(errorCode, responseMessage, null);
+        }
+
         internal static ErrorStatus GetErrorStatus(IEnumerable<Exception> exceptions)
         {
             return exceptions.FirstOrDefault() is MessageProcessingException ex
                 ? new ErrorStatus(ex.ErrorCode, ex.Message)
                 : ErrorStatus.UnknownError;
         }
+
+        public override string ToString()
+        {
+            var text = new StringBuilder(IsSuccess ? "Success" : $"Error, ErrorCode: {ErrorCode}");
+
+            if (ResponseMessage != null)
+                text.Append($", ResponseMessage: [{ResponseMessage}]");
+
+            if (Response != null)
+                text.Append($", Response: [{Response}]");
+
+            return text.ToString();
+        }
     }
 }