diff --git a/CodeBlocker.Test/CodeBlockerExtensionsTests.cs b/CodeBlocker.Test/CodeBlockerExtensionsTests.cs
index 25ffe88..fcb7bf0 100644
--- a/CodeBlocker.Test/CodeBlockerExtensionsTests.cs
+++ b/CodeBlocker.Test/CodeBlockerExtensionsTests.cs
@@ -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()
{
diff --git a/CodeBlocker.Test/TemplateTests.cs b/CodeBlocker.Test/TemplateTests.cs
index c703d78..555d6ae 100644
--- a/CodeBlocker.Test/TemplateTests.cs
+++ b/CodeBlocker.Test/TemplateTests.cs
@@ -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()
{
diff --git a/CodeBlocker/CodeBlockerExtensions.cs b/CodeBlocker/CodeBlockerExtensions.cs
index b1fae56..faddbad 100644
--- a/CodeBlocker/CodeBlockerExtensions.cs
+++ b/CodeBlocker/CodeBlockerExtensions.cs
@@ -73,7 +73,7 @@ public static CodeBlocker WriteNullableDisable(this CodeBlocker codeBlocker)
///
/// 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.
///
/// The to write to.
/// The namespace, for example Contoso.Widgets.
@@ -83,7 +83,7 @@ public static CodeBlocker WriteFileScopedNamespace(this CodeBlocker codeBlocker,
{
Ensure.NotNull(codeBlocker);
- if (string.IsNullOrEmpty(namespaceName))
+ if (string.IsNullOrWhiteSpace(namespaceName))
{
return codeBlocker;
}
diff --git a/CodeBlocker/Templates/SourceFileTemplate.cs b/CodeBlocker/Templates/SourceFileTemplate.cs
index ddaf1ea..b8ec5d1 100644
--- a/CodeBlocker/Templates/SourceFileTemplate.cs
+++ b/CodeBlocker/Templates/SourceFileTemplate.cs
@@ -16,7 +16,7 @@ public class SourceFileTemplate : TemplateBase
///
public string FileName { get; set; } = string.Empty;
- /// Gets or sets the namespace. Empty writes no namespace declaration.
+ /// Gets or sets the namespace. Empty or whitespace writes no namespace declaration.
public string Namespace { get; set; } = string.Empty;
///