Skip to content

Commit

Permalink
Merge pull request #300 from Superzer0/feature/NCM-7385-fixed-append-…
Browse files Browse the repository at this point in the history
…format

Fixing AppendFormat issue + unit tests
  • Loading branch information
danjagnow committed Oct 4, 2021
2 parents 5a2073a + eb44ecd commit e7b9675
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 20 deletions.
60 changes: 50 additions & 10 deletions Src/SwqlStudio/ObjectExplorer/StringBuilderExtensions.cs
Expand Up @@ -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}");
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions 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;
Expand Down
6 changes: 1 addition & 5 deletions 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;
Expand Down
152 changes: 152 additions & 0 deletions 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<Action>
{
() => 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<ArgumentNullException>(() =>
{
method();
});
}
}

[Theory]
[MemberData(nameof(AppendSummaryTrimsInputTestCases))]
public void AppendSummaryTests(string input, string expected)
{
TestAppendMethod(input, expected, (s, stringB) => stringB.AppendSummary(s));
}

public static IEnumerable<object[]> AppendSummaryTrimsInputTestCases = new List<object[]>
{
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<object[]> AppendSummaryParagraphTestCases = new List<object[]>
{
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<object[]> AppendNameTestCases = new List<object[]>
{
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<object[]> AppendTypeTestCases = new List<object[]>
{
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<object[]> AppendObsoleteSectionTestCases = new List<object[]>
{
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<string, StringBuilder> funcToTest)
{
var stringBuilder = new StringBuilder();
funcToTest(input, stringBuilder);
var result = stringBuilder.ToString();
Assert.Equal(expected, result);
}

}
}

0 comments on commit e7b9675

Please sign in to comment.