Skip to content

Commit

Permalink
Move usings to top of file and using file-scoped namespaces (#40)
Browse files Browse the repository at this point in the history
* Move usings outside namespace

* Rebaseline tests

* Use file-scoped namespaces

* Rebaseline tests
  • Loading branch information
MattKotsenas authored Jun 3, 2024
1 parent 0fb11a7 commit 3e12add
Show file tree
Hide file tree
Showing 53 changed files with 1,834 additions and 1,875 deletions.
7 changes: 5 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ insert_final_newline = true
[*.{cs,vb}]

# Organize usings
dotnet_separate_import_directive_groups = true
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = true
file_header_template = unset

Expand Down Expand Up @@ -132,7 +132,10 @@ csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:silent

# 'using' directive preferences
csharp_using_directive_placement = outside_namespace:silent
csharp_using_directive_placement = outside_namespace:warning

# namespace preferences
csharp_style_namespace_declarations = file_scoped:warning

#### C# Formatting Rules ####

Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
Diagnostic 1
Id: Moq1002
Location: SourceFile(Test1.cs[247..253))
Location: SourceFile(Test1.cs[223..229))
Highlight: ("42")
Lines: var mock = new Mock<AbstractClassWithCtor>("42");
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 2
Id: Moq1002
Location: SourceFile(Test1.cs[420..430))
Location: SourceFile(Test1.cs[388..398))
Highlight: ("42", 42)
Lines: var mock1 = new Mock<AbstractClassWithCtor>("42", 42);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 3
Id: Moq1002
Location: SourceFile(Test1.cs[559..563))
Location: SourceFile(Test1.cs[519..523))
Highlight: (42)
Lines: var mock2 = new Mock<AbstractClassDefaultCtor>(42);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 4
Id: Moq1002
Location: SourceFile(Test1.cs[780..786))
Location: SourceFile(Test1.cs[720..726))
Highlight: ("42")
Lines: var mock = new Mock<AbstractGenericClassWithCtor<object>>("42");
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 5
Id: Moq1002
Location: SourceFile(Test1.cs[968..978))
Location: SourceFile(Test1.cs[900..910))
Highlight: ("42", 42)
Lines: var mock1 = new Mock<AbstractGenericClassWithCtor<object>>("42", 42);
Severity: Warning
Message: Parameters provided into mock do not match any existing constructors.

Diagnostic 6
Id: Moq1002
Location: SourceFile(Test1.cs[1122..1126))
Location: SourceFile(Test1.cs[1046..1050))
Highlight: (42)
Lines: var mock2 = new Mock<AbstractGenericClassDefaultCtor<object>>(42);
Severity: Warning
Expand Down
42 changes: 19 additions & 23 deletions Source/Moq.Analyzers.Test/AbstractClassTests.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
using Microsoft.CodeAnalysis.Diagnostics;
using System.IO;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Xunit;

namespace Moq.Analyzers.Test
{
using System.IO;

using TestHelper;

using Xunit;
namespace Moq.Analyzers.Test;

public class AbstractClassTests : DiagnosticVerifier
public class AbstractClassTests : DiagnosticVerifier
{
[Fact]
public Task ShouldPassIfGoodParametersAndFailOnTypeMismatch()
{
[Fact]
public Task ShouldPassIfGoodParametersAndFailOnTypeMismatch()
{
return Verify(VerifyCSharpDiagnostic(
[
File.ReadAllText("Data/AbstractClass.Good.cs"),
File.ReadAllText("Data/AbstractClass.Bad.cs")
]
));
}
return Verify(VerifyCSharpDiagnostic(
[
File.ReadAllText("Data/AbstractClass.Good.cs"),
File.ReadAllText("Data/AbstractClass.Bad.cs")
]
));
}

protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new ConstructorArgumentsShouldMatchAnalyzer();
}
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new ConstructorArgumentsShouldMatchAnalyzer();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Diagnostic 1
Id: Moq1300
Location: SourceFile(Test0.cs[1369..1384))
Location: SourceFile(Test0.cs[1212..1227))
Highlight: BaseSampleClass
Lines: mock.As<BaseSampleClass>();
Severity: Error
Message: Mock.As() should take interfaces only

Diagnostic 2
Id: Moq1300
Location: SourceFile(Test0.cs[1534..1544))
Location: SourceFile(Test0.cs[1357..1367))
Highlight: OtherClass
Lines: mock.As<OtherClass>();
Severity: Error
Expand Down
35 changes: 17 additions & 18 deletions Source/Moq.Analyzers.Test/AsAcceptOnlyInterfaceAnalyzerTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
namespace Moq.Analyzers.Test
{
using System.IO;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Xunit;
using System.IO;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Xunit;

namespace Moq.Analyzers.Test;

public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier
public class AsAcceptOnlyInterfaceAnalyzerTests : DiagnosticVerifier
{
[Fact]
public Task ShouldPassIfGoodParameters()
{
[Fact]
public Task ShouldPassIfGoodParameters()
{
return Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/AsAcceptOnlyInterface.cs")));
}
return Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/AsAcceptOnlyInterface.cs")));
}

protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new AsShouldBeUsedOnlyForInterfaceAnalyzer();
}
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new AsShouldBeUsedOnlyForInterfaceAnalyzer();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
Diagnostic 1
Id: Moq1100
Location: SourceFile(Test0.cs[1090..1097))
Location: SourceFile(Test0.cs[1030..1037))
Highlight: (int i)
Lines: mock.Setup(x => x.Do(It.IsAny<string>())).Callback((int i) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.

Diagnostic 2
Id: Moq1100
Location: SourceFile(Test0.cs[1170..1192))
Location: SourceFile(Test0.cs[1106..1128))
Highlight: (string s1, string s2)
Lines: mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s1, string s2) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.

Diagnostic 3
Id: Moq1100
Location: SourceFile(Test0.cs[1304..1323))
Location: SourceFile(Test0.cs[1236..1255))
Highlight: (string s1, int i1)
Lines: mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((string s1, int i1) => { });
Severity: Warning
Message: Callback signature must match the signature of the mocked method.

Diagnostic 4
Id: Moq1100
Location: SourceFile(Test0.cs[1402..1409))
Location: SourceFile(Test0.cs[1330..1337))
Highlight: (int i)
Lines: mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((int i) => { });
Severity: Warning
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
namespace Moq.Analyzers.Test
{
using System.IO;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Xunit;
using System.IO;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Xunit;

namespace Moq.Analyzers.Test;

public class CallbackSignatureShouldMatchMockedMethodAnalyzerTests : DiagnosticVerifier
public class CallbackSignatureShouldMatchMockedMethodAnalyzerTests : DiagnosticVerifier
{
[Fact]
public Task ShouldPassIfGoodParameters()
{
[Fact]
public Task ShouldPassIfGoodParameters()
{
return Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/CallbackSignatureShouldMatchMockedMethod.cs")));
}
return Verify(VerifyCSharpDiagnostic(File.ReadAllText("Data/CallbackSignatureShouldMatchMockedMethod.cs")));
}

protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new CallbackSignatureShouldMatchMockedMethodAnalyzer();
}
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new CallbackSignatureShouldMatchMockedMethodAnalyzer();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#pragma warning disable SA1402 // File may only contain a single class
using System;
using System.Collections.Generic;
using Moq;

#pragma warning disable SA1402 // File may only contain a single class
#pragma warning disable SA1502 // Element must not be on a single line
#pragma warning disable SA1602 // Undocumented enum values
#pragma warning disable SA1649 // File name must match first type name
Expand All @@ -8,62 +12,57 @@
#pragma warning disable IDE0051 // Unused private member
#pragma warning disable IDE0059 // Unnecessary value assignment
#pragma warning disable IDE0060 // Unused parameter
namespace CallbackSignatureShouldMatchMockedMethod
namespace CallbackSignatureShouldMatchMockedMethod;

internal interface IFoo
{
using System;
using System.Collections.Generic;
using Moq;
int Do(string s);

internal interface IFoo
{
int Do(string s);
int Do(int i, string s, DateTime dt);

int Do(int i, string s, DateTime dt);
int Do(List<string> l);
}

int Do(List<string> l);
internal class MyUnitTests
{
private void TestBadCallbacks()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((List<string> l) => { });
}

internal class MyUnitTests
private void TestGoodSetupAndParameterlessCallback()
{
private void TestBadCallbacks()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((List<string> l) => { });
}

private void TestGoodSetupAndParameterlessCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback(() => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback(() => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback(() => { });
}
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback(() => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback(() => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback(() => { });
}

private void TestGoodSetupAndCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((List<string> l) => { });
}
private void TestGoodSetupAndCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Callback((List<string> l) => { });
}

private void TestGoodSetupAndReturnsAndCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Returns(0).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Returns(0).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Returns(0).Callback((List<string> l) => { });
}
private void TestGoodSetupAndReturnsAndCallback()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Returns(0).Callback((string s) => { });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Returns(0).Callback((int i, string s, DateTime dt) => { });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Returns(0).Callback((List<string> l) => { });
}

private void MyGoodSetupAndReturns()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Returns((string s) => { return 0; });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Returns((int i, string s, DateTime dt) => { return 0; });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Returns((List<string> l) => { return 0; });
}
private void MyGoodSetupAndReturns()
{
var mock = new Mock<IFoo>();
mock.Setup(x => x.Do(It.IsAny<string>())).Returns((string s) => { return 0; });
mock.Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>())).Returns((int i, string s, DateTime dt) => { return 0; });
mock.Setup(x => x.Do(It.IsAny<List<string>>())).Returns((List<string> l) => { return 0; });
}
}
}
Loading

0 comments on commit 3e12add

Please sign in to comment.