Skip to content
Open
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
46 changes: 46 additions & 0 deletions CodeBlocker.Test/ScopesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,52 @@ public void DirectiveScopeWrapsTheBodyInAConditional()
codeBlocker.ToString());
}

[TestMethod]
[DataRow("", DisplayName = "Empty")]
[DataRow(" ", DisplayName = "Whitespace")]
[DataRow(null, DisplayName = "Null")]
public void DirectiveScopeRejectsAMissingConditionBeforeWriting(string? condition)
{
using CodeBlocker codeBlocker = Create();

Assert.ThrowsExactly<ArgumentException>(() => new DirectiveScope(codeBlocker, condition!));

Assert.AreEqual(string.Empty, codeBlocker.ToString());
}

[TestMethod]
[DataRow("\n", DisplayName = "LF")]
[DataRow("\r\n", DisplayName = "CRLF")]
[DataRow("\r", DisplayName = "CR")]
[DataRow("\u0085", DisplayName = "NEL")]
[DataRow("\u2028", DisplayName = "LINE SEPARATOR")]
[DataRow("\u2029", DisplayName = "PARAGRAPH SEPARATOR")]
public void DirectiveScopeRejectsAConditionThatSpansLinesBeforeWriting(string terminator)
{
using CodeBlocker codeBlocker = Create();

Assert.ThrowsExactly<ArgumentException>(() => new DirectiveScope(codeBlocker, $"DEBUG{terminator}|| TRACE"));

Assert.AreEqual(string.Empty, codeBlocker.ToString());
}

[TestMethod]
[DataRow("\n", DisplayName = "LF")]
[DataRow("\r\n", DisplayName = "CRLF")]
[DataRow("\r", DisplayName = "CR")]
[DataRow("\u0085", DisplayName = "NEL")]
[DataRow("\u2028", DisplayName = "LINE SEPARATOR")]
[DataRow("\u2029", DisplayName = "PARAGRAPH SEPARATOR")]
public void PragmaScopeRejectsWarningsThatSpanLinesBeforeWriting(string terminator)
{
using CodeBlocker codeBlocker = Create();

Assert.ThrowsExactly<ArgumentException>(() => new PragmaScope(codeBlocker, $"CS1591{terminator}CA1707"));
Assert.ThrowsExactly<ArgumentException>(() => new PragmaScope(codeBlocker, [$"CS1591{terminator}", "CA1707"]));

Assert.AreEqual(string.Empty, codeBlocker.ToString());
}

[TestMethod]
public void PragmaScopeDisablesAndRestoresTheSameWarnings()
{
Expand Down
24 changes: 24 additions & 0 deletions CodeBlocker/Scopes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,27 @@ private static void End(CodeBlocker codeBlocker)
/// </remarks>
/// <param name="codeBlocker">The parent <see cref="CodeBlocker"/>.</param>
/// <param name="condition">The condition expression, for example <c>NET8_0_OR_GREATER</c>.</param>
/// <exception cref="ArgumentException">
/// <paramref name="condition"/> is null, empty or whitespace, which would write a bare <c>#if</c>, or
/// contains a line terminator, which would leave the rest of the condition outside the directive.
/// Nothing is written.
/// </exception>
public class DirectiveScope(CodeBlocker codeBlocker, string condition)
: ScopedAction(onOpen: () => Begin(codeBlocker, condition), onClose: () => End(codeBlocker))
{
private static void Begin(CodeBlocker codeBlocker, string condition)
{
Ensure.NotNull(codeBlocker);
if (string.IsNullOrWhiteSpace(condition))
{
throw new ArgumentException("A conditional directive needs a condition.", nameof(condition));
}

if (LineTerminators.Contains(condition))
{
throw new ArgumentException("A condition must fit on the #if line.", nameof(condition));
}

codeBlocker.WriteLine($"#if {condition}");
}

Expand All @@ -171,6 +186,10 @@ private static void End(CodeBlocker codeBlocker)
/// identifier such as <c>CS1591</c> or a comma-separated list. Empty or whitespace warnings are a
/// no-op and emit no directives.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="warnings"/> contains a line terminator, which would leave the rest of the list
/// outside the directive. Nothing is written.
/// </exception>
public class PragmaScope(CodeBlocker codeBlocker, string warnings)
: ScopedAction(onOpen: () => Begin(codeBlocker, warnings), onClose: () => End(codeBlocker, warnings))
{
Expand All @@ -192,6 +211,11 @@ private static void Begin(CodeBlocker codeBlocker, string warnings)
return;
}

if (LineTerminators.Contains(warnings))
{
throw new ArgumentException("A warning list must fit on the #pragma line.", nameof(warnings));
}

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

Expand Down
Loading