1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2025-01-14 22:18:43 +00:00

Use only one Client and one Server for all Tests. Output tests.

This commit is contained in:
glucaci 2016-12-20 10:46:40 +01:00
parent 8c3dbecd71
commit 918dc5141a
7 changed files with 164 additions and 79 deletions

View File

@ -26,8 +26,8 @@ namespace LibMpc
public string FormatResponse(IList<KeyValuePair<string, string>> response)
{
// TODO:
return response.ToString();
// Response should be empty.
return string.Join(", ", response);
}
}
@ -47,8 +47,8 @@ namespace LibMpc
public string FormatResponse(IList<KeyValuePair<string, string>> 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));
}

View File

@ -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<MpdServerTest>, IDisposable
public partial class LibMpcTest : IClassFixture<MpdMock>, IClassFixture<MpcMock>
{
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);
}
}
}

26
LibMpcTest/MpcMock.cs Normal file
View File

@ -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.");
}
}
}

View File

@ -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

View File

@ -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));
}
}
}

View File

@ -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));
}
}
}

View File

@ -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));
}
}
}