Skip to content

Commit

Permalink
Extend PhotoSearchOptions with the support for Styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhornyak committed Nov 1, 2016
1 parent 8d05a41 commit 75e659b
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
25 changes: 25 additions & 0 deletions FlickrNet/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,30 @@ public enum PermissionAddMeta
Everybody = 3
}

/// <summary>
/// An enumeration of photo styles.
/// </summary>
[Serializable]
public enum Style
{
/// <summary>
/// Black and white.
/// </summary>
BlackAndWhite = 0,

/// <summary>
/// Shallow depth of field.
/// </summary>
DepthOfField = 1,

/// <summary>
/// Minimalist.
/// </summary>
Minimalism = 2,

/// <summary>
/// Patterns.
/// </summary>
Pattern = 3
}
}
8 changes: 7 additions & 1 deletion FlickrNet/PhotoSearchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ internal string SortOrderString
/// </remarks>
public ICollection<string> ColorCodes { get; set; }

/// <summary>
/// A collection of styles the search results will be filtered against.
/// </summary>
public ICollection<Style> Styles { get; set; }

/// <summary>
/// Calculates the Uri for a Flash slideshow for the given search options.
/// </summary>
Expand Down Expand Up @@ -452,7 +457,8 @@ public void AddToDictionary(Dictionary<string, string> parameters)
if (FoursquareVenueID != null) parameters.Add("foursquare_venueid", FoursquareVenueID);
if (FoursquareWoeID != null) parameters.Add("foursquare_woeid", FoursquareWoeID);
if (GroupPathAlias != null) parameters.Add("group_path_alias", GroupPathAlias);
if( ColorCodes != null && ColorCodes.Count != 0 ) parameters.Add("color_codes", ColorCodeString);
if (ColorCodes != null && ColorCodes.Count != 0 ) parameters.Add("color_codes", ColorCodeString);
if (Styles != null && Styles.Count != 0) parameters.Add("styles", UtilityMethods.StylesToString(Styles));
}

}
Expand Down
14 changes: 14 additions & 0 deletions FlickrNet/UtilityMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Xml.Serialization;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
#if SILVERLIGHT
using System.Linq;
#endif
Expand Down Expand Up @@ -670,6 +671,19 @@ internal static string EscapeDataString(string value)

return sb.ToString();
}

/// <summary>
/// Converts a collection of <see cref="Style"/> values to a string literal containing the
/// lowercase string representations of each distinct style once, separated by commas.
/// </summary>
/// <param name="styles">Set of styles.</param>
/// <returns>Concatenated styles.</returns>
/// <exception cref="ArgumentNullException"><paramref name="styles"/> is null.</exception>
/// <exception cref="OutOfMemoryException">Out of memory.</exception>
public static string StylesToString(ICollection<Style> styles)
{
return string.Join(",", styles.Distinct().Select(s => s.ToString().ToLower()));
}
}

}
37 changes: 37 additions & 0 deletions FlickrNetTest/PhotoSearchOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

using NUnit.Framework;
using FlickrNet;
using System.Collections.Generic;

namespace FlickrNetTest
{
Expand Down Expand Up @@ -37,5 +38,41 @@ public void PhotoSearchExtrasViews()
Assert.IsTrue(photo.Views.HasValue);
}
}

[Test]
public void StylesNotAddedToParameters_WhenItIsNotSet()
{
var o = new PhotoSearchOptions();
var parameters = new Dictionary<string, string>();

o.AddToDictionary(parameters);

Assert.IsFalse(parameters.ContainsKey("styles"));
}

[Test]
public void StylesNotAddedToParameters_WhenItIsEmpty()
{
var o = new PhotoSearchOptions { Styles = new List<Style>() };
var parameters = new Dictionary<string, string>();

o.AddToDictionary(parameters);

Assert.IsFalse(parameters.ContainsKey("styles"));
}

[TestCase(Style.BlackAndWhite)]
[TestCase(Style.BlackAndWhite, Style.DepthOfField)]
[TestCase(Style.BlackAndWhite, Style.DepthOfField, Style.Minimalism)]
[TestCase(Style.BlackAndWhite, Style.DepthOfField, Style.Minimalism, Style.Pattern)]
public void StylesAddedToParameters_WhenItIsNotNullOrEmpty(params Style[] styles)
{
var o = new PhotoSearchOptions { Styles = new List<Style>(styles) };
var parameters = new Dictionary<string, string>();

o.AddToDictionary(parameters);

Assert.IsTrue(parameters.ContainsKey("styles"));
}
}
}
36 changes: 36 additions & 0 deletions FlickrNetTest/UtilityMethodsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,41 @@ public void UtilityAuthToStringTest()
Assert.AreEqual(string.Empty, b);
}

[Test]
public void StylesToString_ParameterIsNull_ThrowsException()
{
Assert.Throws<ArgumentNullException>(() => UtilityMethods.StylesToString(null));
}

[Test]
public void StyleToString_ParameterIsEmpty_ReturnsEmptyString()
{
Assert.AreEqual(UtilityMethods.StylesToString(new List<Style>()), string.Empty);
}

[TestCase(Style.BlackAndWhite, "blackandwhite")]
[TestCase(Style.DepthOfField, "depthoffield")]
[TestCase(Style.Minimalism, "minimalism")]
[TestCase(Style.Pattern, "pattern")]
public void StylesToString_ConvertsSingletonCollectionIntoString(Style style, string excepted)
{
Assert.AreEqual(UtilityMethods.StylesToString(new[] { style }), excepted);
}

[TestCase("blackandwhite,depthoffield", Style.BlackAndWhite, Style.DepthOfField)]
[TestCase("depthoffield,minimalism,pattern", Style.DepthOfField, Style.Minimalism, Style.Pattern)]
[TestCase("minimalism,pattern,depthoffield,blackandwhite", Style.Minimalism, Style.Pattern, Style.DepthOfField, Style.BlackAndWhite)]
public void StylesToString_SeparatesValuesByComma(string expected, params Style[] styles)
{
Assert.AreEqual(UtilityMethods.StylesToString(styles), expected);
}

[TestCase("blackandwhite", Style.BlackAndWhite, Style.BlackAndWhite)]
[TestCase("blackandwhite,minimalism", Style.BlackAndWhite, Style.BlackAndWhite, Style.Minimalism, Style.Minimalism)]
[TestCase("blackandwhite,pattern,minimalism", Style.BlackAndWhite, Style.BlackAndWhite, Style.Pattern, Style.Minimalism, Style.Minimalism)]
public void StylesToString_FiltersOutRecurrences(string expected, params Style[] styles)
{
Assert.AreEqual(UtilityMethods.StylesToString(styles), expected);
}
}
}

0 comments on commit 75e659b

Please sign in to comment.