-
Notifications
You must be signed in to change notification settings - Fork 144
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
danjagnow
merged 1 commit into
solarwinds:master
from
superzer0:feature/NCM-7385-fixed-append-format
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?