mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-23 14:46:26 +00:00
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:
parent
c6d5ebec2f
commit
a70bc5fc09
1
.gitignore
vendored
1
.gitignore
vendored
@ -12,6 +12,7 @@ TestResults
|
||||
*.suo
|
||||
*.user
|
||||
*.sln.docstates
|
||||
.vs/
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
|
@ -32,19 +32,20 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Moq, Version=4.2.1510.2205, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<Reference Include="Moq, Version=4.7.145.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Moq.4.7.145\lib\net45\Moq.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="nunit.framework, Version=3.0.5797.27534, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NUnit.3.0.0\lib\net45\nunit.framework.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</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 Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
@ -53,6 +54,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="SpotifyUriTest.cs" />
|
||||
<Compile Include="TestClass.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
@ -67,6 +69,9 @@
|
||||
<None Include="fixtures\public-user.json" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.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.
|
||||
@ -76,4 +81,4 @@
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
</Project>
|
59
SpotifyAPI.Tests/SpotifyUriTest.cs
Normal file
59
SpotifyAPI.Tests/SpotifyUriTest.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Moq" version="4.2.1510.2205" targetFramework="net45" />
|
||||
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
|
||||
<package id="NUnit" version="3.0.0" targetFramework="net45" />
|
||||
<package id="Castle.Core" version="4.2.1" targetFramework="net45" />
|
||||
<package id="Moq" version="4.7.145" targetFramework="net45" />
|
||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
|
||||
<package id="NUnit" version="3.8.1" targetFramework="net45" />
|
||||
</packages>
|
12
SpotifyAPI/Local/Enums/UriType.cs
Normal file
12
SpotifyAPI/Local/Enums/UriType.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace SpotifyAPI.Local.Enums
|
||||
{
|
||||
public enum UriType
|
||||
{
|
||||
none,
|
||||
track,
|
||||
album,
|
||||
artist,
|
||||
playlist,
|
||||
user
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using SpotifyAPI.Local.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -8,15 +9,29 @@ namespace SpotifyAPI.Local.Models
|
||||
{
|
||||
public class SpotifyUri
|
||||
{
|
||||
public string Base { get; internal set; }
|
||||
public string Type { get; internal set; }
|
||||
public string Id { get; internal set; }
|
||||
internal Dictionary<UriType, string> _properties = new Dictionary<UriType, string>();
|
||||
|
||||
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;
|
||||
Type = uriType;
|
||||
Id = uriId;
|
||||
_properties = properties;
|
||||
}
|
||||
|
||||
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)
|
||||
@ -24,16 +39,26 @@ namespace SpotifyAPI.Local.Models
|
||||
if (string.IsNullOrEmpty(uri))
|
||||
throw new ArgumentNullException("Uri");
|
||||
|
||||
UriType uriType = UriType.none;
|
||||
string[] props = uri.Split(':');
|
||||
if (props.Length != 3)
|
||||
if (props.Length < 3 || !Enum.TryParse(props[1], out uriType))
|
||||
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()
|
||||
{
|
||||
return $"{Base}:{Type}:{Id}";
|
||||
return $"{Base}:{string.Join(":", _properties.SelectMany(x => new string[] { x.Key.ToString(), x.Value }))}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,9 +37,8 @@
|
||||
<NoWarn>1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
@ -55,6 +54,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Local\Enums\UriType.cs" />
|
||||
<Compile Include="Local\ExtendedWebClient.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
|
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
|
||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
|
||||
</packages>
|
Loading…
Reference in New Issue
Block a user