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
33 changes: 33 additions & 0 deletions CodeBlocker.Test/ScopesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,39 @@ public void PragmaScopeJoinsSeveralWarnings()
codeBlocker.ToString());
}

[TestMethod]
public void PragmaScopeWithAnEmptyStringIsANoOp()
{
using CodeBlocker codeBlocker = Create();

using (new PragmaScope(codeBlocker, ""))
{
codeBlocker.WriteLine("public int Value;");
}

Assert.AreEqual("public int Value;\n", codeBlocker.ToString());
}

[TestMethod]
public void PragmaScopeWithAnEmptyWarningSequenceDoesNotAffectAnOuterScope()
{
using CodeBlocker codeBlocker = Create();

using (new PragmaScope(codeBlocker, "CS1591"))
{
using (new PragmaScope(codeBlocker, []))
{
codeBlocker.WriteLine("public int X;");
}

codeBlocker.WriteLine("public int Y;");
}

Assert.AreEqual(
"#pragma warning disable CS1591\npublic int X;\npublic int Y;\n#pragma warning restore CS1591\n",
codeBlocker.ToString());
}

[TestMethod]
public void ScopesOfMixedKindsNestCorrectly()
{
Expand Down
13 changes: 12 additions & 1 deletion CodeBlocker/Scopes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ private static void End(CodeBlocker codeBlocker)
/// <param name="codeBlocker">The parent <see cref="CodeBlocker"/>.</param>
/// <param name="warnings">
/// The warning identifiers to suppress, written verbatim after the directive — either a single
/// identifier such as <c>CS1591</c> or a comma-separated list.
/// identifier such as <c>CS1591</c> or a comma-separated list. Empty or whitespace warnings are a
/// no-op and emit no directives.
/// </param>
public class PragmaScope(CodeBlocker codeBlocker, string warnings)
: ScopedAction(onOpen: () => Begin(codeBlocker, warnings), onClose: () => End(codeBlocker, warnings))
Expand All @@ -177,12 +178,22 @@ public PragmaScope(CodeBlocker codeBlocker, IEnumerable<string> warnings)
private static void Begin(CodeBlocker codeBlocker, string warnings)
{
Ensure.NotNull(codeBlocker);
if (string.IsNullOrWhiteSpace(warnings))
{
return;
}

codeBlocker.WriteLine($"#pragma warning disable {warnings}");
}

private static void End(CodeBlocker codeBlocker, string warnings)
{
Ensure.NotNull(codeBlocker);
if (string.IsNullOrWhiteSpace(warnings))
{
return;
}

codeBlocker.WriteLine($"#pragma warning restore {warnings}");
}
}