diff --git a/LibMpc/Commands/Commands.Output.cs b/LibMpc/Commands/Commands.Output.cs index 3c76ea1..54725ef 100644 --- a/LibMpc/Commands/Commands.Output.cs +++ b/LibMpc/Commands/Commands.Output.cs @@ -26,8 +26,8 @@ namespace LibMpc public string FormatResponse(IList> response) { - // TODO: - return response.ToString(); + // Response should be empty. + return string.Join(", ", response); } } @@ -47,8 +47,8 @@ namespace LibMpc public string FormatResponse(IList> response) { - // TODO: - return response.ToString(); + // Response should be empty. + return string.Join(", ", response); } } @@ -69,7 +69,7 @@ namespace LibMpc { var outputId = int.Parse(response[i].Value); var outputName = response[i + 1].Value; - var outputEnabled = bool.Parse(response[i + 2].Value); + var outputEnabled = response[i + 2].Value == "1"; result.Add(new MpdOutput(outputId, outputName, outputEnabled)); } diff --git a/LibMpcTest/LibMpcTest.cs b/LibMpcTest/LibMpcTest.cs index 4a2332d..bb77415 100644 --- a/LibMpcTest/LibMpcTest.cs +++ b/LibMpcTest/LibMpcTest.cs @@ -1,80 +1,24 @@ using LibMpc; -using System; using System.Diagnostics; -using System.Linq; -using System.Net; -using System.Threading.Tasks; -using Newtonsoft.Json; using Xunit; -using Xunit.Abstractions; namespace LibMpcTest { - public class LibMpcTest : IClassFixture, IDisposable + public partial class LibMpcTest : IClassFixture, IClassFixture { - private readonly MpdServerTest _server; - private readonly ITestOutputHelper _output; - private readonly Mpc _mpc; - - public LibMpcTest(MpdServerTest server, ITestOutputHelper output) + public LibMpcTest(MpcMock mpc) { - _server = server; - _output = output; - - _mpc = new Mpc(new IPEndPoint(IPAddress.Loopback, 6600)); - - var connected = Task.Run(async () => await _mpc.ConnectAsync()).Result; - if (connected) - { - WriteLine("Connected to MPD."); - } - else - { - WriteLine("Could not connect to MPD."); - } + Mpc = mpc.Client; } - [Fact] - public async Task TagTypesTest() + internal Mpc Mpc { get; } + } + + internal class TestUtils + { + internal static void WriteLine(string value) { - var response = await _mpc.SendAsync(new Commands.Reflection.TagTypes()); - - WriteLine("TagTypesTest Result:"); - WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); - - Assert.True(response.Response.Body.Count().Equals(17)); - } - - [Fact] - public async Task ListAllTest() - { - var response = await _mpc.SendAsync(new Commands.Database.ListAll()); - - WriteLine("ListAllTest Result:"); - WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); - - Assert.True(response.Response.Body.Count().Equals(7)); - } - - [Fact] - public async Task FindGenreTest() - { - var response = await _mpc.SendAsync(new Commands.Database.Find(MpdTags.Genre, "soundfx")); - - WriteLine("FindGenreTest Result:"); - WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); - - Assert.True(response.Response.Body.Count().Equals(6)); - } - - public void Dispose() - { - _mpc?.DisconnectAsync().GetAwaiter().GetResult(); - } - - private void WriteLine(string value) - { - Console.Out.WriteLine(value); + Debug.WriteLine(value); } } } diff --git a/LibMpcTest/MpcMock.cs b/LibMpcTest/MpcMock.cs new file mode 100644 index 0000000..f75264d --- /dev/null +++ b/LibMpcTest/MpcMock.cs @@ -0,0 +1,26 @@ +using LibMpc; +using System; +using System.Net; +using System.Threading.Tasks; + +namespace LibMpcTest +{ + public class MpcMock : IDisposable + { + public MpcMock() + { + Client = new Mpc(new IPEndPoint(IPAddress.Loopback, 6600)); + + var connected = Task.Run(async () => await Client.ConnectAsync()).Result; + TestUtils.WriteLine($"Connected to MPD : {connected}"); + } + + public Mpc Client { get; } + + public void Dispose() + { + Client?.DisconnectAsync().GetAwaiter().GetResult(); + TestUtils.WriteLine($"Disconnected from MPD."); + } + } +} \ No newline at end of file diff --git a/LibMpcTest/MpdServerTest.cs b/LibMpcTest/MpdMock.cs similarity index 82% rename from LibMpcTest/MpdServerTest.cs rename to LibMpcTest/MpdMock.cs index d3f9b2d..e0935dd 100644 --- a/LibMpcTest/MpdServerTest.cs +++ b/LibMpcTest/MpdMock.cs @@ -5,9 +5,9 @@ using System.Runtime.InteropServices; namespace LibMpcTest { - public class MpdServerTest : IDisposable + public class MpdMock : IDisposable { - public MpdServerTest() + public MpdMock() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { @@ -32,11 +32,11 @@ namespace LibMpcTest } }; - Console.Out.WriteLine($"Starting Server: {Process.StartInfo.FileName} {Process.StartInfo.Arguments}"); + TestUtils.WriteLine($"Starting Server: {Process.StartInfo.FileName} {Process.StartInfo.Arguments}"); Process.Start(); - Console.Out.WriteLine($"Output: {Process.StandardOutput.ReadToEnd()}"); - Console.Out.WriteLine($"Error: {Process.StandardError.ReadToEnd()}"); + TestUtils.WriteLine($"Output: {Process.StandardOutput.ReadToEnd()}"); + TestUtils.WriteLine($"Error: {Process.StandardError.ReadToEnd()}"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { @@ -80,15 +80,16 @@ namespace LibMpcTest netcat.Start(); netcat.WaitForExit(); - Console.Out.WriteLine(command); - Console.Out.WriteLine($"Output: {netcat.StandardOutput.ReadToEnd()}"); - Console.Out.WriteLine($"Error: {netcat.StandardError.ReadToEnd()}"); + TestUtils.WriteLine(command); + TestUtils.WriteLine($"Output: {netcat.StandardOutput.ReadToEnd()}"); + TestUtils.WriteLine($"Error: {netcat.StandardError.ReadToEnd()}"); } public void Dispose() { Process?.Kill(); Process?.Dispose(); + TestUtils.WriteLine("Server Stopped."); } private class Server diff --git a/LibMpcTest/Tests/DatabaseCommandsTest.cs b/LibMpcTest/Tests/DatabaseCommandsTest.cs new file mode 100644 index 0000000..16b28b8 --- /dev/null +++ b/LibMpcTest/Tests/DatabaseCommandsTest.cs @@ -0,0 +1,33 @@ +using System.Threading.Tasks; +using Newtonsoft.Json; +using Xunit; +using System.Linq; +using LibMpc; + +namespace LibMpcTest +{ + public partial class LibMpcTest + { + [Fact] + public async Task ListAllTest() + { + var response = await Mpc.SendAsync(new Commands.Database.ListAll()); + + TestUtils.WriteLine("ListAllTest Result:"); + TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + + Assert.True(response.Response.Body.Count().Equals(7)); + } + + [Fact] + public async Task FindGenreTest() + { + var response = await Mpc.SendAsync(new Commands.Database.Find(MpdTags.Genre, "soundfx")); + + TestUtils.WriteLine("FindGenreTest Result:"); + TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + + Assert.True(response.Response.Body.Count().Equals(6)); + } + } +} \ No newline at end of file diff --git a/LibMpcTest/Tests/OutputCommandsTest.cs b/LibMpcTest/Tests/OutputCommandsTest.cs new file mode 100644 index 0000000..c46a35b --- /dev/null +++ b/LibMpcTest/Tests/OutputCommandsTest.cs @@ -0,0 +1,59 @@ +using System.Threading.Tasks; +using Newtonsoft.Json; +using Xunit; +using LibMpc; +using System.Linq; + +namespace LibMpcTest +{ + public partial class LibMpcTest + { + [Fact] + public async Task DisableOutputTest() + { + var responseOutputs = await Mpc.SendAsync(new Commands.Output.Outputs()); + Assert.True(responseOutputs.Response.Body.Single(output => output.Id.Equals(2)).IsEnabled); + + var response = await Mpc.SendAsync(new Commands.Output.DisableOutput(2)); + + TestUtils.WriteLine("DisableOutputTest Result:"); + TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + + Assert.True(response.Response.Body.Equals(string.Empty)); + Assert.True(response.Response.State.Status.Equals("OK")); + + responseOutputs = await Mpc.SendAsync(new Commands.Output.Outputs()); + Assert.False(responseOutputs.Response.Body.Single(output => output.Id.Equals(2)).IsEnabled); + } + + [Fact] + public async Task EnableOutputTest() + { + var responseOutputs = await Mpc.SendAsync(new Commands.Output.Outputs()); + // By default should be disable from mpd.config + Assert.False(responseOutputs.Response.Body.Single(output => output.Id.Equals(1)).IsEnabled); + + var response = await Mpc.SendAsync(new Commands.Output.EnableOutput(1)); + + TestUtils.WriteLine("EnableOutputTest Result:"); + TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + + Assert.True(response.Response.Body.Equals(string.Empty)); + Assert.True(response.Response.State.Status.Equals("OK")); + + responseOutputs = await Mpc.SendAsync(new Commands.Output.Outputs()); + Assert.True(responseOutputs.Response.Body.Single(output => output.Id.Equals(1)).IsEnabled); + } + + [Fact] + public async Task LisOutputsTest() + { + var response = await Mpc.SendAsync(new Commands.Output.Outputs()); + + TestUtils.WriteLine("LisOutputsTest Result:"); + TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + + Assert.True(response.Response.Body.Count().Equals(3)); + } + } +} \ No newline at end of file diff --git a/LibMpcTest/Tests/ReflectionCommandsTest.cs b/LibMpcTest/Tests/ReflectionCommandsTest.cs new file mode 100644 index 0000000..4067373 --- /dev/null +++ b/LibMpcTest/Tests/ReflectionCommandsTest.cs @@ -0,0 +1,22 @@ +using System.Threading.Tasks; +using Newtonsoft.Json; +using Xunit; +using LibMpc; +using System.Linq; + +namespace LibMpcTest +{ + public partial class LibMpcTest + { + [Fact] + public async Task TagTypesTest() + { + var response = await Mpc.SendAsync(new Commands.Reflection.TagTypes()); + + TestUtils.WriteLine("TagTypesTest Result:"); + TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + + Assert.True(response.Response.Body.Count().Equals(17)); + } + } +} \ No newline at end of file