Skip to content

Commit

Permalink
Validate arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Apr 25, 2021
1 parent d321dce commit beea4f3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Tests/Testing.Common/Testing/AdditionalFile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
Expand All @@ -20,7 +21,7 @@ public readonly struct AdditionalFile
/// <param name="expectedSource"></param>
public AdditionalFile(string source, string expectedSource = null)
{
Source = source;
Source = source ?? throw new ArgumentNullException(nameof(source));
ExpectedSource = expectedSource;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Testing.Common/Testing/CodeVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class CodeVerifier
{
internal CodeVerifier(IAssert assert)
{
Assert = assert;
Assert = assert ?? throw new ArgumentNullException(nameof(assert));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public async Task VerifyFixAsync(
TestOptions options = null,
CancellationToken cancellationToken = default)
{
if (data == null)
throw new ArgumentNullException(nameof(data));

if (expected == null)
throw new ArgumentNullException(nameof(expected));

cancellationToken.ThrowIfCancellationRequested();

options ??= Options;
Expand Down Expand Up @@ -162,6 +168,9 @@ public async Task VerifyNoFixAsync(
TestOptions options = null,
CancellationToken cancellationToken = default)
{
if (data == null)
throw new ArgumentNullException(nameof(data));

cancellationToken.ThrowIfCancellationRequested();

options ??= Options;
Expand Down
15 changes: 15 additions & 0 deletions src/Tests/Testing.Common/Testing/DiagnosticVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public async Task VerifyDiagnosticAsync(
TestOptions options = null,
CancellationToken cancellationToken = default)
{
if (data == null)
throw new ArgumentNullException(nameof(data));

cancellationToken.ThrowIfCancellationRequested();

options ??= Options;
Expand Down Expand Up @@ -105,6 +108,9 @@ public async Task VerifyNoDiagnosticAsync(
TestOptions options = null,
CancellationToken cancellationToken = default)
{
if (data == null)
throw new ArgumentNullException(nameof(data));

cancellationToken.ThrowIfCancellationRequested();

options ??= Options;
Expand Down Expand Up @@ -176,6 +182,12 @@ private async Task VerifyFixAsync(
TestOptions options = null,
CancellationToken cancellationToken = default)
{
if (data == null)
throw new ArgumentNullException(nameof(data));

if (expected == null)
throw new ArgumentNullException(nameof(expected));

cancellationToken.ThrowIfCancellationRequested();

options ??= Options;
Expand Down Expand Up @@ -315,6 +327,9 @@ private async Task VerifyNoFixAsync(
TestOptions options = null,
CancellationToken cancellationToken = default)
{
if (data == null)
throw new ArgumentNullException(nameof(data));

cancellationToken.ThrowIfCancellationRequested();

options ??= Options;
Expand Down
5 changes: 3 additions & 2 deletions src/Tests/Testing.Common/Testing/ExpectedDocument.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.CodeAnalysis;

namespace Roslynator.Testing
Expand All @@ -8,8 +9,8 @@ internal readonly struct ExpectedDocument
{
public ExpectedDocument(DocumentId id, string text)
{
Id = id;
Text = text;
Id = id ?? throw new ArgumentNullException(nameof(id));
Text = text ?? throw new ArgumentNullException(nameof(text));
}

public DocumentId Id { get; }
Expand Down
9 changes: 9 additions & 0 deletions src/Tests/Testing.Common/Testing/RefactoringVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public async Task VerifyRefactoringAsync(
TestOptions options = null,
CancellationToken cancellationToken = default)
{
if (data == null)
throw new ArgumentNullException(nameof(data));

if (expected == null)
throw new ArgumentNullException(nameof(expected));

if (data.Spans.IsEmpty)
Fail("Span on which a refactoring should be invoked was not found.");

Expand Down Expand Up @@ -108,6 +114,9 @@ public async Task VerifyNoRefactoringAsync(
TestOptions options = null,
CancellationToken cancellationToken = default)
{
if (data == null)
throw new ArgumentNullException(nameof(data));

if (data.Spans.IsEmpty)
Fail("Span on which a refactoring should be invoked was not found.");

Expand Down
16 changes: 15 additions & 1 deletion src/Tests/Testing.Common/Testing/TestCode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.Text;
Expand All @@ -20,8 +21,12 @@ internal TestCode(string value, ImmutableArray<TextSpan> spans, ImmutableArray<T

internal TestCode(string value, string expectedValue, ImmutableArray<TextSpan> spans, ImmutableArray<TextSpan> additionalSpans)
{
Value = value;
Value = value ?? throw new ArgumentNullException(nameof(value));
ExpectedValue = expectedValue;

if (spans.Length == 0)
throw new ArgumentException("'spans' cannot be empty.", nameof(spans));

Spans = spans;
AdditionalSpans = (additionalSpans.IsDefault) ? ImmutableArray<TextSpan>.Empty : additionalSpans;
}
Expand Down Expand Up @@ -55,6 +60,9 @@ internal TestCode(string value, string expectedValue, ImmutableArray<TextSpan> s
/// <param name="value"></param>
public static TestCode Parse(string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));

(string source, ImmutableArray<TextSpan> spans) = FindSpansAndRemove(value);

(string source2, ImmutableArray<TextSpan> additionalSpans) = FindAnnotatedSpansAndRemove(source, "a");
Expand All @@ -73,6 +81,12 @@ public static TestCode Parse(
string replacement1,
string replacement2 = null)
{
if (value == null)
throw new ArgumentNullException(nameof(value));

if (replacement1 == null)
throw new ArgumentNullException(nameof(replacement1));

(string source, string expected, ImmutableArray<TextSpan> spans) = FindSpansAndReplace(value, replacement1, replacement2);

(string source2, ImmutableArray<TextSpan> additionalSpans) = FindAnnotatedSpansAndRemove(source, "a");
Expand Down

0 comments on commit beea4f3

Please sign in to comment.