mirror of
https://github.com/ZetaKebab/MpcNET.git
synced 2025-01-14 22:18:43 +00:00
Test for Find command. MpdDirectory and MpdFile few changes.
This commit is contained in:
parent
01b928f29e
commit
7068ac9db0
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LibMpc.Types;
|
||||
|
||||
namespace LibMpc
|
||||
@ -29,21 +30,21 @@ namespace LibMpc
|
||||
{
|
||||
var results = new List<MpdFile>();
|
||||
|
||||
var fileBuilder = new MpdFileBuidler();
|
||||
var mpdFile = MpdFile.EmptyFile;
|
||||
foreach (var line in response)
|
||||
{
|
||||
if (line.Key.Equals("file"))
|
||||
{
|
||||
if (fileBuilder.IsInitialized)
|
||||
if (mpdFile.IsInitialized)
|
||||
{
|
||||
results.Add(fileBuilder.Build());
|
||||
results.Add(mpdFile);
|
||||
}
|
||||
|
||||
fileBuilder.Init(line.Value);
|
||||
mpdFile = new MpdFile(line.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileBuilder.WithProperty(line.Key, line.Value);
|
||||
mpdFile.AddTag(line.Key, line.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,25 +72,27 @@ namespace LibMpc
|
||||
|
||||
// TODO: findadd
|
||||
|
||||
public class ListAll : IMpcCommand<MpdDirectory>
|
||||
public class ListAll : IMpcCommand<IEnumerable<MpdDirectory>>
|
||||
{
|
||||
public string Value => "listall";
|
||||
|
||||
public MpdDirectory FormatResponse(IList<KeyValuePair<string, string>> response)
|
||||
public IEnumerable<MpdDirectory> FormatResponse(IList<KeyValuePair<string, string>> response)
|
||||
{
|
||||
// Add by default the root directory
|
||||
var rootDirectory = new MpdDirectory("/");
|
||||
var rootDirectory = new List<MpdDirectory>
|
||||
{
|
||||
new MpdDirectory("/") // Add by default the root directory
|
||||
};
|
||||
|
||||
foreach (var line in response)
|
||||
{
|
||||
if (line.Key.Equals("file"))
|
||||
{
|
||||
rootDirectory.AddFile(line.Value);
|
||||
rootDirectory.Last().AddFile(line.Value);
|
||||
}
|
||||
|
||||
if (line.Key.Equals("directory"))
|
||||
{
|
||||
rootDirectory.AddDirectory(line.Value);
|
||||
rootDirectory.Add(new MpdDirectory(line.Value));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,6 @@ namespace LibMpc.Types
|
||||
public class MpdDirectory
|
||||
{
|
||||
private readonly IList<MpdFile> _files = new List<MpdFile>();
|
||||
private readonly IList<MpdDirectory> _subDirectories = new List<MpdDirectory>();
|
||||
|
||||
public MpdDirectory(string path)
|
||||
{
|
||||
@ -21,50 +20,10 @@ namespace LibMpc.Types
|
||||
public string Path { get; }
|
||||
public string Name { get; }
|
||||
public IEnumerable<MpdFile> Files => _files;
|
||||
public IEnumerable<MpdDirectory> SubDirectories => _subDirectories;
|
||||
|
||||
internal void AddFile(string file)
|
||||
{
|
||||
var filePath = file.Split('/');
|
||||
var name = filePath[filePath.Length - 1];
|
||||
|
||||
if (filePath.Length == 1)
|
||||
{
|
||||
_files.Add(new MpdFile(name));
|
||||
}
|
||||
else
|
||||
{
|
||||
var filePathWithoutCurrentDirectory = string.Join("/", filePath.Skip(1));
|
||||
foreach (var subDirectory in _subDirectories)
|
||||
{
|
||||
if (subDirectory.Path.Equals(filePath[0]))
|
||||
{
|
||||
subDirectory.AddFile(filePathWithoutCurrentDirectory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void AddDirectory(string directory)
|
||||
{
|
||||
var directoryPath = directory.Split('/');
|
||||
var name = directoryPath[directoryPath.Length - 1];
|
||||
|
||||
if (directoryPath.Length == 1)
|
||||
{
|
||||
_subDirectories.Add(new MpdDirectory(name));
|
||||
}
|
||||
else
|
||||
{
|
||||
var directoryPathWithoutCurrentDirectory = string.Join("/", directoryPath.Skip(1));
|
||||
foreach (var subDirectory in _subDirectories)
|
||||
{
|
||||
if (subDirectory.Path.Equals(directoryPath[0]))
|
||||
{
|
||||
subDirectory.AddDirectory(directoryPathWithoutCurrentDirectory);
|
||||
}
|
||||
}
|
||||
}
|
||||
_files.Add(new MpdFile(file));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,14 @@
|
||||
namespace LibMpc.Types
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LibMpc.Types
|
||||
{
|
||||
public class MpdFileBuidler
|
||||
/// <summary>
|
||||
/// The MpdFile class contains all meta data for a file of the MPD.
|
||||
/// </summary>
|
||||
public class MpdFile
|
||||
{
|
||||
private const string TagFile = "file";
|
||||
public static readonly MpdFile EmptyFile = new MpdFile(string.Empty);
|
||||
|
||||
private const string TagTime = "Time";
|
||||
private const string TagArtist = "Artist";
|
||||
private const string TagAlbum = "Album";
|
||||
@ -18,56 +24,83 @@
|
||||
private const string TagPos = "Pos";
|
||||
private const string TagId = "Id";
|
||||
|
||||
private MpdFile _mpdFile;
|
||||
private readonly IDictionary<string, string> _unknownTags = new Dictionary<string, string>();
|
||||
|
||||
public bool IsInitialized => _mpdFile != null;
|
||||
|
||||
public MpdFileBuidler Init(string file)
|
||||
{
|
||||
_mpdFile = new MpdFile(file);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MpdFileBuidler WithProperty(string tag, string value)
|
||||
{
|
||||
_mpdFile.CheckNotNull();
|
||||
|
||||
// TODO: Parse tag
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public MpdFile Build()
|
||||
{
|
||||
return _mpdFile;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The MpdFile class contains all meta data for a file of the MPD.
|
||||
/// </summary>
|
||||
public class MpdFile
|
||||
{
|
||||
public MpdFile(string file)
|
||||
{
|
||||
File = file;
|
||||
IsInitialized = !string.IsNullOrEmpty(File);
|
||||
}
|
||||
|
||||
public string File { get; }
|
||||
public int Time { get; internal set; } = -1;
|
||||
public string Album { get; internal set; } = string.Empty;
|
||||
public string Artist { get; internal set; } = string.Empty;
|
||||
public string Title { get; internal set; } = string.Empty;
|
||||
public string Track { get; internal set; } = string.Empty;
|
||||
public string Name { get; internal set; } = string.Empty;
|
||||
public string Genre { get; internal set; } = string.Empty;
|
||||
public string Date { get; internal set; } = string.Empty;
|
||||
public string Composer { get; internal set; } = string.Empty;
|
||||
public string Performer { get; internal set; } = string.Empty;
|
||||
public string Comment { get; internal set; } = string.Empty;
|
||||
public int Disc { get; internal set; } = -1;
|
||||
public int Pos { get; internal set; } = -1;
|
||||
public int Id { get; internal set; } = -1;
|
||||
public int Time { get; private set; } = -1;
|
||||
public string Album { get; private set; } = string.Empty;
|
||||
public string Artist { get; private set; } = string.Empty;
|
||||
public string Title { get; private set; } = string.Empty;
|
||||
public string Track { get; private set; } = string.Empty;
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
public string Genre { get; private set; } = string.Empty;
|
||||
public string Date { get; private set; } = string.Empty;
|
||||
public string Composer { get; private set; } = string.Empty;
|
||||
public string Performer { get; private set; } = string.Empty;
|
||||
public string Comment { get; private set; } = string.Empty;
|
||||
public int Disc { get; private set; } = -1;
|
||||
public int Pos { get; private set; } = -1;
|
||||
public int Id { get; private set; } = -1;
|
||||
public IDictionary<string, string> UnknownTags => _unknownTags;
|
||||
|
||||
internal bool IsInitialized { get; }
|
||||
|
||||
internal void AddTag(string tag, string value)
|
||||
{
|
||||
switch (tag)
|
||||
{
|
||||
case TagTime:
|
||||
Time = int.Parse(value);
|
||||
break;
|
||||
case TagArtist:
|
||||
Artist = value;
|
||||
break;
|
||||
case TagAlbum:
|
||||
Album = value;
|
||||
break;
|
||||
case TagTitle:
|
||||
Title = value;
|
||||
break;
|
||||
case TagTrack:
|
||||
Track = value;
|
||||
break;
|
||||
case TagName:
|
||||
Name = value;
|
||||
break;
|
||||
case TagGenre:
|
||||
Genre = value;
|
||||
break;
|
||||
case TagDate:
|
||||
Date = value;
|
||||
break;
|
||||
case TagComposer:
|
||||
Composer = value;
|
||||
break;
|
||||
case TagPerformer:
|
||||
Performer = value;
|
||||
break;
|
||||
case TagComment:
|
||||
Comment = value;
|
||||
break;
|
||||
case TagDisc:
|
||||
Disc = int.Parse(value);
|
||||
break;
|
||||
case TagPos:
|
||||
Pos = int.Parse(value);
|
||||
break;
|
||||
case TagId:
|
||||
Id = int.Parse(value);
|
||||
break;
|
||||
default:
|
||||
_unknownTags.Add(tag, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,8 +53,18 @@ namespace LibMpcTest
|
||||
WriteLine("ListAllTest Result:");
|
||||
WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented));
|
||||
|
||||
Assert.True(response.Response.Body.SubDirectories.Count().Equals(4));
|
||||
Assert.True(response.Response.Body.Files.Count().Equals(3));
|
||||
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()
|
||||
@ -64,11 +74,7 @@ namespace LibMpcTest
|
||||
|
||||
private void WriteLine(string value)
|
||||
{
|
||||
#if DEBUG
|
||||
Debug.WriteLine(value);
|
||||
#else
|
||||
Console.Out.WriteLine(value);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user