-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from Superzer0/feature/NCM-7385-fixed-append-…
…format Fixing AppendFormat issue + unit tests
- Loading branch information
Showing
4 changed files
with
204 additions
and
20 deletions.
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); | ||
} | ||
|
||
} | ||
} |