replaced equality api

This commit is contained in:
andy 2021-10-01 22:01:28 +01:00
parent 2136a7953b
commit 918ebbb238
14 changed files with 530 additions and 552 deletions

162
Selector.Tests/Equal.cs Normal file
View File

@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using Xunit;
using Moq;
using FluentAssertions;
using SpotifyAPI.Web;
using Selector;
namespace Selector.Tests
{
public class EqualTests
{
public static IEnumerable<object[]> TrackData =>
new List<object[]>
{
// SAME
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("1", "1", "1"),
true
},
// WRONG ALBUM
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("1", "2", "1"),
false
},
// WRONG TRACK
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("2", "1", "1"),
false
},
// WRONG ARTIST
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("1", "1", "2"),
false
},
// WRONG TRACK/ARTIST
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("2", "1", "2"),
false
},
// RIGHT MULTIPLE ARTISTS
new object[] {
Helper.FullTrack("1", "1", new List<string>() { "1", "2" }),
Helper.FullTrack("1", "1", new List<string>() { "1", "2" }),
true
},
// WRONG ARTISTS
new object[] {
Helper.FullTrack("1", "1", new List<string>() { "1", "2" }),
Helper.FullTrack("1", "1", new List<string>() { "1" }),
false
}
};
[Theory]
[MemberData(nameof(TrackData))]
public void TrackEquality(FullTrack track1, FullTrack track2, bool shouldEqual)
{
var eq = Equal.String();
eq.IsEqual<FullTrack>(track1, track2).Should().Be(shouldEqual);
}
public static IEnumerable<object[]> AlbumData =>
new List<object[]>
{
// SAME
new object[] {
Helper.SimpleAlbum("1", "1"),
Helper.SimpleAlbum("1", "1"),
true
},
// DIFFERENT NAME
new object[] {
Helper.SimpleAlbum("1", "1"),
Helper.SimpleAlbum("2", "1"),
false
},
// DIFFERENT ARTIST
new object[] {
Helper.SimpleAlbum("1", "1"),
Helper.SimpleAlbum("1", "2"),
false
},
// SAME ARTISTS
new object[] {
Helper.SimpleAlbum("1", new List<string>() { "1", "2" }),
Helper.SimpleAlbum("1", new List<string>() { "1", "2" }),
true
},
// DIFFERENT ARTISTS
new object[] {
Helper.SimpleAlbum("1", new List<string>() { "1", "2" }),
Helper.SimpleAlbum("1", new List<string>() { "1" }),
false
},
};
[Theory]
[MemberData(nameof(AlbumData))]
public void AlbumEquality(SimpleAlbum album1, SimpleAlbum album2, bool shouldEqual)
{
var eq = Equal.String();
eq.IsEqual<SimpleAlbum>(album1, album2).Should().Be(shouldEqual);
}
public static IEnumerable<object[]> ArtistData =>
new List<object[]>
{
// SAME
new object[] {
Helper.SimpleArtist("1"),
Helper.SimpleArtist("1"),
true
},
// DIFFERENT
new object[] {
Helper.SimpleArtist("1"),
Helper.SimpleArtist("2"),
false
}
};
[Theory]
[MemberData(nameof(ArtistData))]
public void ArtistEquality(SimpleArtist artist1, SimpleArtist artist2, bool shouldEqual)
{
var eq = Equal.String();
eq.IsEqual<SimpleArtist>(artist1, artist2).Should().Be(shouldEqual);
}
public static IEnumerable<object[]> EpisodeData =>
new List<object[]>
{
// SAME
new object[] {
Helper.FullEpisode("1"),
Helper.FullEpisode("1"),
true
},
// DIFFERENT
new object[] {
Helper.FullEpisode("1"),
Helper.FullEpisode("2"),
false
}
};
[Theory]
[MemberData(nameof(EpisodeData))]
public void EpisodeEquality(FullEpisode episode1, FullEpisode episode2, bool shouldEqual)
{
var eq = Equal.String();
eq.IsEqual<FullEpisode>(episode1, episode2).Should().Be(shouldEqual);
}
}
}

View File

