Skip to content

Commit

Permalink
Implement more FxCop examples. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
aueda authored Apr 9, 2019
1 parent 67b6ddd commit e5efac3
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 3 deletions.
4 changes: 2 additions & 2 deletions FxCopDemo.Tests/ConvertHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void ConvertoToStringReturnsIntAsString(
/// if exists.
/// </summary>
[Fact]
public void GetNameReturnsTheNameIfExits()
public void GetNameReturnsTheNameIfExists()
{
var actual = ConvertHelper.GetName(1);
var expected = "one";
Expand All @@ -42,7 +42,7 @@ public void GetNameReturnsTheNameIfExits()
/// if not exists.
/// </summary>
[Fact]
public void GetNameReturnsTheNameIfNotExits()
public void GetNameReturnsTheNameIfNotExists()
{
var actual = ConvertHelper.GetName(15);
var expected = string.Empty;
Expand Down
31 changes: 31 additions & 0 deletions FxCopDemo.Tests/IEmptyInterface2Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Adriano Ueda. All rights reserved.

namespace FxCopDemo.Tests
{
using FxCopDemo;
using Xunit;

public class IEmptyInterface2Tests
{
/// <summary>
/// Tests if mock class implements interface.
/// </summary>
[Fact]
public void TestsIfObjectImplementsInterface()
{
var mockObject = new MockEmptyInterface();

var actual = mockObject is IEmptyInterface2;

Assert.True(actual);
}

/// <summary>
/// Implements a mock class.
/// </summary>
private class MockEmptyInterface
: IEmptyInterface2
{
}
}
}
39 changes: 39 additions & 0 deletions FxCopDemo.Tests/IEmptyInterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Adriano Ueda. All rights reserved.

namespace FxCopDemo.Tests
{
using FxCopDemo;
using Xunit;

public class IEmptyInterfaceTests
{
/// <summary>
/// Tests if mock class implements interface.
/// </summary>
[Fact]
public void TestsIfObjectImplementsInterface()
{
var mockObject = new MockEmptyInterface();

var actual = mockObject is IEmptyInterface;

Assert.True(actual);
}

/// <summary>
/// Implements a mock class.
/// </summary>
private class MockEmptyInterface
: IEmptyInterface
{
/// <summary>
/// Returns the name of the mock object.
/// </summary>
/// <returns>The name of mock object.</returns>
public string GetName()
{
return "Mockup Empty Interface";
}
}
}
}
11 changes: 11 additions & 0 deletions FxCopDemo/ConvertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ namespace FxCopDemo
/// <summary>
/// Implements methods to convert numbers to string.
/// </summary>
/// <remarks>
/// Set DISABLE_FIXES to see FxCop in action.
/// </remarks>
public static class ConvertHelper
{
/// <summary>
/// Initializes the <see cref="ConvertHelper"/> class.
/// </summary>
static ConvertHelper()
{
// Example 1: FxCop will throw a CA1810 warning.
// The dictionary names should be defined inline,
// instead of being initialized in the constructor.
#if DISABLE_FIXES
names = new Dictionary<int, string>
{
Expand All @@ -28,6 +34,8 @@ static ConvertHelper()
#endif
}

// Example 1 (cont'd): the field names should be
// initialized inline.
/// <summary>
/// Gets or sets the helper name.
/// </summary>
Expand All @@ -50,6 +58,9 @@ static ConvertHelper()
/// <returns>A string version of the number.</returns>
public static string ConvertToString(int value)
{
// Example 2: FxCop will throw a CA1315 warning.
// The ToString should specify the culture to
// convert the integer value correctly.
#if DISABLE_FIXES
return value.ToString();
#else
Expand Down
23 changes: 23 additions & 0 deletions FxCopDemo/IEmptyInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Adriano Ueda. All rights reserved.

namespace FxCopDemo
{
/// <summary>
/// Empty interface for demo purposes.
/// </summary>
/// <remarks>
/// Set DISABLE_FIXES to see FxCop in action.
/// </remarks>
public interface IEmptyInterface
{
// Example 3: FxCop will throw a CA1040 warning.
// Implements an empty interface.
#if !DISABLE_FIXES
/// <summary>
/// Gets the property name.
/// </summary>
/// <returns>The name.</returns>
string GetName();
#endif
}
}
25 changes: 25 additions & 0 deletions FxCopDemo/IEmptyInterface2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Adriano Ueda. All rights reserved.

namespace FxCopDemo
{
using System.Diagnostics.CodeAnalysis;

// Example 4: FxCop will not throw a CA1040 warning.
// Implements an empty interface but FxCop will not
// throw an exception because it was told to ignore
// it using the SuppressMessage attribute.

/// <summary>
/// Empty interface for demo purposes.
/// </summary>
/// <remarks>
/// Set DISABLE_FIXES to see FxCop in action.
/// FxCop will throw the CA1040 warning.
/// </remarks>
#if !DISABLE_FIXES
[SuppressMessage("Microsoft.Design", "CA1040:AvoidEmptyInterfaces")]
#endif
public interface IEmptyInterface2
{
}
}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ This is a static analysis demo focused on StyleCop and FxCop for .NET projects.

By default, the solution will build without any warnings.

## Presentation

See the presentation [here](https://tiny.cc/sa-demo).

## FxCop Demo

Add the following conditional to the project settings to enable warnings:
Expand All @@ -21,7 +25,7 @@ Add the following conditional to the project settings to enable warnings:

## License

This project is licensed under the [MIT License](http://opensource.org/licenses/MIT).
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).

## Author

Expand Down

0 comments on commit e5efac3

Please sign in to comment.