From e2ebbe7e1466eecc630707c54b9c51b1965db8e2 Mon Sep 17 00:00:00 2001 From: glucaci Date: Tue, 20 Dec 2016 11:03:28 +0100 Subject: [PATCH] Output commands completed + test --- LibMpc/Commands/Commands.Output.cs | 21 ++++++++++++++- LibMpcTest/LibMpcTest.cs | 9 ------- LibMpcTest/MpcMock.cs | 4 +-- LibMpcTest/MpdMock.cs | 14 +++++----- LibMpcTest/TestOutput.cs | 12 +++++++++ LibMpcTest/Tests/DatabaseCommandsTest.cs | 8 +++--- LibMpcTest/Tests/OutputCommandsTest.cs | 30 +++++++++++++++++----- LibMpcTest/Tests/ReflectionCommandsTest.cs | 4 +-- 8 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 LibMpcTest/TestOutput.cs diff --git a/LibMpc/Commands/Commands.Output.cs b/LibMpc/Commands/Commands.Output.cs index 54725ef..0597d37 100644 --- a/LibMpc/Commands/Commands.Output.cs +++ b/LibMpc/Commands/Commands.Output.cs @@ -52,7 +52,26 @@ namespace LibMpc } } - // TODO: toggleoutput // Turns an output on or off, depending on the current state. + /// + /// Turns an output on or off, depending on the current state. + /// + public class ToggleOutput : IMpcCommand + { + private readonly int _outputId; + + public ToggleOutput(int outputId) + { + _outputId = outputId; + } + + public string Value => string.Join(" ", "toggleoutput", _outputId); + + public string FormatResponse(IList> response) + { + // Response should be empty. + return string.Join(", ", response); + } + } /// /// Shows information about all outputs. diff --git a/LibMpcTest/LibMpcTest.cs b/LibMpcTest/LibMpcTest.cs index bb77415..a88a16a 100644 --- a/LibMpcTest/LibMpcTest.cs +++ b/LibMpcTest/LibMpcTest.cs @@ -1,5 +1,4 @@ using LibMpc; -using System.Diagnostics; using Xunit; namespace LibMpcTest @@ -13,12 +12,4 @@ namespace LibMpcTest internal Mpc Mpc { get; } } - - internal class TestUtils - { - internal static void WriteLine(string value) - { - Debug.WriteLine(value); - } - } } diff --git a/LibMpcTest/MpcMock.cs b/LibMpcTest/MpcMock.cs index f75264d..51ab76c 100644 --- a/LibMpcTest/MpcMock.cs +++ b/LibMpcTest/MpcMock.cs @@ -12,7 +12,7 @@ namespace LibMpcTest Client = new Mpc(new IPEndPoint(IPAddress.Loopback, 6600)); var connected = Task.Run(async () => await Client.ConnectAsync()).Result; - TestUtils.WriteLine($"Connected to MPD : {connected}"); + TestOutput.WriteLine($"Connected to MPD : {connected}"); } public Mpc Client { get; } @@ -20,7 +20,7 @@ namespace LibMpcTest public void Dispose() { Client?.DisconnectAsync().GetAwaiter().GetResult(); - TestUtils.WriteLine($"Disconnected from MPD."); + TestOutput.WriteLine($"Disconnected from MPD."); } } } \ No newline at end of file diff --git a/LibMpcTest/MpdMock.cs b/LibMpcTest/MpdMock.cs index e0935dd..de5b289 100644 --- a/LibMpcTest/MpdMock.cs +++ b/LibMpcTest/MpdMock.cs @@ -32,11 +32,11 @@ namespace LibMpcTest } }; - TestUtils.WriteLine($"Starting Server: {Process.StartInfo.FileName} {Process.StartInfo.Arguments}"); + TestOutput.WriteLine($"Starting Server: {Process.StartInfo.FileName} {Process.StartInfo.Arguments}"); Process.Start(); - TestUtils.WriteLine($"Output: {Process.StandardOutput.ReadToEnd()}"); - TestUtils.WriteLine($"Error: {Process.StandardError.ReadToEnd()}"); + TestOutput.WriteLine($"Output: {Process.StandardOutput.ReadToEnd()}"); + TestOutput.WriteLine($"Error: {Process.StandardError.ReadToEnd()}"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { @@ -80,16 +80,16 @@ namespace LibMpcTest netcat.Start(); netcat.WaitForExit(); - TestUtils.WriteLine(command); - TestUtils.WriteLine($"Output: {netcat.StandardOutput.ReadToEnd()}"); - TestUtils.WriteLine($"Error: {netcat.StandardError.ReadToEnd()}"); + TestOutput.WriteLine(command); + TestOutput.WriteLine($"Output: {netcat.StandardOutput.ReadToEnd()}"); + TestOutput.WriteLine($"Error: {netcat.StandardError.ReadToEnd()}"); } public void Dispose() { Process?.Kill(); Process?.Dispose(); - TestUtils.WriteLine("Server Stopped."); + TestOutput.WriteLine("Server Stopped."); } private class Server diff --git a/LibMpcTest/TestOutput.cs b/LibMpcTest/TestOutput.cs new file mode 100644 index 0000000..bb67d96 --- /dev/null +++ b/LibMpcTest/TestOutput.cs @@ -0,0 +1,12 @@ +using System; + +namespace LibMpcTest +{ + internal class TestOutput + { + internal static void WriteLine(string value) + { + Console.Out.WriteLine(value); + } + } +} \ No newline at end of file diff --git a/LibMpcTest/Tests/DatabaseCommandsTest.cs b/LibMpcTest/Tests/DatabaseCommandsTest.cs index 16b28b8..e8a7c56 100644 --- a/LibMpcTest/Tests/DatabaseCommandsTest.cs +++ b/LibMpcTest/Tests/DatabaseCommandsTest.cs @@ -13,8 +13,8 @@ namespace LibMpcTest { var response = await Mpc.SendAsync(new Commands.Database.ListAll()); - TestUtils.WriteLine("ListAllTest Result:"); - TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + TestOutput.WriteLine("ListAllTest Result:"); + TestOutput.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); Assert.True(response.Response.Body.Count().Equals(7)); } @@ -24,8 +24,8 @@ namespace LibMpcTest { var response = await Mpc.SendAsync(new Commands.Database.Find(MpdTags.Genre, "soundfx")); - TestUtils.WriteLine("FindGenreTest Result:"); - TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + TestOutput.WriteLine("FindGenreTest Result:"); + TestOutput.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); Assert.True(response.Response.Body.Count().Equals(6)); } diff --git a/LibMpcTest/Tests/OutputCommandsTest.cs b/LibMpcTest/Tests/OutputCommandsTest.cs index c46a35b..264c363 100644 --- a/LibMpcTest/Tests/OutputCommandsTest.cs +++ b/LibMpcTest/Tests/OutputCommandsTest.cs @@ -16,8 +16,8 @@ namespace LibMpcTest var response = await Mpc.SendAsync(new Commands.Output.DisableOutput(2)); - TestUtils.WriteLine("DisableOutputTest Result:"); - TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + TestOutput.WriteLine("DisableOutputTest Result:"); + TestOutput.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); Assert.True(response.Response.Body.Equals(string.Empty)); Assert.True(response.Response.State.Status.Equals("OK")); @@ -35,8 +35,8 @@ namespace LibMpcTest var response = await Mpc.SendAsync(new Commands.Output.EnableOutput(1)); - TestUtils.WriteLine("EnableOutputTest Result:"); - TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + TestOutput.WriteLine("EnableOutputTest Result:"); + TestOutput.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); Assert.True(response.Response.Body.Equals(string.Empty)); Assert.True(response.Response.State.Status.Equals("OK")); @@ -45,13 +45,31 @@ namespace LibMpcTest Assert.True(responseOutputs.Response.Body.Single(output => output.Id.Equals(1)).IsEnabled); } + [Fact] + public async Task ToggleOutputTest() + { + 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.ToggleOutput(2)); + + TestOutput.WriteLine("ToggleOutputTest Result:"); + TestOutput.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 LisOutputsTest() { var response = await Mpc.SendAsync(new Commands.Output.Outputs()); - TestUtils.WriteLine("LisOutputsTest Result:"); - TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + TestOutput.WriteLine("LisOutputsTest Result:"); + TestOutput.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); Assert.True(response.Response.Body.Count().Equals(3)); } diff --git a/LibMpcTest/Tests/ReflectionCommandsTest.cs b/LibMpcTest/Tests/ReflectionCommandsTest.cs index 4067373..4f3afe7 100644 --- a/LibMpcTest/Tests/ReflectionCommandsTest.cs +++ b/LibMpcTest/Tests/ReflectionCommandsTest.cs @@ -13,8 +13,8 @@ namespace LibMpcTest { var response = await Mpc.SendAsync(new Commands.Reflection.TagTypes()); - TestUtils.WriteLine("TagTypesTest Result:"); - TestUtils.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + TestOutput.WriteLine("TagTypesTest Result:"); + TestOutput.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); Assert.True(response.Response.Body.Count().Equals(17)); }