Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing AppendFormat issue + unit tests #300

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions Src/SwqlStudio/ObjectExplorer/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
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)
{

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Why the blank line here?

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}

}
}