@ -1,339 +0,0 @@
using System;
using System.Collections.Generic;
using Xunit;
using Moq;
using FluentAssertions;
using SpotifyAPI.Web;
using Selector;
namespace Selector.Tests
{
public class UriEqualityTests
{
public static IEnumerable<object[]> TrackData =>
new List<object[]>
{
// SAME
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("1", "1", "1"),
true,
true
},
// WRONG ALBUM BUT IGNORING
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("1", "2", "1"),
false,
true
},
// WRONG TRACK
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("2", "1", "1"),
true,
false
},
// WRONG ARTIST
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("1", "1", "2"),
true,
false
},
// WRONG TRACK/ARTIST
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("2", "1", "2"),
true,
false
},
// RIGHT MULTIPLE ARTISTS
new object[] {
Helper.FullTrack("1", "1", new List<string>() { "1", "2" }),
Helper.FullTrack("1", "1", new List<string>() { "1", "2" }),
true,
true
},
// WRONG ARTISTS
new object[] {
Helper.FullTrack("1", "1", new List<string>() { "1", "2" }),
Helper.FullTrack("1", "1", new List<string>() { "1" }),
true,
false
}
};
[Theory]
[MemberData(nameof(TrackData))]
public void TrackEquality(FullTrack track1, FullTrack track2, bool includingAlbum, bool shouldEqual)
{
var eq = new UriEquality();
eq.Track(track1, track2, includingAlbum: includingAlbum).Should().Be(shouldEqual);
}
public static IEnumerable<object[]> AlbumData =>
new List<object[]>
{
// SAME
new object[] {
Helper.SimpleAlbum("1", "1"),
Helper.SimpleAlbum("1", "1"),
true
},
// DIFFERENT NAME
new object[] {
Helper.SimpleAlbum("1", "1"),
Helper.SimpleAlbum("2", "1"),
false
},
// DIFFERENT ARTIST
new object[] {
Helper.SimpleAlbum("1", "1"),
Helper.SimpleAlbum("1", "2"),
false
},
// SAME ARTISTS
new object[] {
Helper.SimpleAlbum("1", new List<string>() { "1", "2" }),
Helper.SimpleAlbum("1", new List<string>() { "1", "2" }),
true
},
// DIFFERENT ARTISTS
new object[] {
Helper.SimpleAlbum("1", new List<string>() { "1", "2" }),
Helper.SimpleAlbum("1", new List<string>() { "1" }),
false
},
};
[Theory]
[MemberData(nameof(AlbumData))]
public void AlbumEquality(SimpleAlbum album1, SimpleAlbum album2, bool shouldEqual)
{
var eq = new UriEquality();
eq.Album(album1, album2).Should().Be(shouldEqual);
}
public static IEnumerable<object[]> ArtistData =>
new List<object[]>
{
// SAME
new object[] {
Helper.SimpleArtist("1"),
Helper.SimpleArtist("1"),
true
},
// DIFFERENT
new object[] {
Helper.SimpleArtist("1"),
Helper.SimpleArtist("2"),
false
}
};
[Theory]
[MemberData(nameof(ArtistData))]
public void ArtistEquality(SimpleArtist artist1, SimpleArtist artist2, bool shouldEqual)
{
var eq = new UriEquality();
eq.Artist(artist1, artist2).Should().Be(shouldEqual);
}
public static IEnumerable<object[]> EpisodeData =>
new List<object[]>
{
// SAME
new object[] {
Helper.FullEpisode("1"),
Helper.FullEpisode("1"),
true
},
// DIFFERENT
new object[] {
Helper.FullEpisode("1"),
Helper.FullEpisode("2"),
false
}
};
[Theory]
[MemberData(nameof(EpisodeData))]
public void EpisodeEquality(FullEpisode episode1, FullEpisode episode2, bool shouldEqual)
{
var eq = new UriEquality();
eq.Episode(episode1, episode2).Should().Be(shouldEqual);
}
}
public class StringEqualityTests
{
public static IEnumerable<object[]> TrackData =>
new List<object[]>
{
// SAME
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("1", "1", "1"),
true,
true
},
// WRONG ALBUM BUT IGNORING
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("1", "2", "1"),
false,
true
},
// WRONG TRACK
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("2", "1", "1"),
true,
false
},
// WRONG ARTIST
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("1", "1", "2"),
true,
false
},
// WRONG TRACK/ARTIST
new object[] {
Helper.FullTrack("1", "1", "1"),
Helper.FullTrack("2", "1", "2"),
true,
false
},
// RIGHT MULTIPLE ARTISTS
new object[] {
Helper.FullTrack("1", "1", new List<string>() { "1", "2" }),
Helper.FullTrack("1", "1", new List<string>() { "1", "2" }),
true,
true
},
// WRONG ARTISTS
new object[] {
Helper.FullTrack("1", "1", new List<string>() { "1", "2" }),
Helper.FullTrack("1", "1", new List<string>() { "1" }),
true,
false
}
};
[Theory]
[MemberData(nameof(TrackData))]
public void TrackEquality(FullTrack track1, FullTrack track2, bool includingAlbum, bool shouldEqual)
{
var eq = new StringEquality();
eq.Track(track1, track2, includingAlbum: includingAlbum).Should().Be(shouldEqual);
}
public static IEnumerable<object[]> AlbumData =>
new List<object[]>
{
// SAME
new object[] {
Helper.SimpleAlbum("1", "1"),
Helper.SimpleAlbum("1", "1"),
true
},
// DIFFERENT NAME
new object[] {
Helper.SimpleAlbum("1", "1"),
Helper.SimpleAlbum("2", "1"),
false
},
// DIFFERENT ARTIST
new object[] {
Helper.SimpleAlbum("1", "1"),
Helper.SimpleAlbum("1", "2"),
false
},
// SAME ARTISTS
new object[] {
Helper.SimpleAlbum("1", new List<string>() { "1", "2" }),
Helper.SimpleAlbum("1", new List<string>() { "1", "2" }),
true
},
// DIFFERENT ARTISTS
new object[] {
Helper.SimpleAlbum("1", new List<string>() { "1", "2" }),
Helper.SimpleAlbum("1", new List<string>() { "1" }),
false
},
};
[Theory]
[MemberData(nameof(AlbumData))]
public void AlbumEquality(SimpleAlbum album1, SimpleAlbum album2, bool shouldEqual)
{
var eq = new StringEquality();
eq.Album(album1, album2).Should().Be(shouldEqual);
}
public static IEnumerable<object[]> ArtistData =>
new List<object[]>
{
// SAME
new object[] {
Helper.SimpleArtist("1"),
Helper.SimpleArtist("1"),
true
},
// DIFFERENT
new object[] {
Helper.SimpleArtist("1"),
Helper.SimpleArtist("2"),
false
}
};
[Theory]
[MemberData(nameof(ArtistData))]
public void ArtistEquality(SimpleArtist artist1, SimpleArtist artist2, bool shouldEqual)
{
var eq = new StringEquality();
eq.Artist(artist1, artist2).Should().Be(shouldEqual);
}
public static IEnumerable<object[]> EpisodeData =>
new List<object[]>
{
// SAME
new object[] {
Helper.FullEpisode("1", "1", "1"),
Helper.FullEpisode("1", "1", "1"),
true
},
// DIFFERENT PUBLISHER
new object[] {
Helper.FullEpisode("1", "1", "1"),
Helper.FullEpisode("1", "1", "2"),
false
},
// DIFFERENT SHOW
new object[] {
Helper.FullEpisode("1", "1", "1"),
Helper.FullEpisode("1", "2", "1"),
false
},
// DIFFERENT EPISODE
new object[] {
Helper.FullEpisode("1", "1", "1"),
Helper.FullEpisode("2", "1", "1"),
false
},
};
[Theory]
[MemberData(nameof(EpisodeData))]
public void EpisodeEquality(FullEpisode episode1, FullEpisode episode2, bool shouldEqual)
{
var eq = new StringEquality();
eq.Episode(episode1, episode2).Should().Be(shouldEqual);
}
}
}

