-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Files array results matching (#1234)
* WIP preliminary results matching * Restore results matching for files array * Add back autogenerated (unused) namespace directive
- Loading branch information
1 parent
fdce2e5
commit 82a5511
Showing
17 changed files
with
608 additions
and
175 deletions.
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.