Skip to content

Commit

Permalink
Merge pull request #36 from CHG-MERIDIAN/features/FixIssue35-Normaliz…
Browse files Browse the repository at this point in the history
…eEndOfLine

Add option to define new line style
  • Loading branch information
twenzel authored Mar 12, 2019
2 parents e2a3dbc + 20e52b3 commit 675f7dc
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 38 deletions.
46 changes: 30 additions & 16 deletions src/Internal/SecurityTextContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Validate()
if (!string.IsNullOrEmpty(Text))
return;

ValidateContact();
ValidateContact();
ValidateAcknowledgments();
ValidateEncryption();
ValidateHiring();
Expand All @@ -59,49 +59,49 @@ public void Validate()
private void AddPermission(StringBuilder builder)
{
if (!string.IsNullOrEmpty(Permission))
builder.AppendLine($"Permission: {Permission}");
AppendLine(builder, $"Permission: {Permission}");
}

private void AddHiring(StringBuilder builder)
{
if (!string.IsNullOrEmpty(Hiring))
builder.AppendLine($"Hiring: {Hiring}");
AppendLine(builder, $"Hiring: {Hiring}");
}

private void AddAcknowledgments(StringBuilder builder)
{
if (!string.IsNullOrEmpty(Acknowledgments))
builder.AppendLine($"Acknowledgments: {Acknowledgments}");
AppendLine(builder, $"Acknowledgments: {Acknowledgments}");
}

private void AddPolicy(StringBuilder builder)
{
if (!string.IsNullOrEmpty(Policy))
builder.AppendLine($"Policy: {Policy}");
AppendLine(builder, $"Policy: {Policy}");
}

private void AddSignature(StringBuilder builder)
{
if (!string.IsNullOrEmpty(Signature))
builder.AppendLine($"Signature: {Signature}");
AppendLine(builder, $"Signature: {Signature}");
}

private void AddEncryption(StringBuilder builder)
{
if (!string.IsNullOrEmpty(Encryption))
builder.AppendLine($"Encryption: {Encryption}");
AppendLine(builder, $"Encryption: {Encryption}");
}

private void AddContact(StringBuilder builder)
{
if (!string.IsNullOrEmpty(Contact))
builder.Append(ExtractMultiple("Contact: ", Contact));
builder.Append(ExtractMultiple("Contact: ", Contact, NewLineString));
}

private void AddIntroduction(StringBuilder builder)
{
if (!string.IsNullOrEmpty(Introduction))
builder.AppendLine(CreateComment(Introduction));
AppendLine(builder, CreateComment(Introduction));
}

/// <summary>
Expand Down Expand Up @@ -254,34 +254,43 @@ private bool IsValidEmail(string email)
}
}

private string CreateComment(string value)
{
return CreateComment(value, NewLineString);
}

/// <summary>
/// Creates a comment from the given value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
internal static string CreateComment(string value)
internal static string CreateComment(string value, string newLineString)
{
if (string.IsNullOrEmpty(value))
return string.Empty;

return COMMENT_PREFIX +
value.Replace("\r\n", "~~")
.Replace("\r", "~~")
.Replace("\n", "~~")
.Replace("~~", Environment.NewLine + COMMENT_PREFIX);
value.Replace("\r\n", "\n")
.Replace("\r", "\n")
.Replace("\n", newLineString + COMMENT_PREFIX);
}

private static string ExtractMultiple(string directive, string value)
private static string ExtractMultiple(string directive, string value, string newLineString)
{
var values = value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var builder = new StringBuilder();

foreach (var item in values)
builder.AppendLine($"{directive}{item}");
builder.Append($"{directive}{item}").Append(newLineString);

return builder.ToString();
}

private void AppendLine(StringBuilder builder, string value)
{
builder.Append(value).Append(NewLineString);
}

/// <summary>
/// Gets or sets the whole security text.
/// </summary>
Expand Down Expand Up @@ -340,5 +349,10 @@ private static string ExtractMultiple(string directive, string value)
/// </summary>
public bool ValidateValues { get; set; } = true;

/// <summary>
/// Gets or sets the string to define a new line
/// </summary>
public string NewLineString { get; set; } = Environment.NewLine;

}
}
32 changes: 26 additions & 6 deletions src/SecurityTextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,32 @@ public SecurityTextBuilder SetIntroduction(string value)
_container.Introduction = value;
return this;
}

/// <summary>
/// Builds the security information text
/// </summary>
/// <returns></returns>
public SecurityTextContainer GetContainer()

