diff --git a/Src/SwqlStudio/ObjectExplorer/StringBuilderExtensions.cs b/Src/SwqlStudio/ObjectExplorer/StringBuilderExtensions.cs index 547c587bf..2dfca612d 100644 --- a/Src/SwqlStudio/ObjectExplorer/StringBuilderExtensions.cs +++ b/Src/SwqlStudio/ObjectExplorer/StringBuilderExtensions.cs @@ -8,49 +8,89 @@ internal static class StringBuilderExtensions { public static void AppendName(this StringBuilder builder, string name) { - builder.AppendFormat("Name: {0}\r\n", name); + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + builder.AppendFormat("Name: {0}{1}", name, Environment.NewLine); } public static void AppendType(this StringBuilder builder, string metadataType) { - builder.AppendFormat("Type: {0}\r\n", metadataType); + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + builder.AppendFormat("Type: {0}{1}", metadataType, Environment.NewLine); } public static void AppendSummaryParagraph(this StringBuilder builder, string summary) { - builder.Append("\r\n\r\n"); + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + builder.AppendLine(); + builder.AppendLine(); builder.AppendSummary(summary); } public static void AppendSummary(this StringBuilder builder, string summary) { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + if (string.IsNullOrEmpty(summary)) + { return; + } - var trimmed = summary.Trim(); - builder.AppendFormat(trimmed); + string trimmed = summary.Trim(); + builder.Append(trimmed); } public static void AppendAccessControl(this StringBuilder builder, ConnectionInfo connection, Entity entity) { + if (entity == null) + { + throw new ArgumentNullException(nameof(entity)); + } + + if (connection == null) + { + throw new ArgumentNullException(nameof(connection)); + } + + if (entity.IsIndication) { + builder.Append($@"Can Subscribe: {connection.CanCreateSubscription}"); } else { - builder.Append($"Can Read: {entity.CanRead}\r\n"); - builder.Append($"Can Create: {entity.CanCreate}\r\n"); - builder.Append($"Can Update: {entity.CanUpdate}\r\n"); - builder.Append($"Can Delete: {entity.CanDelete}\r\n"); + builder.AppendLine($"Can Read: {entity.CanRead}"); + builder.AppendLine($"Can Create: {entity.CanCreate}"); + builder.AppendLine($"Can Update: {entity.CanUpdate}"); + builder.AppendLine($"Can Delete: {entity.CanDelete}"); } } public static void AppendObsoleteSection(this StringBuilder builder, IObsoleteMetadata entity) { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + if (entity != null && entity.IsObsolete) { - builder.Append($"Obsolete: {entity.ObsolescenceReason}{Environment.NewLine}"); + builder.AppendLine($"Obsolete: {entity.ObsolescenceReason}"); } } } diff --git a/Test/SwqlStudio.Tests/AutocompleteProviderTest.cs b/Test/SwqlStudio.Tests/AutocompleteProviderTest.cs index d31c7c669..24a668cf9 100644 --- a/Test/SwqlStudio.Tests/AutocompleteProviderTest.cs +++ b/Test/SwqlStudio.Tests/AutocompleteProviderTest.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; using FluentAssertions; using SwqlStudio.Autocomplete; using Xunit; diff --git a/Test/SwqlStudio.Tests/AutocompleteTokenizerTest.cs b/Test/SwqlStudio.Tests/AutocompleteTokenizerTest.cs index e822c945c..8cf5fa718 100644 --- a/Test/SwqlStudio.Tests/AutocompleteTokenizerTest.cs +++ b/Test/SwqlStudio.Tests/AutocompleteTokenizerTest.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; using FluentAssertions; using SwqlStudio.Autocomplete; using Xunit; diff --git a/Test/SwqlStudio.Tests/StringBuilderExtensionsTests.cs b/Test/SwqlStudio.Tests/StringBuilderExtensionsTests.cs new file mode 100644 index 000000000..1f19e3c7e --- /dev/null +++ b/Test/SwqlStudio.Tests/StringBuilderExtensionsTests.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Generic; +using System.Text; +using SwqlStudio.Metadata; +using Xunit; + +namespace SwqlStudio.Tests +{ + public class StringBuilderExtensionsTests + { + [Fact] + public void AllMethodsThrowExceptionOnNullBuilder() + { + StringBuilder stringBuilder = null; + var methods = new List + { + () => stringBuilder.AppendName(string.Empty), + () => stringBuilder.AppendType(string.Empty), + () => stringBuilder.AppendObsoleteSection(null), + () => stringBuilder.AppendSummary(string.Empty), + () => stringBuilder.AppendSummaryParagraph(string.Empty), + () => stringBuilder.AppendAccessControl(null, null), + }; + + foreach (Action method in methods) + { + Assert.Throws(() => + { + method(); + }); + } + } + + [Theory] + [MemberData(nameof(AppendSummaryTrimsInputTestCases))] + public void AppendSummaryTests(string input, string expected) + { + TestAppendMethod(input, expected, (s, stringB) => stringB.AppendSummary(s)); + } + + public static IEnumerable AppendSummaryTrimsInputTestCases = new List + { + new object[]{ "a", "a"}, + new object[]{ "a ", "a"}, + new object[]{ " a", "a"}, + new object[]{ null, ""}, + new object[]{ string.Empty, ""}, + new object[]{ " a", "a"}, + new object[]{ "a {0} ", "a {0}"} + }; + + [Theory] + [MemberData(nameof(AppendSummaryParagraphTestCases))] + public void AppendSummaryParagraphTests(string input, string expected) + { + TestAppendMethod(input, expected, (s, stringB) => stringB.AppendSummaryParagraph(s)); + } + + public static IEnumerable AppendSummaryParagraphTestCases = new List + { + new object[]{ "a ", $"{Environment.NewLine}{Environment.NewLine}a"}, + new object[]{ " ", $"{Environment.NewLine}{Environment.NewLine}"} + }; + + + [Theory] + [MemberData(nameof(AppendNameTestCases))] + public void AppendNameTests(string input, string expected) + { + TestAppendMethod(input, expected, (s, stringB) => stringB.AppendName(s)); + } + + public static IEnumerable AppendNameTestCases = new List + { + new object[]{ "a", $"Name: a{Environment.NewLine}"}, + new object[]{ string.Empty, $"Name: {Environment.NewLine}"}, + }; + + [Theory] + [MemberData(nameof(AppendTypeTestCases))] + public void AppendTypeTests(string input, string expected) + { + TestAppendMethod(input, expected, (s, stringB) => stringB.AppendType(s)); + } + + public static IEnumerable AppendTypeTestCases = new List + { + new object[]{ "a", $"Type: a{Environment.NewLine}"}, + new object[]{ string.Empty, $"Type: {Environment.NewLine}"}, + }; + + [Theory] + [MemberData(nameof(AppendObsoleteSectionTestCases))] + public void AppendObsoleteSectionTests(string input, string expected, bool isObsolete) + { + IObsoleteMetadata entity = new Entity { IsObsolete = isObsolete, ObsolescenceReason = input }; + TestAppendMethod(input, expected, (s, stringB) => stringB.AppendObsoleteSection(entity)); + } + + public static IEnumerable AppendObsoleteSectionTestCases = new List + { + new object[]{ "a", $"Obsolete: a{Environment.NewLine}", true}, + new object[]{ string.Empty, $"Obsolete: {Environment.NewLine}", true}, + new object[]{ string.Empty, string.Empty, false}, + }; + + [Fact] + public void AppendAccessControlTests_IsIndication() + { + var stringBuilder = new StringBuilder(); + + var connectionInfo = new ConnectionInfo("localhost", "admin", "pass", "orion"); + var entity = new Entity { IsIndication = true }; + + stringBuilder.AppendAccessControl(connectionInfo, entity); + + var result = stringBuilder.ToString(); + + Assert.Equal("Can Subscribe: False", result); + } + + [Fact] + public void AppendAccessControlTests_NotIndication() + { + var stringBuilder = new StringBuilder(); + + var connectionInfo = new ConnectionInfo("localhost", "admin", "pass", "orion"); + var entity = new Entity { IsIndication = false }; + + stringBuilder.AppendAccessControl(connectionInfo, entity); + + var result = stringBuilder.ToString(); + + Assert.Equal( + $"Can Read: False{Environment.NewLine}" + + $"Can Create: False{Environment.NewLine}" + + $"Can Update: False{Environment.NewLine}" + + $"Can Delete: False{Environment.NewLine}", + result); + } + + + private static void TestAppendMethod(string input, string expected, Action funcToTest) + { + var stringBuilder = new StringBuilder(); + funcToTest(input, stringBuilder); + var result = stringBuilder.ToString(); + Assert.Equal(expected, result); + } + + } +}