Skip to content

Commit 3544c34

Browse files
author
mrnikbobjeff
committed
Changed Contracts to if-throw-else as requested
Changed use of nullable return types to throw exceptions instead of returning null
1 parent e5e57c3 commit 3544c34

File tree

10 files changed

+91
-73
lines changed

10 files changed

+91
-73
lines changed

SpotifyAPI.Example/App.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<configuration>
3-
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5-
</startup>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
66
</configuration>

SpotifyAPI.Example/ExampleForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ public ExampleForm()
99
InitializeComponent();
1010
}
1111
}
12-
}
12+
}

SpotifyAPI.Example/LocalControl.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System;
1+
using SpotifyAPI.Local;
2+
using SpotifyAPI.Local.Enums;
3+
using SpotifyAPI.Local.Models;
4+
using System;
25
using System.Diagnostics;
36
using System.Globalization;
47
using System.Windows.Forms;
5-
using SpotifyAPI.Local;
6-
using SpotifyAPI.Local.Enums;
7-
using SpotifyAPI.Local.Models;
88

99
namespace SpotifyAPI.Example
1010
{
@@ -102,23 +102,23 @@ public void UpdatePlayingStatus(bool playing)
102102
isPlayingLabel.Text = playing.ToString();
103103
}
104104

105-
void _spotify_OnVolumeChange(VolumeChangeEventArgs e)
105+
private void _spotify_OnVolumeChange(VolumeChangeEventArgs e)
106106
{
107-
volumeLabel.Text = (e.NewVolume*100).ToString(CultureInfo.InvariantCulture);
107+
volumeLabel.Text = (e.NewVolume * 100).ToString(CultureInfo.InvariantCulture);
108108
}
109109

110-
void _spotify_OnTrackTimeChange(TrackTimeChangeEventArgs e)
110+
private void _spotify_OnTrackTimeChange(TrackTimeChangeEventArgs e)
111111
{
112112
timeLabel.Text = FormatTime(e.TrackTime) + "/" + FormatTime(_currentTrack.Length);
113-
timeProgressBar.Value = (int) e.TrackTime;
113+
timeProgressBar.Value = (int)e.TrackTime;
114114
}
115115

116-
void _spotify_OnTrackChange(TrackChangeEventArgs e)
116+
private void _spotify_OnTrackChange(TrackChangeEventArgs e)
117117
{
118118
UpdateTrack(e.NewTrack);
119119
}
120120

121-
void _spotify_OnPlayStateChange(PlayStateEventArgs e)
121+
private void _spotify_OnPlayStateChange(PlayStateEventArgs e)
122122
{
123123
UpdatePlayingStatus(e.Playing);
124124
}
@@ -162,4 +162,4 @@ private static String FormatTime(double sec)
162162
return mins + ":" + secs;
163163
}
164164
}
165-
}
165+
}

SpotifyAPI.Example/Program.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
52
using System.Windows.Forms;
63

74
namespace SpotifyAPI.Example
85
{
9-
static class Program
6+
internal static class Program
107
{
118
/// <summary>
129
/// Der Haupteinstiegspunkt für die Anwendung.
1310
/// </summary>
1411
[STAThread]
15-
static void Main()
12+
private static void Main()
1613
{
1714
Application.EnableVisualStyles();
1815
Application.SetCompatibleTextRenderingDefault(false);
1916
Application.Run(new ExampleForm());
2017
}
2118
}
22-
}
19+
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

5-
// Allgemeine Informationen über eine Assembly werden über die folgenden
4+
// Allgemeine Informationen über eine Assembly werden über die folgenden
65
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
76
// die mit einer Assembly verknüpft sind.
87
[assembly: AssemblyTitle("SpotifyAPI.Example")]
@@ -14,8 +13,8 @@
1413
[assembly: AssemblyTrademark("")]
1514
[assembly: AssemblyCulture("")]
1615

17-
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
18-
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
16+
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
17+
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
1918
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
2019
[assembly: ComVisible(false)]
2120

@@ -25,12 +24,12 @@
2524
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
2625
//
2726
// Hauptversion
28-
// Nebenversion
27+
// Nebenversion
2928
// Buildnummer
3029
// Revision
3130
//
32-
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
31+
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
3332
// übernehmen, indem Sie "*" eingeben:
3433
// [assembly: AssemblyVersion("1.0.*")]
3534
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]

SpotifyAPI.Example/WebControl.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using System;
1+
using SpotifyAPI.Web;
2+
using SpotifyAPI.Web.Auth;
3+
using SpotifyAPI.Web.Enums;
4+
using SpotifyAPI.Web.Models;
5+
using System;
26
using System.Collections.Generic;
3-
using System.Drawing;
47
using System.IO;
58
using System.Linq;
69
using System.Net;
710
using System.Windows.Forms;
8-
using SpotifyAPI.Web;
9-
using SpotifyAPI.Web.Auth;
10-
using SpotifyAPI.Web.Enums;
11-
using SpotifyAPI.Web.Models;
1211
using Image = System.Drawing.Image;
1312

1413
namespace SpotifyAPI.Example
@@ -21,7 +20,7 @@ public partial class WebControl : UserControl
2120
private PrivateProfile _profile;
2221
private List<FullTrack> _savedTracks;
2322
private List<SimplePlaylist> _playlists;
24-
23+
2524
public WebControl()
2625
{
2726
InitializeComponent();
@@ -37,7 +36,7 @@ public WebControl()
3736
_auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent;
3837
}
3938

