Merge pull request #7548 from 1337joe/comparer-null-fix

This commit is contained in:
Cody Robibero 2022-04-04 06:30:24 -06:00 committed by GitHub
commit 72aca15191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 109 additions and 0 deletions

View File

@ -34,6 +34,11 @@ namespace Emby.Server.Implementations.Sorting
throw new ArgumentNullException(nameof(y));
}
if (!x.IndexNumber.HasValue && !y.IndexNumber.HasValue)
{
return 0;
}
if (!x.IndexNumber.HasValue)
{
return -1;

View File

@ -34,6 +34,11 @@ namespace Emby.Server.Implementations.Sorting
throw new ArgumentNullException(nameof(y));
}
if (!x.ParentIndexNumber.HasValue && !y.ParentIndexNumber.HasValue)
{
return 0;
}
if (!x.ParentIndexNumber.HasValue)
{
return -1;

View File

@ -0,0 +1,49 @@
using System;
using Emby.Server.Implementations.Sorting;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Sorting;
using Xunit;
namespace Jellyfin.Server.Implementations.Tests.Sorting;
public class IndexNumberComparerTests
{
private readonly IBaseItemComparer _cmp = new IndexNumberComparer();
private static TheoryData<BaseItem?, BaseItem?> Compare_GivenNull_ThrowsArgumentNullException_TestData()
=> new()
{
{ null, new Audio() },
{ new Audio(), null }
};
[Theory]
[MemberData(nameof(Compare_GivenNull_ThrowsArgumentNullException_TestData))]
public void Compare_GivenNull_ThrowsArgumentNullException(BaseItem? x, BaseItem? y)
{
Assert.Throws<ArgumentNullException>(() => _cmp.Compare(x, y));
}
[Theory]
[InlineData(null, null, 0)]
[InlineData(0, null, 1)]
[InlineData(null, 0, -1)]
[InlineData(1, 1, 0)]
[InlineData(0, 1, -1)]
[InlineData(1, 0, 1)]
public void Compare_ValidIndices_SortsExpected(int? index1, int? index2, int expected)
{
BaseItem x = new Audio
{
IndexNumber = index1
};
BaseItem y = new Audio
{
IndexNumber = index2
};
Assert.Equal(expected, _cmp.Compare(x, y));
Assert.Equal(-expected, _cmp.Compare(y, x));
}
}

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using Emby.Server.Implementations.Sorting;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Sorting;
using Xunit;
namespace Jellyfin.Server.Implementations.Tests.Sorting;
public class ParentIndexNumberComparerTests
{
private readonly IBaseItemComparer _cmp = new ParentIndexNumberComparer();
private static TheoryData<BaseItem?, BaseItem?> Compare_GivenNull_ThrowsArgumentNullException_TestData()
=> new()
{
{ null, new Audio() },
{ new Audio(), null }
};
[Theory]
[MemberData(nameof(Compare_GivenNull_ThrowsArgumentNullException_TestData))]
public void Compare_GivenNull_ThrowsArgumentNullException(BaseItem? x, BaseItem? y)
{
Assert.Throws<ArgumentNullException>(() => _cmp.Compare(x, y));
}
[Theory]
[InlineData(null, null, 0)]
[InlineData(0, null, 1)]
[InlineData(null, 0, -1)]
[InlineData(1, 1, 0)]
[InlineData(0, 1, -1)]
[InlineData(1, 0, 1)]
public void Compare_ValidIndices_SortsExpected(int? parentIndex1, int? parentIndex2, int expected)
{
BaseItem x = new Audio
{
ParentIndexNumber = parentIndex1
};
BaseItem y = new Audio
{
ParentIndexNumber = parentIndex2
};
Assert.Equal(expected, _cmp.Compare(x, y));
Assert.Equal(-expected, _cmp.Compare(y, x));
}
}