-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Files array results matching #1234
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c3c82f9
WIP preliminary results matching
michaelcfanning f8db812
Restore results matching for files array
583bdf9
Merge remote-tracking branch 'origin' into files-array-results-matching
5e46269
Add back autogenerated (unused) namespace directive
c10661d
Merge branch 'files-array' into files-array-results-matching
michaelcfanning File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/Sarif.UnitTests/OrderSensitiveValueComparisonListTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.UnitTests | ||
{ | ||
/// <summary> | ||
/// A comparer, analagous to the internal .NET ObjectEqualityComparer, that | ||
/// provides comparison semantics roughly equivalent to the default .NET | ||
/// behavior provided by Object.Equals and friends (this functionality is | ||
/// what's invoked when calling List.Contains). Our comparer only exists | ||
/// for testing, because ObjectEqualityComparer is an internal type. We | ||
/// don't cover all possible cases to make the types equivalent; the | ||
/// implementation covers enough for core validation. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
internal class DefaultObjectComparer<T> : IEqualityComparer<T> | ||
{ | ||
public bool Equals(T x, T y) | ||
{ | ||
return Object.Equals(x, y); | ||
} | ||
|
||
public int GetHashCode(T obj) | ||
{ | ||
if (obj == null) { return 0; } | ||
return obj.GetHashCode(); | ||
} | ||
} | ||
|
||
public class OrderSensitiveValueComparisonListTests | ||
{ | ||
[Fact] | ||
public void OrderSensitiveValueComparisonListTests_DefaultObjectComparer() | ||
{ | ||
var equalityComparer = new DefaultObjectComparer<FileChange>(); | ||
|
||
var listOne = CreateTestList(equalityComparer); | ||
|
||
// Populate the second list with references from the first. | ||
var listTwo = new OrderSensitiveValueComparisonList<FileChange>(equalityComparer); | ||
for (int i = 0; i < listOne.Count; i++) | ||
{ | ||
listTwo.Add(listOne[i]); | ||
} | ||
|
||
// Every list s/be equal to itself. | ||
listOne.Equals(listOne).Should().Be(true); | ||
|
||
// Two lists with shared objects, by reference, in the same | ||
// order should be regarded as equivalent. | ||
listOne.Equals(listTwo).Should().Be(true); | ||
|
||
FileChange toSwap = listTwo[0]; | ||
listTwo[0] = listTwo[1]; | ||
listTwo[1] = toSwap; | ||
|
||
// We have reordered two objects that are themselves identical. | ||
// The comparison should fail as the order of references changed. | ||
listOne.Equals(listTwo).Should().Be(false); | ||
} | ||
|
||
[Fact] | ||
public void OrderSensitiveValueComparisonListTests_ValueComparer() | ||
{ | ||
// Two identical lists with elements that are | ||
// distinct objects, by reference. | ||
var listOne = CreateTestList(FileChange.ValueComparer); | ||
var listTwo = CreateTestList(FileChange.ValueComparer); | ||
|
||
// Every list s/be equal to itself | ||
listOne.Equals(listOne).Should().Be(true); | ||
|
||
// As initialized, these objects are different, due | ||
// to a unique GUID property on each list | ||
listOne.Equals(listTwo).Should().Be(false); | ||
|
||
// Make the two lists equivalent, by value | ||
listTwo[2].SetProperty(DIFFERENTIATING_PROPERTY_NAME, listOne[2].GetProperty<Guid>(DIFFERENTIATING_PROPERTY_NAME)); | ||
listOne.Equals(listTwo).Should().Be(true); | ||
|
||
FileChange toSwap = listTwo[0]; | ||
listTwo[0] = listTwo[1]; | ||
listTwo[1] = toSwap; | ||
|
||
// We have reordered two objects that are themselves identical. | ||
// by value. The comparison should still succeed. | ||
listOne.Equals(listTwo).Should().Be(true); | ||
} | ||
|
||
private const string DIFFERENTIATING_PROPERTY_NAME = nameof(DIFFERENTIATING_PROPERTY_NAME); | ||
|
||
private OrderSensitiveValueComparisonList<FileChange> CreateTestList(IEqualityComparer<FileChange> equalityComparer) | ||
{ | ||
// Test list. First two elements are identical. The third element is unique. | ||
var fileChangeOne = new FileChange(); | ||
var fileChangeTwo = new FileChange(); | ||
|
||
var fileChangeThree = new FileChange(); | ||
Guid differentiatingProperty = Guid.NewGuid(); | ||
fileChangeThree.SetProperty(DIFFERENTIATING_PROPERTY_NAME, differentiatingProperty); | ||
|
||
var list = new OrderSensitiveValueComparisonList<FileChange>(equalityComparer); | ||
list.AddRange(new[] { fileChangeOne, fileChangeTwo, fileChangeThree }); | ||
|
||
return list; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The results matcher now only persists files that are observed via results. I will fix this later, the results matcher should retain all files from current run. Implementation as it stands exists to ensure we flow property bags associated with baseline files, when appropriate. It's easier for me to just fix the gap then to explain much more. :)
#1235