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
28 changes: 28 additions & 0 deletions CodeBlocker.Test/ScopesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,34 @@ public void PragmaScopeWithAnEmptyStringIsANoOp()
Assert.AreEqual("public int Value;\n", codeBlocker.ToString());
}

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

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

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

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

using (new PragmaScope(codeBlocker, ["CS0168", "", " CA1707 "]))
{
codeBlocker.WriteLine("int x;");
}

Assert.AreEqual(
"#pragma warning disable CS0168, CA1707\nint x;\n#pragma warning restore CS0168, CA1707\n",
codeBlocker.ToString());
}

[TestMethod]
public void PragmaScopeWithAnEmptyWarningSequenceDoesNotAffectAnOuterScope()
{
Expand Down
10 changes: 8 additions & 2 deletions CodeBlocker/Scopes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,18 @@ public class PragmaScope(CodeBlocker codeBlocker, string warnings)
/// Create a new instance of <see cref="PragmaScope"/> suppressing several warnings.
/// </summary>
/// <param name="codeBlocker">The parent <see cref="CodeBlocker"/>.</param>
/// <param name="warnings">The warning identifiers to suppress.</param>
/// <param name="warnings">
/// The warning identifiers to suppress. Each identifier is trimmed, and null, empty or whitespace
/// entries are skipped, so a sequence holding none but those is a no-op and emits no directives.
/// </param>
public PragmaScope(CodeBlocker codeBlocker, IEnumerable<string> warnings)
: this(codeBlocker, string.Join(", ", warnings ?? []))
: this(codeBlocker, JoinWarnings(warnings))
{
}

private static string JoinWarnings(IEnumerable<string>? warnings) =>
string.Join(", ", (warnings ?? []).Where(warning => !string.IsNullOrWhiteSpace(warning)).Select(warning => warning.Trim()));

private static void Begin(CodeBlocker codeBlocker, string warnings)
{
Ensure.NotNull(codeBlocker);
Expand Down
Loading