Browse Source

Remove by tag now removes all entry's not just the first. Similar GetValue now returns a list of entrys instead of just one

pull/1174/head
Brian Popow 6 years ago
parent
commit
7e9d553361
  1. 45
      src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs
  2. 49
      tests/ImageSharp.Tests/Metadata/Profiles/IPTC/IptcProfileTests.cs

45
src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs

@ -79,42 +79,67 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Iptc
public IptcProfile DeepClone() => new IptcProfile(this); public IptcProfile DeepClone() => new IptcProfile(this);
/// <summary> /// <summary>
/// Returns the value with the specified tag. /// Returns all value with the specified tag.
/// </summary> /// </summary>
/// <param name="tag">The tag of the iptc value.</param> /// <param name="tag">The tag of the iptc value.</param>
/// <returns>The value with the specified tag.</returns> /// <returns>The values found with the specified tag.</returns>
public IptcValue GetValue(IptcTag tag) public List<IptcValue> GetValues(IptcTag tag)
{ {
var values = new List<IptcValue>();
foreach (IptcValue iptcValue in this.Values) foreach (IptcValue iptcValue in this.Values)
{ {
if (iptcValue.Tag == tag) if (iptcValue.Tag == tag)
{ {
return iptcValue; values.Add(iptcValue);
} }
} }
return null; return values;
} }
/// <summary> /// <summary>
/// Removes the value with the specified tag. /// Removes all values with the specified tag.
/// </summary> /// </summary>
/// <param name="tag">The tag of the iptc value.</param> /// <param name="tag">The tag of the iptc value to remove.</param>
/// <returns>True when the value was found and removed.</returns> /// <returns>True when the value was found and removed.</returns>
public bool RemoveValue(IptcTag tag) public bool RemoveValue(IptcTag tag)
{ {
this.Initialize(); this.Initialize();
for (int i = 0; i < this.values.Count; i++) bool removed = false;
for (int i = this.values.Count - 1; i >= 0; i--)
{ {
if (this.values[i].Tag == tag) if (this.values[i].Tag == tag)
{ {
this.values.RemoveAt(i); this.values.RemoveAt(i);
return true; removed = true;
}
}
return removed;
}
/// <summary>
/// Removes values with the specified tag and value.
/// </summary>
/// <param name="tag">The tag of the iptc value to remove.</param>
/// <param name="value">The value of the iptc item to remove.</param>
/// <returns>True when the value was found and removed.</returns>
public bool RemoveValue(IptcTag tag, string value)
{
this.Initialize();
bool removed = false;
for (int i = this.values.Count - 1; i >= 0; i--)
{
if (this.values[i].Tag == tag && this.values[i].Value.Equals(value))
{
this.values.RemoveAt(i);
removed = true;
} }
} }
return false; return removed;
} }
/// <summary> /// <summary>

49
tests/ImageSharp.Tests/Metadata/Profiles/IPTC/IptcProfileTests.cs

@ -215,6 +215,55 @@ namespace SixLabors.ImageSharp.Tests.Metadata.Profiles.IPTC
ContainsIptcValue(values, tag, expectedValue); ContainsIptcValue(values, tag, expectedValue);
} }
[Fact]
public void IptcProfile_RemoveByTag_RemovesAllEntrys()
{
// arange
var profile = new IptcProfile();
profile.SetValue(IptcTag.Byline, "test");
profile.SetValue(IptcTag.Byline, "test2");
// act
var result = profile.RemoveValue(IptcTag.Byline);
// assert
Assert.True(result, "removed result should be true");
Assert.Empty(profile.Values);
}
[Fact]
public void IptcProfile_RemoveByTagAndValue_Works()
{
// arange
var profile = new IptcProfile();
profile.SetValue(IptcTag.Byline, "test");
profile.SetValue(IptcTag.Byline, "test2");
// act
var result = profile.RemoveValue(IptcTag.Byline, "test2");
// assert
Assert.True(result, "removed result should be true");
ContainsIptcValue(profile.Values, IptcTag.Byline, "test");
}
[Fact]
public void IptcProfile_GetValue_RetrievesAllEntrys()
{
// arange
var profile = new IptcProfile();
profile.SetValue(IptcTag.Byline, "test");
profile.SetValue(IptcTag.Byline, "test2");
profile.SetValue(IptcTag.Caption, "test");
// act
List<IptcValue> result = profile.GetValues(IptcTag.Byline);
// assert
Assert.NotNull(result);
Assert.Equal(2, result.Count);
}
private static void ContainsIptcValue(IEnumerable<IptcValue> values, IptcTag tag, string value) private static void ContainsIptcValue(IEnumerable<IptcValue> values, IptcTag tag, string value)
{ {
Assert.True(values.Any(val => val.Tag == tag), $"Missing iptc tag {tag}"); Assert.True(values.Any(val => val.Tag == tag), $"Missing iptc tag {tag}");

Loading…
Cancel
Save