Skip to content
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
13 changes: 13 additions & 0 deletions CodeBlocker.Test/CodeBlockerExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ public void AnEmptyNamespaceWritesNothing()
Assert.AreEqual(string.Empty, emptyNamespace.ToString());
}

[TestMethod]
public void AWhitespaceNamespaceWritesNothing()
{
using CodeBlocker spaces = Create();
using CodeBlocker tab = Create();

spaces.WriteFileScopedNamespace(" ");
tab.WriteFileScopedNamespace("\t");

Assert.AreEqual(string.Empty, spaces.ToString());
Assert.AreEqual(string.Empty, tab.ToString());
}

[TestMethod]
public void UsingsAreWrittenOnePerLineAndFollowedByABlankLine()
{
Expand Down
14 changes: 14 additions & 0 deletions CodeBlocker.Test/TemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,20 @@ public void ASourceFileSkipsBlankUsings()
Render(codeBlocker => codeBlocker.AddSourceFile(file)));
}

[TestMethod]
public void ASourceFileWithAWhitespaceNamespaceWritesNoNamespace()
{
SourceFileTemplate file = new()
{
Namespace = " ",
Usings = { " ", "System" },
};

Assert.AreEqual(
"using System;\n\n",
Render(codeBlocker => codeBlocker.AddSourceFile(file)));
}

[TestMethod]
public void ASourceFileWithAnInvalidClassWritesNothing()
{
Expand Down
4 changes: 2 additions & 2 deletions CodeBlocker/CodeBlockerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static CodeBlocker WriteNullableDisable(this CodeBlocker codeBlocker)

/// <summary>
/// Writes a file-scoped namespace declaration followed by a blank line, or nothing at all when
/// no namespace is given.
/// no namespace is given or it is only whitespace.
/// </summary>
/// <param name="codeBlocker">The <see cref="CodeBlocker"/> to write to.</param>
/// <param name="namespaceName">The namespace, for example <c>Contoso.Widgets</c>.</param>
Expand All @@ -83,7 +83,7 @@ public static CodeBlocker WriteFileScopedNamespace(this CodeBlocker codeBlocker,
{
Ensure.NotNull(codeBlocker);

if (string.IsNullOrEmpty(namespaceName))
if (string.IsNullOrWhiteSpace(namespaceName))
{
return codeBlocker;
}
Expand Down
2 changes: 1 addition & 1 deletion CodeBlocker/Templates/SourceFileTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SourceFileTemplate : TemplateBase
/// </summary>
public string FileName { get; set; } = string.Empty;

/// <summary>Gets or sets the namespace. Empty writes no namespace declaration.</summary>
/// <summary>Gets or sets the namespace. Empty or whitespace writes no namespace declaration.</summary>
public string Namespace { get; set; } = string.Empty;

/// <summary>
Expand Down
Loading