This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for RegistrationComparer core logic (#730)
Progress on NuGet/NuGetGallery#7741
- Loading branch information
1 parent
69bdcc7
commit 59fd6f2
Showing
7 changed files
with
318 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/> | ||
</startup> | ||
</configuration> |
206 changes: 206 additions & 0 deletions
206
tests/NuGet.Jobs.RegistrationComparer.Tests/JsonComparerFacts.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,206 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
using ValueNormalizer = System.Collections.Generic.KeyValuePair<NuGet.Jobs.RegistrationComparer.ShouldNormalizeByPath, NuGet.Jobs.RegistrationComparer.NormalizeToken>; | ||
using ArrayNormalizer = System.Collections.Generic.KeyValuePair<NuGet.Jobs.RegistrationComparer.ShouldNormalizeByArray, System.Comparison<Newtonsoft.Json.Linq.JToken>>; | ||
|
||
namespace NuGet.Jobs.RegistrationComparer | ||
{ | ||
public class JsonComparerFacts | ||
{ | ||
[Fact] | ||
public void AcceptsSameObject() | ||
{ | ||
var a = Json(new { array = new[] { 0, 1, 3 } }); | ||
var b = Json(new { array = new[] { 0, 1, 3 } }); | ||
|
||
Target.Compare(a, b, Context); | ||
|
||
Assert.NotSame(a, b); | ||
} | ||
|
||
[Fact] | ||
public void DetectsMissingItemInArray() | ||
{ | ||
var a = Json(new { array = new[] { 0, 1, 3 } }); | ||
var b = Json(new { array = new[] { 0, 1, 2, 3 } }); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => Target.Compare(a, b, Context)); | ||
|
||
Assert.Contains("The JSON array item count is different.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void DetectsDifferentItemsInArray() | ||
{ | ||
var a = Json(new { array = new[] { 0, 1, 3 } }); | ||
var b = Json(new { array = new[] { 0, 1, 2 } }); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => Target.Compare(a, b, Context)); | ||
|
||
Assert.Contains("The value of the JSON scalar is different.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void DetectsOutOfOrderItemsInArray() | ||
{ | ||
var a = Json(new { array = new[] { 0, 2, 1 } }); | ||
var b = Json(new { array = new[] { 0, 1, 2 } }); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => Target.Compare(a, b, Context)); | ||
|
||
Assert.Contains("The value of the JSON scalar is different.", ex.Message); | ||
} | ||
|
||
[Theory] | ||
[InlineData("2", 2)] | ||
[InlineData(true, 1)] | ||
[InlineData("false", false)] | ||
[InlineData("null", null)] | ||
[InlineData(0, null)] | ||
public void DetectsDifferentTypesInArray(object valueA, object valueB) | ||
{ | ||
var a = Json(new { array = new object[] { 0, 1, valueA } }); | ||
var b = Json(new { array = new object[] { 0, 1, valueB } }); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => Target.Compare(a, b, Context)); | ||
|
||
Assert.Contains("The type of the JSON value is different.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void DetectsDifferentProperties() | ||
{ | ||
var a = Json(new { arrayA = new[] { 0, 1, 2 } }); | ||
var b = Json(new { arrayB = new[] { 0, 1, 2 } }); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => Target.Compare(a, b, Context)); | ||
|
||
Assert.Contains("The JSON object property names are disjoint.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void DetectsOutOfOrderProperties() | ||
{ | ||
var a = Json(new { inner = new { a = "a", b = "b" } }); | ||
var b = Json(new { inner = new { b = "b", a = "a" } }); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => Target.Compare(a, b, Context)); | ||
|
||
Assert.Contains("The JSON object property names are in a different order.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void DetectsDifferentCaseOfPropertyNames() | ||
{ | ||
var a = Json(new { array = new[] { 0, 1, 2 } }); | ||
var b = Json(new { Array = new[] { 0, 1, 2 } }); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => Target.Compare(a, b, Context)); | ||
|
||
Assert.Contains("The JSON object property names are disjoint.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void DetectsExtraProperty() | ||
{ | ||
var a = Json(new { array = new[] { 0, 1, 2 } }); | ||
var b = Json(new { array = new[] { 0, 1, 2 }, somethingElse = 2 }); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => Target.Compare(a, b, Context)); | ||
|
||
Assert.Contains("The JSON object property names are disjoint.", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void AllowsValueToBeNormalized() | ||
{ | ||
var normalizers = new Normalizers( | ||
scalarNormalizers: new List<ValueNormalizer> | ||
{ | ||
new ValueNormalizer( | ||
(path) => path == "random", | ||
(token, isLeft, context) => "999"), | ||
}, | ||
unsortedObjects: new List<ShouldNormalizeByPath>(), | ||
unsortedArrays: new List<ArrayNormalizer>()); | ||
var a = Json(new { array = new[] { 0, 1, 2 }, random = 23 }); | ||
var b = Json(new { array = new[] { 0, 1, 2 }, random = 42 }); | ||
|
||
Target.Compare(a, b, GetContext(normalizers)); | ||
} | ||
|
||
[Fact] | ||
public void AllowsObjectPropertyOrderToBeIgnored() | ||
{ | ||
var normalizers = new Normalizers( | ||
scalarNormalizers: new List<ValueNormalizer>(), | ||
unsortedObjects: new List<ShouldNormalizeByPath> | ||
{ | ||
(path) => path == "inner", | ||
}, | ||
unsortedArrays: new List<ArrayNormalizer>()); | ||
var a = Json(new { inner = new { a = "a", b = "b" } }); | ||
var b = Json(new { inner = new { b = "b", a = "a" } }); | ||
|
||
Target.Compare(a, b, GetContext(normalizers)); | ||
} | ||
|
||
[Fact] | ||
public void AllowsArrayItemOrderToBeIgnored() | ||
{ | ||
var normalizers = new Normalizers( | ||
scalarNormalizers: new List<ValueNormalizer>(), | ||
unsortedObjects: new List<ShouldNormalizeByPath>(), | ||
unsortedArrays: new List<ArrayNormalizer> | ||
{ | ||
new ArrayNormalizer( | ||
array => array.Path == "array", | ||
(x, y) => Comparer<JToken>.Default.Compare(x, y)), | ||
}); | ||
var a = Json(new { array = new[] { 0, 2, 1 } }); | ||
var b = Json(new { array = new[] { 0, 1, 2 } }); | ||
|
||
Target.Compare(a, b, GetContext(normalizers)); | ||
} | ||
|
||
public JsonComparerFacts() | ||
{ | ||
Context = GetContext(); | ||
Target = new JsonComparer(); | ||
} | ||
|
||
private ComparisonContext GetContext(Normalizers normalizers) | ||
{ | ||
return new ComparisonContext( | ||
"NuGet.Versioning", | ||
"https://example/api/a", | ||
"https://example/api/b", | ||
"https://example/api/a/index.json", | ||
"https://example/api/b/index.json", | ||
normalizers); | ||
} | ||
|
||
private ComparisonContext GetContext() | ||
{ | ||
return GetContext( | ||
new Normalizers( | ||
new List<ValueNormalizer>(), | ||
new List<ShouldNormalizeByPath>(), | ||
new List<ArrayNormalizer>())); | ||
} | ||
|
||
public ComparisonContext Context { get; } | ||
public JsonComparer Target { get; } | ||
|
||
private JToken Json<T>(T obj) | ||
{ | ||
return JsonConvert.DeserializeObject<JToken>(JsonConvert.SerializeObject(obj)); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
tests/NuGet.Jobs.RegistrationComparer.Tests/NuGet.Jobs.RegistrationComparer.Tests.csproj
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,79 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{A0E0698A-1161-4DEA-81A9-06D30FB16538}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>NuGet.Jobs.RegistrationComparer</RootNamespace> | ||
<AssemblyName>NuGet.Jobs.RegistrationComparer.Tests</AssemblyName> | ||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<Deterministic>true</Deterministic> | ||
<TargetFrameworkProfile /> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<StartupObject /> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Moq"> | ||
<Version>4.10.1</Version> | ||
</PackageReference> | ||
<PackageReference Include="xunit"> | ||
<Version>2.4.1</Version> | ||
</PackageReference> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<Version>2.4.1</Version> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="JsonComparerFacts.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\NuGet.Jobs.RegistrationComparer\NuGet.Jobs.RegistrationComparer.csproj"> | ||
<Project>{4ce6c864-db4d-4262-a2dd-80bb932f6e8c}</Project> | ||
<Name>NuGet.Jobs.RegistrationComparer</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<PropertyGroup> | ||
<SignPath>..\..\build</SignPath> | ||
<SignPath Condition="'$(BUILD_SOURCESDIRECTORY)' != ''">$(BUILD_SOURCESDIRECTORY)\build</SignPath> | ||
<SignPath Condition="'$(NuGetBuildPath)' != ''">$(NuGetBuildPath)</SignPath> | ||
</PropertyGroup> | ||
<Import Project="$(SignPath)\sign.targets" Condition="Exists('$(SignPath)\sign.targets')" /> | ||
</Project> |
9 changes: 9 additions & 0 deletions
9
tests/NuGet.Jobs.RegistrationComparer.Tests/Properties/AssemblyInfo.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,9 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
[assembly: AssemblyTitle("NuGet.Jobs.RegistrationComparer.Tests")] | ||
[assembly: ComVisible(false)] | ||
[assembly: Guid("a83b83d0-4f95-4e1e-bb46-8c4ce547bd67")] |