Refactored SpotifyUri Parsing (#194)

* refactored SpotifyUri parsing

* unit tests for SpotifyUri class

* added missing file reference in SpotifyAPI.csproj

* updated NuGet packages in SpotifyAPI.Tests and reverted inline TryParse variable declarations in SpotifyUri.cs
This commit is contained in:
Philipp Drebes 2017-11-09 15:36:53 +01:00 committed by Jonas Dellinger
parent c6d5ebec2f
commit a70bc5fc09
8 changed files with 130 additions and 27 deletions

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ TestResults
*.suo *.suo
*.user *.user
*.sln.docstates *.sln.docstates
.vs/
# Build results # Build results
[Dd]ebug/ [Dd]ebug/

View File

@ -32,19 +32,20 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Moq, Version=4.2.1510.2205, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL"> <Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath> <HintPath>..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Moq, Version=4.7.145.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Moq.4.7.145\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
<Reference Include="nunit.framework, Version=3.0.5797.27534, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.0.0\lib\net45\nunit.framework.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private> </Reference>
<Reference Include="nunit.framework, Version=3.8.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.8.1\lib\net45\nunit.framework.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
@ -53,6 +54,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="SpotifyUriTest.cs" />
<Compile Include="TestClass.cs" /> <Compile Include="TestClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
@ -67,6 +69,9 @@
<None Include="fixtures\public-user.json" /> <None Include="fixtures\public-user.json" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" /> <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -0,0 +1,59 @@
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using SpotifyAPI.Local;
using SpotifyAPI.Local.Models;
using SpotifyAPI.Local.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace SpotifyAPI.Tests
{
public class SpotifyUriTest
{
[Test]
public void ShouldThrowArgumentExceptionForInvalidUri()
{
Assert.Throws<ArgumentException>(() => SpotifyUri.Parse("asdafadfgsrsegqejfa"));
}
[Test]
public void ShouldCorrectlyParseTrackUri()
{
string testUri = "spotify:track:3QOruXa2lvqIFvOOa2rYyJ";
SpotifyUri uri = SpotifyUri.Parse(testUri);
Assert.AreEqual(uri.Base, "spotify");
Assert.AreEqual(uri.Type, UriType.track);
Assert.AreEqual(uri.Id, "3QOruXa2lvqIFvOOa2rYyJ");
Assert.AreEqual(uri.ToString(), testUri);
}
[Test]
public void ShouldCorrectlyParsePlaylistUri()
{
string testUri = "spotify:user:spotifycharts:playlist:37i9dQZEVXbMDoHDwVN2tF";
SpotifyUri uri = SpotifyUri.Parse(testUri);
Assert.AreEqual(uri.Base, "spotify");
Assert.AreEqual(uri.Type, UriType.playlist);
Assert.AreEqual(uri.Id, "37i9dQZEVXbMDoHDwVN2tF");
Assert.AreEqual(uri.GetUriPropValue(UriType.user), "spotifycharts");
Assert.AreEqual(uri.ToString(), testUri);
}
[Test]
public void ShouldHandleNotExistingUriProperty()
{
string testUri = "spotify:track:3QOruXa2lvqIFvOOa2rYyJ";
SpotifyUri uri = SpotifyUri.Parse(testUri);
Assert.DoesNotThrow(() => uri.GetUriPropValue(UriType.user));
Assert.IsNull(uri.GetUriPropValue(UriType.user));
}
}
}

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Moq" version="4.2.1510.2205" targetFramework="net45" /> <package id="Castle.Core" version="4.2.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" /> <package id="Moq" version="4.7.145" targetFramework="net45" />
<package id="NUnit" version="3.0.0" targetFramework="net45" /> <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
<package id="NUnit" version="3.8.1" targetFramework="net45" />
</packages> </packages>

View File

@ -0,0 +1,12 @@
namespace SpotifyAPI.Local.Enums
{
public enum UriType
{
none,
track,
album,
artist,
playlist,
user
}
}

View File

@ -1,4 +1,5 @@
using System; using SpotifyAPI.Local.Enums;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -8,15 +9,29 @@ namespace SpotifyAPI.Local.Models
{ {
public class SpotifyUri public class SpotifyUri
{ {
public string Base { get; internal set; } internal Dictionary<UriType, string> _properties = new Dictionary<UriType, string>();
public string Type { get; internal set; }
public string Id { get; internal set; }
public SpotifyUri(string uriBase, string uriType, string uriId) public string Base { get; internal set; }
public UriType Type => _properties?.LastOrDefault().Key ?? UriType.none;
public string Id => _properties?.LastOrDefault().Value;
public SpotifyUri(string uriBase, Dictionary<UriType, string> properties)
{ {
Base = uriBase; Base = uriBase;
Type = uriType; _properties = properties;
Id = uriId; }
public SpotifyUri(string uriBase, UriType uriType, string uriId)
{
Base = uriBase;
_properties.Add(uriType, uriId);
}
public string GetUriPropValue(UriType type)
{
if (!_properties.ContainsKey(type))
return null;
return _properties[type];
} }
public static SpotifyUri Parse(string uri) public static SpotifyUri Parse(string uri)
@ -24,16 +39,26 @@ namespace SpotifyAPI.Local.Models
if (string.IsNullOrEmpty(uri)) if (string.IsNullOrEmpty(uri))
throw new ArgumentNullException("Uri"); throw new ArgumentNullException("Uri");
UriType uriType = UriType.none;
string[] props = uri.Split(':'); string[] props = uri.Split(':');
if (props.Length != 3) if (props.Length < 3 || !Enum.TryParse(props[1], out uriType))
throw new ArgumentException("Unexpected Uri"); throw new ArgumentException("Unexpected Uri");
return new SpotifyUri(props[0], props[1], props[2]); Dictionary<UriType, string> properties = new Dictionary<UriType, string> { { uriType, props[2] } };
for (int index = 3; index < props.Length; index += 2)
{
UriType type = UriType.none;
if (Enum.TryParse(props[index], out type))
properties.Add(type, props[index + 1]);
}
return new SpotifyUri(props[0], properties);
} }
public override string ToString() public override string ToString()
{ {
return $"{Base}:{Type}:{Id}"; return $"{Base}:{string.Join(":", _properties.SelectMany(x => new string[] { x.Key.ToString(), x.Value }))}";
} }
} }
} }

View File

@ -37,9 +37,8 @@
<NoWarn>1591</NoWarn> <NoWarn>1591</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" /> <Reference Include="System.ComponentModel.DataAnnotations" />
@ -55,6 +54,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Local\Enums\UriType.cs" />
<Compile Include="Local\ExtendedWebClient.cs"> <Compile Include="Local\ExtendedWebClient.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" /> <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
</packages> </packages>