From 89990d5e8180e0a673b8161aed539b8102032a72 Mon Sep 17 00:00:00 2001 From: glucaci Date: Tue, 20 Dec 2016 12:10:13 +0100 Subject: [PATCH] Decoders command + test. Still to Assert response depending on build server. --- LibMpc/Commands/Commands.Reflection.cs | 37 +++- LibMpc/Types/MpdDecoderPlugin.cs | 36 ++++ LibMpc/Types/MpdFile.cs | 202 ++++++++++----------- LibMpcTest/Tests/ReflectionCommandsTest.cs | 11 ++ 4 files changed, 184 insertions(+), 102 deletions(-) create mode 100644 LibMpc/Types/MpdDecoderPlugin.cs diff --git a/LibMpc/Commands/Commands.Reflection.cs b/LibMpc/Commands/Commands.Reflection.cs index 15f6282..5a7ea32 100644 --- a/LibMpc/Commands/Commands.Reflection.cs +++ b/LibMpc/Commands/Commands.Reflection.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using LibMpc.Types; namespace LibMpc { @@ -53,7 +54,41 @@ namespace LibMpc } } - // TODO: decoders + public class Decoders : IMpcCommand> + { + public string Value => "decoders"; + + public IEnumerable FormatResponse(IList> response) + { + var result = new List(); + + var mpdDecoderPlugin = MpdDecoderPlugin.Empty; + foreach (var line in response) + { + if (line.Key.Equals("plugin")) + { + if (mpdDecoderPlugin.IsInitialized) + { + result.Add(mpdDecoderPlugin); + } + + mpdDecoderPlugin = new MpdDecoderPlugin(line.Value); + } + + if (line.Key.Equals("suffix") && mpdDecoderPlugin.IsInitialized) + { + mpdDecoderPlugin.AddSuffix(line.Value); + } + + if (line.Key.Equals("mime_type") && mpdDecoderPlugin.IsInitialized) + { + mpdDecoderPlugin.AddMediaType(line.Value); + } + } + + return result; + } + } } } } diff --git a/LibMpc/Types/MpdDecoderPlugin.cs b/LibMpc/Types/MpdDecoderPlugin.cs new file mode 100644 index 0000000..ede4f43 --- /dev/null +++ b/LibMpc/Types/MpdDecoderPlugin.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; + +namespace LibMpc.Types +{ + public class MpdDecoderPlugin + { + public static readonly MpdDecoderPlugin Empty = new MpdDecoderPlugin(string.Empty); + + private readonly IList _suffixes = new List(); + private readonly IList _mediaTypes = new List(); + + public MpdDecoderPlugin(string name) + { + name.CheckNotNull(); + + Name = name; + IsInitialized = !string.IsNullOrEmpty(name); + } + + public string Name { get; } + public IEnumerable Suffixes => _suffixes; + public IEnumerable MediaTypes => _mediaTypes; + + internal bool IsInitialized { get; } + + internal void AddSuffix(string suffix) + { + _suffixes.Add(suffix); + } + + internal void AddMediaType(string type) + { + _mediaTypes.Add(type); + } + } +} \ No newline at end of file diff --git a/LibMpc/Types/MpdFile.cs b/LibMpc/Types/MpdFile.cs index 12a15c3..c57372f 100644 --- a/LibMpc/Types/MpdFile.cs +++ b/LibMpc/Types/MpdFile.cs @@ -1,106 +1,106 @@ -using System.Collections.Generic; - -namespace LibMpc.Types -{ - /// - /// The MpdFile class contains all meta data for a file of the MPD. - /// - public class MpdFile - { - public static readonly MpdFile EmptyFile = new MpdFile(string.Empty); - - private const string TagTime = "Time"; - private const string TagArtist = "Artist"; - private const string TagAlbum = "Album"; - private const string TagTitle = "Title"; - private const string TagTrack = "Track"; - private const string TagName = "Name"; - private const string TagGenre = "Genre"; - private const string TagDate = "Date"; - private const string TagComposer = "Composer"; - private const string TagPerformer = "Performer"; - private const string TagComment = "Comment"; - private const string TagDisc = "Disc"; - private const string TagPos = "Pos"; - private const string TagId = "Id"; - - private readonly IDictionary _unknownTags = new Dictionary(); - - public MpdFile(string file) - { - File = file; - IsInitialized = !string.IsNullOrEmpty(File); - } - - public string File { get; } - 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 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; +using System.Collections.Generic; + +namespace LibMpc.Types +{ + /// + /// The MpdFile class contains all meta data for a file of the MPD. + /// + public class MpdFile + { + public static readonly MpdFile EmptyFile = new MpdFile(string.Empty); + + private const string TagTime = "Time"; + private const string TagArtist = "Artist"; + private const string TagAlbum = "Album"; + private const string TagTitle = "Title"; + private const string TagTrack = "Track"; + private const string TagName = "Name"; + private const string TagGenre = "Genre"; + private const string TagDate = "Date"; + private const string TagComposer = "Composer"; + private const string TagPerformer = "Performer"; + private const string TagComment = "Comment"; + private const string TagDisc = "Disc"; + private const string TagPos = "Pos"; + private const string TagId = "Id"; + + private readonly IDictionary _unknownTags = new Dictionary(); + + public MpdFile(string file) + { + File = file; + IsInitialized = !string.IsNullOrEmpty(File); + } + + public string File { get; } + 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 UnknownTags => _unknownTags; + + internal bool IsInitialized { get; } + + internal void AddTag(string tag, string value) + { + switch (tag) + { + case TagTime: + Time = int.Parse(value); break; - case TagComment: - Comment = value; + case TagArtist: + Artist = value; break; - case TagDisc: - Disc = int.Parse(value); + case TagAlbum: + Album = value; break; - case TagPos: - Pos = int.Parse(value); + case TagTitle: + Title = value; break; - case TagId: - Id = int.Parse(value); + case TagTrack: + Track = value; break; - default: - _unknownTags.Add(tag, 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; + } + } + } +} diff --git a/LibMpcTest/Tests/ReflectionCommandsTest.cs b/LibMpcTest/Tests/ReflectionCommandsTest.cs index 2818489..91f7879 100644 --- a/LibMpcTest/Tests/ReflectionCommandsTest.cs +++ b/LibMpcTest/Tests/ReflectionCommandsTest.cs @@ -52,5 +52,16 @@ namespace LibMpcTest Assert.True(response.Response.Body.Any(handler => handler.Equals("gopher://"))); Assert.True(response.Response.Body.Any(handler => handler.Equals("rtp://"))); } + + [Fact] + public async Task DecodersTest() + { + var response = await Mpc.SendAsync(new Commands.Reflection.Decoders()); + + TestOutput.WriteLine($"DecodersTest (decoders: {response.Response.Body.Count()}) Result:"); + TestOutput.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented)); + + // TODO: Assert + } } } \ No newline at end of file