Skip to content

Commit

Permalink
In tests only check distinct diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelvo committed Jan 22, 2015
1 parent 19a18d4 commit cc9a15a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,6 @@ public interface InterfaceName
expected =
new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Elements must be documented",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 8, 25)
}
},
// Workaround because the diagnostic is called twice for the SyntaxNode
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down Expand Up @@ -697,17 +685,6 @@ public class OuterClass
expected =
new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Elements must be documented",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 8, 19)
}
},
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down Expand Up @@ -750,17 +727,6 @@ public class OuterClass
expected =
new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Elements must be documented",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 8, 19)
}
},
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,17 +700,6 @@ public class ClassName
expected =
new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Element documentation must have summary",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 8, 22)
}
},
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down Expand Up @@ -790,17 +779,6 @@ public class ClassName
expected =
new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Element documentation must have summary",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 8, 32)
}
},
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,17 +716,6 @@ public class ClassName
expected =
new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Element documentation must have summary text",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 10, 22)
}
},
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down Expand Up @@ -808,17 +797,6 @@ public class ClassName
expected =
new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Element documentation must have summary text",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 10, 32)
}
},
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected static async Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsFrom
}
}

var results = SortDiagnostics(diagnostics);
var results = SortDistinctDiagnostics(diagnostics);
return results.ToImmutableArray();
}

Expand All @@ -104,9 +104,9 @@ protected static async Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsFrom
/// <param name="diagnostics">A collection of <see cref="Diagnostic"/>s to be sorted.</param>
/// <returns>A collection containing the input <paramref name="diagnostics"/>, sorted by
/// <see cref="Diagnostic.Location"/>.</returns>
private static Diagnostic[] SortDiagnostics(IEnumerable<Diagnostic> diagnostics)
private static Diagnostic[] SortDistinctDiagnostics(IEnumerable<Diagnostic> diagnostics)
{
return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).Distinct(default(DiagnosticEqualityComparer)).ToArray();
}

#endregion
Expand Down Expand Up @@ -190,6 +190,43 @@ private static Project CreateProject(string[] sources, string language = Languag
return solution.GetProject(projectId);
}
#endregion

/// <summary>
/// A little helper to be able to use Enumerable.Distinct. Currently Roslyn does have a bug so that Diagnostic.GetHashCode()
/// is not implemented correctly. <see href="https://github.com/dotnet/roslyn/issues/57"/>.
/// </summary>
struct DiagnosticEqualityComparer : IEqualityComparer<Diagnostic>
{
public bool Equals(Diagnostic x, Diagnostic y)
{
return (x == null && y == null)
|| x.Equals(y);
}

public int GetHashCode(Diagnostic obj)
{
return Combine(obj.Descriptor,
Combine(obj.Location.GetHashCode(),
Combine(obj.Severity.GetHashCode(), obj.WarningLevel)
));
}

int Combine<T>(T newKeyPart, int currentKey) where T : class
{
int hash = unchecked(currentKey * (int)0xA5555529);

if (newKeyPart != null)
{
return unchecked(hash + newKeyPart.GetHashCode());
}

return hash;
}
int Combine(int newKey, int currentKey)
{
return unchecked((currentKey * (int)0xA5555529) + newKey);
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ public async Task TestPublicReadonlyFieldStartingWithLowerCase()

var expected = new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Non-private readonly fields must begin with upper-case letter",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 3, 28)
}
},
// Workaround because the diagnostic is called twice for the SyntaxNode
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down Expand Up @@ -80,18 +68,6 @@ public async Task TestProtectedReadonlyFieldStartingWithLowerCase()

var expected = new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Non-private readonly fields must begin with upper-case letter",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 3, 31)
}
},
// Workaround because the diagnostic is called twice for the SyntaxNode
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down Expand Up @@ -129,18 +105,6 @@ public async Task TestInternalReadonlyFieldStartingWithLowerCase()

var expected = new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Non-private readonly fields must begin with upper-case letter",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 3, 30)
}
},
// Workaround because the diagnostic is called twice for the SyntaxNode
new DiagnosticResult
{
Id = DiagnosticId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,18 +936,6 @@ class Bar
new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Use built-in type alias",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 6, 5)
}
},
// Workaround because the diagnostic is called twice for the SyntaxNode
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Use built-in type alias",
Expand Down Expand Up @@ -1006,18 +994,6 @@ class Bar
new[]
{
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Use built-in type alias",
Severity = DiagnosticSeverity.Warning,
Locations =
new[]
{
new DiagnosticResultLocation("Test0.cs", 6, 5)
}
},
// Workaround because the diagnostic is called twice for the SyntaxNode
new DiagnosticResult
{
Id = DiagnosticId,
Message = "Use built-in type alias",
Expand Down

0 comments on commit cc9a15a

Please sign in to comment.