View File

@ -10,8 +10,10 @@ namespace Selector.Tests
{
static class Helper
{
public static FullTrack FullTrack(string name, string album, List<string> artists)
public static FullTrack FullTrack(string name, string album = "album name", List<string> artists = null)
{
if (artists is null) artists = new List<string>() {"artist"};
return new FullTrack()
{
Name = name,
@ -36,8 +38,9 @@ namespace Selector.Tests
};
}
public static SimpleAlbum SimpleAlbum(string name, List<string> artists)
public static SimpleAlbum SimpleAlbum(string name, List<string> artists = null)
{
if (artists is null) artists = new List<string>() {"artist"};
return new SimpleAlbum()
{
Name = name,
@ -80,11 +83,11 @@ namespace Selector.Tests
};
}
public static CurrentlyPlayingContext CurrentPlayback(FullTrack track, Device device = null, bool isPlaying = true, string context = null)
public static CurrentlyPlayingContext CurrentPlayback(FullTrack track, Device device = null, bool isPlaying = true, string context = "context")
{
return new CurrentlyPlayingContext()
{
Context = Context(context ?? track.Uri),
Context = Context(context),
Device = device ?? Device("device"),
IsPlaying = isPlaying,
Item = track

View File

@ -29,7 +29,7 @@ namespace Selector.Tests
var playingQueue = new Queue<CurrentlyPlayingContext>(playing);
var spotMock = new Mock<IPlayerClient>();
var eq = new UriEquality();
var eq = new UriEqual();
spotMock.Setup(s => s.GetCurrentPlayback().Result).Returns(playingQueue.Dequeue);
@ -47,9 +47,9 @@ namespace Selector.Tests
{
// NO CHANGING
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("nochange", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("nochange", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("nochange", "album1", "artist1"), isPlaying: true, context: "context1"),
},
// to raise
new List<string>(){ },
@ -58,48 +58,48 @@ namespace Selector.Tests
},
// TRACK CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("track2", "album1", "artist1"))
Helper.CurrentPlayback(Helper.FullTrack("trackchange1", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("trackchange2", "album1", "artist1"))
},
// to raise
new List<string>(){ "ItemChange" },
// to not raise
new List<string>(){ "AlbumChange", "ArtistChange", "DeviceChange", "VolumeChange" }
new List<string>(){ "AlbumChange", "ArtistChange", "ContextChange", "PlayingChange", "DeviceChange", "VolumeChange" }
},
// ALBUM CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album2", "artist1"))
Helper.CurrentPlayback(Helper.FullTrack("albumchange", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("albumchange", "album2", "artist1"))
},
// to raise
new List<string>(){ "ItemChange", "AlbumChange" },
new List<string>(){ "AlbumChange" },
// to not raise
new List<string>(){ "ArtistChange", "DeviceChange", "VolumeChange" }
new List<string>(){ "ItemChange", "ArtistChange", "ContextChange", "PlayingChange", "DeviceChange", "VolumeChange" }
},
// ARTIST CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist2"))
Helper.CurrentPlayback(Helper.FullTrack("artistchange", "album1", "artist1")),
Helper.CurrentPlayback(Helper.FullTrack("artistchange", "album1", "artist2"))
},
// to raise
new List<string>(){ "ItemChange", "AlbumChange", "ArtistChange" },
new List<string>(){ "ArtistChange" },
// to not raise
new List<string>(){ "DeviceChange", "VolumeChange" }
new List<string>(){ "ItemChange", "AlbumChange", "ContextChange", "PlayingChange", "DeviceChange", "VolumeChange" }
},
// CONTEXT CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), context: "context2")
Helper.CurrentPlayback(Helper.FullTrack("contextchange", "album1", "artist1"), context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("contextchange", "album1", "artist1"), context: "context2")
},
// to raise
new List<string>(){ "ContextChange" },
// to not raise
new List<string>(){ "ItemChange", "AlbumChange", "ArtistChange", "DeviceChange", "VolumeChange" }
new List<string>(){ "ItemChange", "AlbumChange", "ArtistChange", "PlayingChange", "DeviceChange", "VolumeChange" }
},
// PLAYING CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: false, context: "context1")
Helper.CurrentPlayback(Helper.FullTrack("playingchange1", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("playingchange1", "album1", "artist1"), isPlaying: false, context: "context1")
},
// to raise
new List<string>(){ "PlayingChange" },
@ -108,8 +108,8 @@ namespace Selector.Tests
},
// PLAYING CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: false, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: true, context: "context1")
Helper.CurrentPlayback(Helper.FullTrack("playingchange2", "album1", "artist1"), isPlaying: false, context: "context1"),
Helper.CurrentPlayback(Helper.FullTrack("playingchange2", "album1", "artist1"), isPlaying: true, context: "context1")
},
// to raise
new List<string>(){ "PlayingChange" },
@ -118,8 +118,8 @@ namespace Selector.Tests
},
// CONTENT CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullEpisode("ep1", "show1", "pub1"), isPlaying: true, context: "context2")
Helper.CurrentPlayback(Helper.FullTrack("contentchange1", "album1", "artist1"), isPlaying: true, context: "context1"),
Helper.CurrentPlayback(Helper.FullEpisode("contentchange1", "show1", "pub1"), isPlaying: true, context: "context2")
},
// to raise
new List<string>(){ "ContentChange", "ContextChange", "ItemChange" },
@ -128,8 +128,8 @@ namespace Selector.Tests
},
// CONTENT CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullEpisode("ep1", "show1", "pub1"), isPlaying: true, context: "context2"),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), isPlaying: true, context: "context1")
Helper.CurrentPlayback(Helper.FullEpisode("contentchange1", "show1", "pub1"), isPlaying: true, context: "context2"),
Helper.CurrentPlayback(Helper.FullTrack("contentchange1", "album1", "artist1"), isPlaying: true, context: "context1")
},
// to raise
new List<string>(){ "ContentChange", "ContextChange", "ItemChange" },
@ -138,8 +138,8 @@ namespace Selector.Tests
},
// DEVICE CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), device: Helper.Device("dev1")),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), device: Helper.Device("dev2"))
Helper.CurrentPlayback(Helper.FullTrack("devicechange", "album1", "artist1"), device: Helper.Device("dev1")),
Helper.CurrentPlayback(Helper.FullTrack("devicechange", "album1", "artist1"), device: Helper.Device("dev2"))
},
// to raise
new List<string>(){ "DeviceChange" },
@ -148,8 +148,8 @@ namespace Selector.Tests
},
// VOLUME CHANGE
new object[] { new List<CurrentlyPlayingContext>(){
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), device: Helper.Device("dev1", volume: 50)),
Helper.CurrentPlayback(Helper.FullTrack("track1", "album1", "artist1"), device: Helper.Device("dev1", volume: 60))
Helper.CurrentPlayback(Helper.FullTrack("volumechange", "album1", "artist1"), device: Helper.Device("dev1", volume: 50)),
Helper.CurrentPlayback(Helper.FullTrack("volumechange", "album1", "artist1"), device: Helper.Device("dev1", volume: 60))
},
// to raise
new List<string>(){ "VolumeChange" },
@ -185,7 +185,7 @@ namespace Selector.Tests
var playingQueue = new Queue<CurrentlyPlayingContext>(playing);
var spotMock = new Mock<IPlayerClient>();
var eq = new UriEquality();
var eq = new UriEqual();
spotMock.Setup(
s => s.GetCurrentPlayback().Result

View File

@ -4,6 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>

176
Selector.Tests/UriEqual.cs Normal file
View File

@ -0,0 +1,176 @@
using System;
using System.Collections.Generic;
using Xunit;
using Moq;
using FluentAssertions;
using SpotifyAPI.Web;
using Selector;
namespace Selector.Tests
{
public class UriEqualTests
{
public static IEnumerable<object[]> TrackData =>
new List<object[]>
{
// SAME
new object[] {
Helper.FullTrack("1"),
Helper.FullTrack("1"),
true
},
// WRONG
new object[] {
Helper.FullTrack("1"),
Helper.FullTrack("2"),
false
}
};
[Theory]
[MemberData(nameof(TrackData))]
public void TrackEquality(FullTrack track1, FullTrack track2, bool shouldEqual)
{
new UriEqual()
.IsEqual<FullTrack>(track1, track2)
.Should()
.Be(shouldEqual);
}
public static IEnumerable<object[]> AlbumData =>
new List<object[]>
{
// SAME
new object[] {
Helper.SimpleAlbum("1"),
Helper.SimpleAlbum("1"),
true
},
// WRONG
new object[] {
Helper.SimpleAlbum("1"),
Helper.SimpleAlbum("2"),
false
}
};
[Theory]
[MemberData(nameof(AlbumData))]
public void AlbumEquality(SimpleAlbum album1, SimpleAlbum album2, bool shouldEqual)
{
new UriEqual()
.IsEqual<SimpleAlbum>(album1, album2)
.Should()
.Be(shouldEqual);
}
public static IEnumerable<object[]> ArtistData =>
new List<object[]>
{
// SAME
new object[] {
Helper.SimpleArtist("1"),
Helper.SimpleArtist("1"),
true
},
// WRONG
new object[] {
Helper.SimpleArtist("1"),
Helper.SimpleArtist("2"),
false
}
};
[Theory]
[MemberData(nameof(ArtistData))]
public void ArtistEquality(SimpleArtist artist1, SimpleArtist artist2, bool shouldEqual)
{
new UriEqual()
.IsEqual<SimpleArtist>(artist1, artist2)
.Should()
.Be(shouldEqual);
}
public static IEnumerable<object[]> EpisodeData =>
new List<object[]>
{
// SAME
new object[] {
Helper.FullEpisode("1"),
Helper.FullEpisode("1"),
true
},
// WRONG
new object[] {
Helper.FullEpisode("1"),
Helper.FullEpisode("2"),
false
}
};
[Theory]
[MemberData(nameof(EpisodeData))]
public void EpisodeEquality(FullEpisode ep1, FullEpisode ep2, bool shouldEqual)
{
new UriEqual()
.IsEqual<FullEpisode>(ep1, ep2)
.Should()
.Be(shouldEqual);
}
public static IEnumerable<object[]> ContextData =>
new List<object[]>
{
// SAME
new object[] {
Helper.Context("1"),
Helper.Context("1"),
true
},
// WRONG
new object[] {
Helper.Context("1"),
Helper.Context("2"),
false
}
};
[Theory]
[MemberData(nameof(ContextData))]
public void ContextEquality(Context context1, Context context2, bool shouldEqual)
{
new UriEqual()
.IsEqual<Context>(context1, context2)
.Should()
.Be(shouldEqual);
}
public static IEnumerable<object[]> DeviceData =>
new List<object[]>
{
// SAME
new object[] {
Helper.Device("1"),
Helper.Device("1"),
true
},
// WRONG
new object[] {
Helper.Device("1"),
Helper.Device("2"),
false
}
};
[Theory]
[MemberData(nameof(DeviceData))]
public void DeviceEquality(Device device1, Device device2, bool shouldEqual)
{
new UriEqual()
.IsEqual<Device>(device1, device2)
.Should()
.Be(shouldEqual);
}
}
}

View File

@ -1,13 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
using SpotifyAPI.Web;
namespace Selector
{
class Equal : IEqual
public class Equal : IEqual
{
private Dictionary<Type, object> comps;
public static Equal String() {
return new Equal(){
comps = new Dictionary<Type, object>(){
{typeof(FullTrack), new FullTrackStringComparer()},
{typeof(FullEpisode), new FullEpisodeStringComparer()},
{typeof(FullAlbum), new FullAlbumStringComparer()},
{typeof(FullShow), new FullShowStringComparer()},
{typeof(FullArtist), new FullArtistStringComparer()},
{typeof(SimpleTrack), new SimpleTrackStringComparer()},
{typeof(SimpleEpisode), new SimpleEpisodeStringComparer()},
{typeof(SimpleAlbum), new SimpleAlbumStringComparer()},
{typeof(SimpleShow), new SimpleShowStringComparer()},
{typeof(SimpleArtist), new SimpleArtistStringComparer()},
}
};
}
public bool IsEqual<T>(T item, T other)
{
if (comps.ContainsKey(typeof(T)))

View File

@ -1,22 +0,0 @@
using System;
using SpotifyAPI.Web;
namespace Selector {
public interface IEqualityChecker {
public bool Track(FullTrack track1, FullTrack track2, bool includingAlbum);
public bool Episode(FullEpisode ep1, FullEpisode ep2);
public bool Album(FullAlbum album1, FullAlbum album2);
public bool Show(FullShow show1, FullShow show2);
public bool Artist(FullArtist artist1, FullArtist artist2);
public bool Track(SimpleTrack track1, SimpleTrack track2);
public bool Episode(SimpleEpisode ep1, SimpleEpisode ep2);
public bool Album(SimpleAlbum album1, SimpleAlbum album2);
public bool Show(SimpleShow show1, SimpleShow show2);
public bool Artist(SimpleArtist artist1, SimpleArtist artist2);
public bool Context(Context context1, Context context2);
public bool Device(Device device1, Device device2);
}
}

View File

@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SpotifyAPI.Web;
namespace Selector
{
public abstract class NoHashCode<T>: EqualityComparer<T>
{
public override int GetHashCode(T obj)
{
throw new NotImplementedException();
}
}
public class FullTrackStringComparer : NoHashCode<FullTrack>
{
public override bool Equals(FullTrack track1, FullTrack track2) => FullTrackStringComparer.IsEqual(track1, track2);
public static bool IsEqual(FullTrack track1, FullTrack track2) => track1.Name == track2.Name
&& Enumerable.SequenceEqual(track1.Artists.Select(a => a.Name), track2.Artists.Select(a => a.Name))
&& SimpleAlbumStringComparer.IsEqual(track1.Album, track2.Album);
}
public class FullEpisodeStringComparer : NoHashCode<FullEpisode>
{
public override bool Equals(FullEpisode ep1, FullEpisode ep2) => FullEpisodeStringComparer.IsEqual(ep1, ep2);
public static bool IsEqual(FullEpisode ep1, FullEpisode ep2) => ep1.Name == ep2.Name
&& SimpleShowStringComparer.IsEqual(ep1.Show, ep2.Show);
}
public class FullAlbumStringComparer : NoHashCode<FullAlbum>
{
public override bool Equals(FullAlbum album1, FullAlbum album2) => FullAlbumStringComparer.IsEqual(album1, album2);
public static bool IsEqual(FullAlbum album1, FullAlbum album2) => album1.Name == album2.Name
&& Enumerable.SequenceEqual(album1.Artists.Select(a => a.Name), album2.Artists.Select(a => a.Name));
}
public class FullShowStringComparer : NoHashCode<FullShow>
{
public override bool Equals(FullShow show1, FullShow show2) => FullShowStringComparer.IsEqual(show1, show2);
public static bool IsEqual(FullShow show1, FullShow show2) => show1.Name == show2.Name
&& show1.Publisher == show2.Publisher;
}
public class FullArtistStringComparer : NoHashCode<FullArtist>
{
public override bool Equals(FullArtist artist1, FullArtist artist2) => FullArtistStringComparer.IsEqual(artist1, artist2);
public static bool IsEqual(FullArtist artist1, FullArtist artist2) => artist1.Name == artist2.Name;
}
public class SimpleTrackStringComparer : NoHashCode<SimpleTrack>
{
public override bool Equals(SimpleTrack track1, SimpleTrack track2) => SimpleTrackStringComparer.IsEqual(track1, track2);
public static bool IsEqual(SimpleTrack track1, SimpleTrack track2) => track1.Name == track2.Name
&& Enumerable.SequenceEqual(track1.Artists.Select(a => a.Name), track2.Artists.Select(a => a.Name));
}
public class SimpleEpisodeStringComparer : NoHashCode<SimpleEpisode>
{
public override bool Equals(SimpleEpisode ep1, SimpleEpisode ep2) => SimpleEpisodeStringComparer.IsEqual(ep1, ep2);
public static bool IsEqual(SimpleEpisode ep1, SimpleEpisode ep2) => ep1.Name == ep2.Name;
}
public class SimpleAlbumStringComparer : NoHashCode<SimpleAlbum>
{
public override bool Equals(SimpleAlbum album1, SimpleAlbum album2) => SimpleAlbumStringComparer.IsEqual(album1, album2);
public static bool IsEqual(SimpleAlbum album1, SimpleAlbum album2) => album1.Name == album2.Name
&& Enumerable.SequenceEqual(album1.Artists.Select(a => a.Name), album2.Artists.Select(a => a.Name));
}
public class SimpleShowStringComparer : NoHashCode<SimpleShow>
{
public override bool Equals(SimpleShow show1, SimpleShow show2) => SimpleShowStringComparer.IsEqual(show1, show2);
public static bool IsEqual(SimpleShow show1, SimpleShow show2) => show1.Name == show2.Name
&& show1.Publisher == show2.Publisher;
}
public class SimpleArtistStringComparer : NoHashCode<SimpleArtist>
{
public override bool Equals(SimpleArtist artist1, SimpleArtist artist2) => SimpleArtistStringComparer.IsEqual(artist1, artist2);
public static bool IsEqual(SimpleArtist artist1, SimpleArtist artist2) => artist1.Name == artist2.Name;
}
}

View File

@ -1,70 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SpotifyAPI.Web;
namespace Selector {
public class StringEquality: UriEquality {
new public bool Track(FullTrack track1, FullTrack track2, bool includingAlbum = true)
{
var ret = includingAlbum ? Album(track1.Album, track2.Album): true;
return ret
&& track1.Name == track2.Name
&& Enumerable.SequenceEqual(track1.Artists.Select(a => a.Name), track2.Artists.Select(a => a.Name));
}
new public bool Episode(FullEpisode ep1, FullEpisode ep2)
{
return ep1.Uri == ep2.Uri
&& Show(ep1.Show, ep2.Show);
}
new public bool Album(FullAlbum album1, FullAlbum album2)
{
return album1.Name == album2.Name
&& Enumerable.SequenceEqual(album1.Artists.Select(a => a.Name), album2.Artists.Select(a => a.Name));
}
new public bool Show(FullShow show1, FullShow show2)
{
return show1.Name == show2.Name
&& show1.Publisher == show2.Publisher;
}
new public bool Artist(FullArtist artist1, FullArtist artist2)
{
return artist1.Name == artist2.Name;
}
new public bool Track(SimpleTrack track1, SimpleTrack track2)
{
return track1.Name == track2.Name
&& Enumerable.SequenceEqual(track1.Artists.Select(a => a.Name), track2.Artists.Select(a => a.Name));
}
new public bool Episode(SimpleEpisode ep1, SimpleEpisode ep2)
{
return ep1.Name == ep2.Name;
}
new public bool Album(SimpleAlbum album1, SimpleAlbum album2)
{
return album1.Name == album2.Name
&& Enumerable.SequenceEqual(album1.Artists.Select(a => a.Name), album2.Artists.Select(a => a.Name));
}
new public bool Show(SimpleShow show1, SimpleShow show2)
{
return show1.Name == show2.Name
&& show1.Publisher == show2.Publisher;
}
new public bool Artist(SimpleArtist artist1, SimpleArtist artist2)
{
return artist1.Name == artist2.Name;
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
using SpotifyAPI.Web;
namespace Selector
{
public class UriEqual : IEqual
{
public bool IsEqual<T>(T item, T other)
{
var uri = typeof(T).GetProperty("Uri");
if(uri is not null)
return (string) uri.GetValue(item) == (string) uri.GetValue(other);
else
{
var id = typeof(T).GetProperty("Id");
if(id is not null)
return (string) id.GetValue(item) == (string) id.GetValue(other);
else
throw new ArgumentException($"{typeof(T)} does not contain a uri or id");
}
}
}
}

View File

@ -1,74 +0,0 @@
using System;
using System.Linq;
using SpotifyAPI.Web;
namespace Selector {
public class UriEquality: IEqualityChecker {
public bool Track(FullTrack track1, FullTrack track2, bool includingAlbum = true)
{
var ret = includingAlbum ? Album(track1.Album, track2.Album) : true;
return ret
&& track1.Uri == track2.Uri
&& Enumerable.SequenceEqual(track1.Artists.Select(a => a.Uri), track2.Artists.Select(a => a.Uri));
}
public bool Episode(FullEpisode ep1, FullEpisode ep2)
{
return ep1.Uri == ep2.Uri;
}
public bool Album(FullAlbum album1, FullAlbum album2)
{
return album1.Uri == album2.Uri
&& Enumerable.SequenceEqual(album1.Artists.Select(a => a.Uri), album2.Artists.Select(a => a.Uri));
}
public bool Show(FullShow show1, FullShow show2)
{
return show1.Uri == show2.Uri;
}
public bool Artist(FullArtist artist1, FullArtist artist2)
{
return artist1.Uri == artist2.Uri;
}
public bool Track(SimpleTrack track1, SimpleTrack track2)
{
return track1.Uri == track2.Uri
&& Enumerable.SequenceEqual(track1.Artists.Select(a => a.Uri), track2.Artists.Select(a => a.Uri));
}
public bool Episode(SimpleEpisode ep1, SimpleEpisode ep2)
{
return ep1.Uri == ep2.Uri;
}
public bool Album(SimpleAlbum album1, SimpleAlbum album2)
{
return album1.Uri == album2.Uri
&& Enumerable.SequenceEqual(album1.Artists.Select(a => a.Uri), album2.Artists.Select(a => a.Uri));
}
public bool Show(SimpleShow show1, SimpleShow show2)
{
return show1.Uri == show2.Uri;
}
public bool Artist(SimpleArtist artist1, SimpleArtist artist2)
{
return artist1.Uri == artist2.Uri;
}
public bool Context(Context context1, Context context2)
{
return context1?.Uri == context2?.Uri;
}
public bool Device(Device device1, Device device2)
{
return device1?.Id == device2?.Id;
}
}
}

View File

@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<EnableDefaultCompileItems>true</EnableDefaultCompileItems>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>

View File

@ -9,7 +9,7 @@ namespace Selector
public class PlayerWatcher: BaseWatcher, IPlayerWatcher
{
private readonly IPlayerClient spotifyClient;
private readonly IEqualityChecker equalityChecker;
private readonly IEqual eq;
public event EventHandler<ListeningChangeEventArgs> ItemChange;
public event EventHandler<ListeningChangeEventArgs> AlbumChange;
@ -26,11 +26,11 @@ namespace Selector
private List<List<CurrentlyPlayingContext>> lastPlays { get; set; }
public PlayerWatcher(IPlayerClient spotifyClient,
IEqualityChecker equalityChecker,
IEqual equalityChecker,
int pollPeriod = 3000) {
this.spotifyClient = spotifyClient;
this.equalityChecker = equalityChecker;
this.eq = equalityChecker;
this.PollPeriod = pollPeriod;
lastPlays = new List<List<CurrentlyPlayingContext>>();
@ -78,15 +78,15 @@ namespace Selector
if(previous.Item is FullTrack previousTrack && Live.Item is FullTrack currentTrack)
{
if(!equalityChecker.Track(previousTrack, currentTrack, true)) {
if(!eq.IsEqual<FullTrack>(previousTrack, currentTrack)) {
OnItemChange(ListeningChangeEventArgs.From(previous, Live));
}
if(!equalityChecker.Album(previousTrack.Album, currentTrack.Album)) {
if(!eq.IsEqual<SimpleAlbum>(previousTrack.Album, currentTrack.Album)) {
OnAlbumChange(ListeningChangeEventArgs.From(previous, Live));
}
if(!equalityChecker.Artist(previousTrack.Artists[0], currentTrack.Artists[0])) {
if(!eq.IsEqual<SimpleArtist>(previousTrack.Artists[0], currentTrack.Artists[0])) {
OnArtistChange(ListeningChangeEventArgs.From(previous, Live));
}
}
@ -100,7 +100,7 @@ namespace Selector
// PODCASTS
else if(previous.Item is FullEpisode previousEp && Live.Item is FullEpisode currentEp)
{
if(!equalityChecker.Episode(previousEp, currentEp)) {
if(!eq.IsEqual<FullEpisode>(previousEp, currentEp)) {
OnItemChange(ListeningChangeEventArgs.From(previous, Live));
}
}
@ -109,12 +109,12 @@ namespace Selector
}
// CONTEXT
if(!equalityChecker.Context(previous.Context, Live.Context)) {
if(!eq.IsEqual<Context>(previous.Context, Live.Context)) {
OnContextChange(ListeningChangeEventArgs.From(previous, Live));
}
// DEVICE
if(!equalityChecker.Device(previous?.Device, Live?.Device)) {
if(!eq.IsEqual<Device>(previous?.Device, Live?.Device)) {
OnDeviceChange(ListeningChangeEventArgs.From(previous, Live));
}
@ -158,14 +158,14 @@ namespace Selector
var castItem = (FullTrack) current.Item;
var castStoredItem = (FullTrack) lastPlays[0][0].Item;
matchesMostRecent = equalityChecker.Track(castItem, castStoredItem, true);
matchesMostRecent = eq.IsEqual<FullTrack>(castItem, castStoredItem);
}
catch(InvalidCastException)
{
var castItem = (FullEpisode) current.Item;
var castStoredItem = (FullEpisode) lastPlays[0][0].Item;
matchesMostRecent = equalityChecker.Episode(castItem, castStoredItem);
matchesMostRecent = eq.IsEqual<FullEpisode>(castItem, castStoredItem);
}
if (matchesMostRecent)