/// <summary>
/// Use Carriage Return And Line Feed (CR LF) as new line style
/// </summary>
/// <returns></returns>
public SecurityTextBuilder UseWindowsStyleNewLine()
{
_container.NewLineString = "\r\n";
return this;
}

/// <summary>
/// Use Line Feed (LF) as new line style
/// </summary>
/// <returns></returns>
public SecurityTextBuilder UseUnixStyleNewLine()
{
_container.NewLineString = "\n";
return this;
}

/// <summary>
/// Builds the security information text
/// </summary>
/// <returns></returns>
public SecurityTextContainer GetContainer()
{
return _container;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,25 @@ public void Set_Introduction_Value()
.GetContainer().Build().Should().Be("# The ACME Security information.");
}
}
}

public class UseWindowsStyleNewLineMethod: SecurityTextBuilderTests
{
[Test]
public void Sets_Line_Style()
{
_builder.UseWindowsStyleNewLine()
.GetContainer().NewLineString.Should().Be("\r\n");
}
}

public class UseUnixStyleNewLineMethod : SecurityTextBuilderTests
{
[Test]
public void Sets_Line_Style()
{
_builder.UseUnixStyleNewLine()
.GetContainer().NewLineString.Should().Be("\n");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ public void Returns_Always_Text_When_Given()
_container.Build().Should().Be("Test\n123");
}

[Test]
public void Returns_Security_Infos()
[TestCase("\n")]
[TestCase("\r\n")]
public void Returns_Security_Infos(string newLineStyle)
{
_container.Contact = "mailto:[email protected]";
_container.NewLineString = newLineStyle;
_container.Contact = "mailto:[email protected]";
_container.Encryption = "https://example.com/pgp-key.txt";
_container.Hiring = "https://example.com/jobs.html";
_container.Acknowledgments = "https://example.com/hall-of-fame.html";
Expand All @@ -57,13 +59,13 @@ public void Returns_Security_Infos()
_container.Introduction = "The ACME Security information.";
_container.Permission = "none";

_container.Build().Should().Be("# The ACME Security information." + Environment.NewLine +
"Contact: mailto:[email protected]" + Environment.NewLine +
"Encryption: https://example.com/pgp-key.txt" + Environment.NewLine +
"Signature: https://example.com/.well-known/security.txt.sig" + Environment.NewLine +
"Policy: https://example.com/security-policy.html" + Environment.NewLine +
"Acknowledgments: https://example.com/hall-of-fame.html" + Environment.NewLine +
"Hiring: https://example.com/jobs.html" + Environment.NewLine +
_container.Build().Should().Be("# The ACME Security information." + newLineStyle +
"Contact: mailto:[email protected]" + newLineStyle +
"Encryption: https://example.com/pgp-key.txt" + newLineStyle +
"Signature: https://example.com/.well-known/security.txt.sig" + newLineStyle +
"Policy: https://example.com/security-policy.html" + newLineStyle +
"Acknowledgments: https://example.com/hall-of-fame.html" + newLineStyle +
"Hiring: https://example.com/jobs.html" + newLineStyle +
"Permission: none");
}
}
Expand All @@ -73,27 +75,33 @@ public class CreateCommentMethod : SecurityTextContainerTests
[Test]
public void Returns_Empty_For_EmptyValue()
{
SecurityTextContainer.CreateComment("").Should().BeEmpty();
SecurityTextContainer.CreateComment("", Environment.NewLine).Should().BeEmpty();
}

[Test]
public void Returns_Empty_For_Null_Value()
{
SecurityTextContainer.CreateComment(null).Should().BeEmpty();
SecurityTextContainer.CreateComment(null, Environment.NewLine).Should().BeEmpty();
}

[Test]
public void Returns_Single_Line()
{
SecurityTextContainer.CreateComment("test").Should().Be("# test");
SecurityTextContainer.CreateComment("test", Environment.NewLine).Should().Be("# test");
}

[Test]
public void Returns_Multiline_With_Prefix()
{
SecurityTextContainer.CreateComment("test\r\nother line").Should().Be("# test\r\n# other line");
SecurityTextContainer.CreateComment("test\r\nother line", Environment.NewLine).Should().Be("# test\r\n# other line");
}
}

[Test]
public void Returns_Multiline_With_Prefix_With_Mixed_NewLine_Style()
{
SecurityTextContainer.CreateComment("test\r\nother line\nanother line", Environment.NewLine).Should().Be("# test\r\n# other line\r\n# another line");
}
}

public class ValidateMethod : SecurityTextContainerTests
{
Expand Down

0 comments on commit 675f7dc

Please sign in to comment.