40-
void _auth_OnResponseReceivedEvent(Token token, string state)
39+
private void _auth_OnResponseReceivedEvent(Token token, string state)
4140
{
4241
_auth.StopHttpServer();
4342

@@ -77,7 +76,7 @@ private async void InitialSetup()
7776
_savedTracks.ForEach(track => savedTracksListView.Items.Add(new ListViewItem()
7877
{
7978
Text = track.Name,
80-
SubItems = {string.Join(",", track.Artists.Select(source => source.Name)), track.Album.Name}
79+
SubItems = { string.Join(",", track.Artists.Select(source => source.Name)), track.Album.Name }
8180
}));
8281

8382
_playlists = GetPlaylists();
@@ -134,4 +133,4 @@ private void authButton_Click(object sender, EventArgs e)
134133
_auth.DoAuth();
135134
}
136135
}
137-
}
136+
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

5-
// Allgemeine Informationen über eine Assembly werden über die folgenden
4+
// Allgemeine Informationen über eine Assembly werden über die folgenden
65
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
76
// die mit einer Assembly verknüpft sind.
87
[assembly: AssemblyTitle("SpotifyAPI.Tests")]
@@ -14,8 +13,8 @@
1413
[assembly: AssemblyTrademark("")]
1514
[assembly: AssemblyCulture("")]
1615

17-
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
18-
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
16+
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
17+
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
1918
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
2019
[assembly: ComVisible(false)]
2120

@@ -25,12 +24,12 @@
2524
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
2625
//
2726
// Hauptversion
28-
// Nebenversion
27+
// Nebenversion
2928
// Buildnummer
3029
// Revision
3130
//
32-
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
31+
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
3332
// übernehmen, indem Sie "*" eingeben:
3433
// [assembly: AssemblyVersion("1.0.*")]
3534
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]

SpotifyAPI.Tests/TestClass.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using System.IO;
3-
using System.Linq;
4-
using Moq;
1+
using Moq;
52
using Newtonsoft.Json;
63
using NUnit.Framework;
74
using SpotifyAPI.Web;
85
using SpotifyAPI.Web.Models;
6+
using System;
7+
using System.IO;
8+
using System.Linq;
99

1010
namespace SpotifyAPI.Tests
1111
{
@@ -67,4 +67,4 @@ public void ShouldGetPublicProfile()
6767

6868
//Will add more tests once I decided if this is worth the effort (propably not?)
6969
}
70-
}
70+
}

SpotifyAPI/Local/SpotifyLocalAPI.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,12 @@ public StatusResponse GetStatus()
162162
public void Mute()
163163
{
164164
//Windows < Windows Vista Check
165-
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
165+
if (Environment.OSVersion.Version.Major < 6)
166+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
166167
//Windows Vista Check
167168
if (Environment.OSVersion.Version.Major == 6)
168-
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
169+
if(Environment.OSVersion.Version.Minor == 0)
170+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
169171
VolumeMixerControl.MuteSpotify(true);
170172
}
171173

@@ -175,24 +177,28 @@ public void Mute()
175177
public void UnMute()
176178
{
177179
//Windows < Windows Vista Check
178-
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
180+
if (Environment.OSVersion.Version.Major < 6)
181+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
179182
//Windows Vista Check
180183
if (Environment.OSVersion.Version.Major == 6)
181-
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
184+
if (Environment.OSVersion.Version.Minor == 0)
185+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
182186
VolumeMixerControl.MuteSpotify(false);
183187
}
184188

185189
/// <summary>
186190
/// Checks whether Spotify is muted in the Volume Mixer control (required Windows 7 or newer)
187191
/// </summary>
188192
/// <returns>Null if an error occured, otherwise the muted state</returns>
189-
public bool? IsSpotifyMuted()
193+
public bool IsSpotifyMuted()
190194
{
191195
//Windows < Windows Vista Check
192-
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
196+
if (Environment.OSVersion.Version.Major < 6)
197+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
193198
//Windows Vista Check
194199
if (Environment.OSVersion.Version.Major == 6)
195-
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
200+
if (Environment.OSVersion.Version.Minor == 0)
201+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
196202
return VolumeMixerControl.IsSpotifyMuted();
197203
}
198204

@@ -202,26 +208,31 @@ public void UnMute()
202208
/// <param name="volume">A value between 0 and 100</param>
203209
public void SetSpotifyVolume(float volume = 100)
204210
{
205-
Contract.Requires(0 <= volume && volume <= 100);
206211
//Windows < Windows Vista Check
207-
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
212+
if (Environment.OSVersion.Version.Major < 6)
213+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
208214
//Windows Vista Check
209215
if (Environment.OSVersion.Version.Major == 6)
210-
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
216+
if (Environment.OSVersion.Version.Minor == 0)
217+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
218+
if (volume < 0 || volume > 100)
219+
throw new ArgumentOutOfRangeException("Volume parameter has to be a value between 0 and 100");
211220
VolumeMixerControl.SetSpotifyVolume(volume);
212221
}
213222

214223
/// <summary>
215224
/// Return the Volume Mixer volume of Spotify (requires Windows 7 or newer)
216225
/// </summary>
217226
/// <returns>Null if an error occured, otherwise a float between 0 and 100</returns>
218-
public float? GetSpotifyVolume()
227+
public float GetSpotifyVolume()
219228
{
220229
//Windows < Windows Vista Check
221-
Contract.Requires(Environment.OSVersion.Version.Major >= 6);
230+
if (Environment.OSVersion.Version.Major < 6)
231+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
222232
//Windows Vista Check
223233
if (Environment.OSVersion.Version.Major == 6)
224-
Contract.Requires(Environment.OSVersion.Version.Minor != 0);
234+
if (Environment.OSVersion.Version.Minor == 0)
235+
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
225236
return VolumeMixerControl.GetSpotifyVolume();
226237
}
227238

0 commit comments

Comments
 (0)