Add SplittableProgress and tests

This commit is contained in:
J2ghz 2016-11-16 10:50:08 +01:00
parent 269563de63
commit 6391acf4e2
9 changed files with 265 additions and 0 deletions

View File

@ -0,0 +1,30 @@
using System.Resources;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Trakt.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Trakt.Test")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,59 @@
namespace Trakt.Test
{
using System;
using Trakt.Helpers;
using Xunit;
public class SplittableProgressTests
{
[Fact]
public void AsIProgress()
{
IProgress<double> mainProg = new SplittableProgress(d => Assert.Equal(100, d));
mainProg.Report(100);
}
[Fact]
public void ExtensionConversion()
{
IProgress<double> mainProg = new Progress<double>(d => Assert.Equal(100, d));
mainProg.ToSplittableProgress().Report(100);
}
[Fact]
public void ExtensionSplit()
{
IProgress<double> mainProg = new Progress<double>(d => Assert.Equal(25, d));
mainProg.Split(4).Report(100);
}
[Fact]
public void Split()
{
var firstSplit = 3;
var expected = 100d / firstSplit;
IProgress<double> mainProg = new Progress<double>(d => Assert.Equal(expected, d));
ISplittableProgress<double> mainProgS = new SplittableProgress(mainProg.Report);
var childProgS = mainProgS.Split(firstSplit);
childProgS.Report(100);
}
[Fact]
public void SplitTwice()
{
var firstSplit = 3;
var secondSplit = 5;
var expected = 100d / firstSplit / secondSplit;
IProgress<double> mainProg = new Progress<double>(d => Assert.Equal(expected, d));
ISplittableProgress<double> mainProgS = new SplittableProgress(mainProg.Report);
var grandchildProgS = mainProgS.Split(firstSplit).Split(secondSplit);
grandchildProgS.Report(100);
}
}
}

View File

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\xunit.runner.visualstudio.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.runner.visualstudio.props')" />
<Import Project="..\packages\xunit.core.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.core.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E26917D8-838E-40CF-BE87-2911B3096458}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Trakt.Test</RootNamespace>
<AssemblyName>Trakt.Test</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile7</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</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>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SplittableProgressTests.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="MediaBrowser.Common, Version=3.1.6161.23127, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MediaBrowser.Common.3.0.691\lib\portable-net45+win8+wpa81\MediaBrowser.Common.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MediaBrowser.Controller, Version=3.1.6161.23125, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MediaBrowser.Server.Core.3.0.691\lib\portable-net45+win8+wpa81\MediaBrowser.Controller.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MediaBrowser.Model, Version=3.1.6161.23126, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MediaBrowser.Common.3.0.691\lib\portable-net45+win8+wpa81\MediaBrowser.Model.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.assert, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.assert.2.1.0\lib\portable-net45+win8+wp8+wpa81\xunit.assert.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.core.2.1.0\lib\portable-net45+win8+wp8+wpa81\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.execution.dotnet, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.execution.2.1.0\lib\portable-net45+win8+wp8+wpa81\xunit.execution.dotnet.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Trakt\Trakt.csproj">
<Project>{7ffc306b-2680-49c7-8be0-6358b2a8a409}</Project>
<Name>Trakt</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\xunit.core.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.core.props'))" />
<Error Condition="!Exists('..\packages\xunit.runner.visualstudio.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.runner.visualstudio.2.1.0\build\portable-net45+win8+wp8+wpa81\xunit.runner.visualstudio.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MediaBrowser.Common" version="3.0.691" targetFramework="portable45-net45+win8" />
<package id="MediaBrowser.Server.Core" version="3.0.691" targetFramework="portable45-net45+win8" />
<package id="xunit" version="2.1.0" targetFramework="portable45-net45+win8" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="portable45-net45+win8" />
<package id="xunit.assert" version="2.1.0" targetFramework="portable45-net45+win8" />
<package id="xunit.core" version="2.1.0" targetFramework="portable45-net45+win8" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="portable45-net45+win8" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="portable45-net45+win8" />
<package id="xunit.runner.console" version="2.1.0" targetFramework="portable45-net45+win8" />
<package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="portable45-net45+win8" />
</packages>

View File

@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Trakt", "Trakt\Trakt.csproj", "{7FFC306B-2680-49C7-8BE0-6358B2A8A409}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Trakt.Test", "Trakt.Test\Trakt.Test.csproj", "{E26917D8-838E-40CF-BE87-2911B3096458}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -24,6 +26,14 @@ Global
{7FFC306B-2680-49C7-8BE0-6358B2A8A409}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FFC306B-2680-49C7-8BE0-6358B2A8A409}.Release|Any CPU.Build.0 = Release|Any CPU
{7FFC306B-2680-49C7-8BE0-6358B2A8A409}.Release|x86.ActiveCfg = Release|Any CPU
{E26917D8-838E-40CF-BE87-2911B3096458}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E26917D8-838E-40CF-BE87-2911B3096458}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E26917D8-838E-40CF-BE87-2911B3096458}.Debug|x86.ActiveCfg = Debug|Any CPU
{E26917D8-838E-40CF-BE87-2911B3096458}.Debug|x86.Build.0 = Debug|Any CPU
{E26917D8-838E-40CF-BE87-2911B3096458}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E26917D8-838E-40CF-BE87-2911B3096458}.Release|Any CPU.Build.0 = Release|Any CPU
{E26917D8-838E-40CF-BE87-2911B3096458}.Release|x86.ActiveCfg = Release|Any CPU
{E26917D8-838E-40CF-BE87-2911B3096458}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -8,6 +8,8 @@ using Trakt.Api.DataContracts.Users.Collection;
namespace Trakt
{
using Trakt.Helpers;
public static class Extensions
{
// Trakt.tv uses Unix timestamps, which are seconds past epoch.
@ -189,6 +191,18 @@ namespace Trakt
return chunks;
}
public static ISplittableProgress<double> Split(this IProgress<double> parent, int parts)
{
var current = parent.ToSplittableProgress();
return current.Split(parts);
}
public static ISplittableProgress<double> ToSplittableProgress(this IProgress<double> progress)
{
var splittable = new SplittableProgress(progress.Report);
return splittable;
}
public enum TraktAudio
{
lpcm,
@ -204,5 +218,6 @@ namespace Trakt
dolby_digital_plus,
dolby_truehd
}
}
}

View File

@ -0,0 +1,9 @@
namespace Trakt.Helpers
{
using System;
public interface ISplittableProgress<T> : IProgress<T>
{
ISplittableProgress<T> Split(int parts);
}
}

View File

@ -0,0 +1,26 @@
namespace Trakt.Helpers
{
using System;
// Can't be generic, because it's impossible to do arithmetics on generics
public class SplittableProgress : Progress<double>, ISplittableProgress<double>
{
public SplittableProgress(Action<double> handler)
: base(handler)
{
}
public double Progress { get; private set; }
ISplittableProgress<double> ISplittableProgress<double>.Split(int parts)
{
var child = new SplittableProgress(
d =>
{
Progress += d / parts;
OnReport(Progress);
});
return child;
}
}
}

View File

@ -97,7 +97,9 @@
<Compile Include="Configuration\PluginConfiguration.cs" />
<Compile Include="Enums.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Helpers\ISplittableProgress.cs" />
<Compile Include="Helpers\LibraryManagerEventsHelper.cs" />
<Compile Include="Helpers\SplittableProgress.cs" />
<Compile Include="Helpers\UserDataManagerEventsHelper.cs" />
<Compile Include="Helpers\UserHelpers.cs" />
<Compile Include="Model\TraktUser.cs" />