public void Format_NullError_ReturnsEmptyString()
        {
            // Arrange & Act.
            PSResourceManagerError error = null;

            // Act.
            string result = PSResourceManagerErrorFormatter.Format(error);

            // Assert.
            result.Should().Be(string.Empty);
        }
        public static PSResourceManagerError ToPSResourceManagerError(this ResourceManagementErrorWithDetails error)
        {
            PSResourceManagerError rmError = new PSResourceManagerError
            {
                Code    = error.Code,
                Message = error.Message,
                Target  = string.IsNullOrEmpty(error.Target) ? null : error.Target
            };

            if (error.Details != null)
            {
                List <PSResourceManagerError> innerRMError = new List <PSResourceManagerError>();
                error.Details.ForEach(detail => innerRMError.Add(detail.ToPSResourceManagerError()));
                rmError.Details = innerRMError;
            }

            return(rmError);
        }
        public static string Format(PSResourceManagerError error)
        {
            if (error == null)
            {
                return(string.Empty);
            }

            IList <string> messages = new List <string>
            {
                $"{error.Code} - {error.Message}"
            };

            if (error.Details != null)
            {
                foreach (var innerError in error.Details)
                {
                    messages.Add(Format(innerError));
                }
            }

            return(string.Join(Environment.NewLine, messages));
        }
        public void Format_NestedErrors_ReturnsFlattenedErrorString()
        {
            // Arrange & Act.
            PSResourceManagerError error = new PSResourceManagerError
            {
                Code    = "TopLevelError",
                Message = "Top level error message.",
                Details = new List <PSResourceManagerError>
                {
                    new PSResourceManagerError
                    {
                        Code    = "SecondLevelError",
                        Message = "Second level error message.",
                        Details = new List <PSResourceManagerError>
                        {
                            new PSResourceManagerError
                            {
                                Code    = "ThirdLevelError",
                                Message = "Third level error message."
                            }
                        }
                    },
                    new PSResourceManagerError
                    {
                        Code    = "AnotherSecondLevelError",
                        Message = "Another second level error message."
                    }
                }
            };

            // Act.
            string result = PSResourceManagerErrorFormatter.Format(error);

            // Assert.
            result.Should().Be(@"TopLevelError - Top level error message.
SecondLevelError - Second level error message.
ThirdLevelError - Third level error message.
AnotherSecondLevelError - Another second level error message.".Replace("\r\n", Environment.NewLine));
        }