diff --git a/src/UglyToad.PdfPig.Tests/Filters/DecodeParameterResolverTests.cs b/src/UglyToad.PdfPig.Tests/Filters/DecodeParameterResolverTests.cs index c1c7e298..357dd119 100644 --- a/src/UglyToad.PdfPig.Tests/Filters/DecodeParameterResolverTests.cs +++ b/src/UglyToad.PdfPig.Tests/Filters/DecodeParameterResolverTests.cs @@ -31,5 +31,106 @@ Assert.Empty(result.Data); } - } + + [Fact] + public void SingleFilter_ReturnsParameterDictionary() + { + var filter = NameToken.CcittfaxDecode; + var filterParameters = new DictionaryToken(new Dictionary + { + { NameToken.K, new NumericToken(-1) }, + { NameToken.Columns, new NumericToken(1800) }, + { NameToken.Rows, new NumericToken(3113) }, + { NameToken.BlackIs1, BooleanToken.True } + }); + + var dictionary = new Dictionary + { + { NameToken.F, filter }, + { NameToken.DecodeParms, filterParameters } + }; + + var result = DecodeParameterResolver.GetFilterParameters(new DictionaryToken(dictionary), 0); + + Assert.Equal(filterParameters, result); + } + + [Fact] + public void SingleFilter_SpecifiedInArray_ReturnsParameterDictionary() + { + var filter = NameToken.CcittfaxDecode; + var filterParameters = new DictionaryToken(new Dictionary + { + { NameToken.K, new NumericToken(-1) }, + { NameToken.Columns, new NumericToken(1800) }, + { NameToken.Rows, new NumericToken(3113) }, + { NameToken.BlackIs1, BooleanToken.True } + }); + + var dictionary = new Dictionary + { + { NameToken.F, new ArrayToken(new [] { filter }) }, + { NameToken.DecodeParms, new ArrayToken(new [] { filterParameters }) } + }; + + var result = DecodeParameterResolver.GetFilterParameters(new DictionaryToken(dictionary), 0); + + Assert.Equal(filterParameters, result); + } + + [Fact] + public void MultipleFilters_WhenParameterIsNull_ReturnsEmptyDictionary() + { + var filter1 = NameToken.FlateDecode; + var filter1Parameters = NullToken.Instance; + + var filter2 = NameToken.CcittfaxDecode; + var filter2Parameters = new DictionaryToken(new Dictionary + { + { NameToken.K, new NumericToken(-1) }, + { NameToken.Columns, new NumericToken(1800) }, + { NameToken.Rows, new NumericToken(3113) }, + { NameToken.BlackIs1, BooleanToken.True } + + }); + + var dictionary = new Dictionary + { + { NameToken.F, new ArrayToken(new [] { filter1, filter2 }) }, + { NameToken.DecodeParms, new ArrayToken(new IToken[] { filter1Parameters, filter2Parameters }) } + }; + + var result = DecodeParameterResolver.GetFilterParameters(new DictionaryToken(dictionary), 0); + + Assert.Equal(new DictionaryToken(new Dictionary()), result); + } + + [Fact] + public void MultipleFilters_ReturnsParameterDictionary() + { + var filter1 = NameToken.FlateDecode; + var filter1Parameters = NullToken.Instance; + + var filter2 = NameToken.CcittfaxDecode; + var filter2Parameters = new DictionaryToken(new Dictionary + { + { NameToken.K, new NumericToken(-1) }, + { NameToken.Columns, new NumericToken(1800) }, + { NameToken.Rows, new NumericToken(3113) }, + { NameToken.BlackIs1, BooleanToken.True } + }); + + var dictionary = new Dictionary + { + { NameToken.F, new ArrayToken(new [] { filter1, filter2 }) }, + { NameToken.DecodeParms, new ArrayToken(new IToken[] { filter1Parameters, filter2Parameters }) } + }; + + var result = DecodeParameterResolver.GetFilterParameters(new DictionaryToken(dictionary), 1); + + Assert.Equal(filter2Parameters, result); + } + + } } + diff --git a/src/UglyToad.PdfPig.Tokens/DictionaryToken.cs b/src/UglyToad.PdfPig.Tokens/DictionaryToken.cs index f770dd14..391bd6ae 100644 --- a/src/UglyToad.PdfPig.Tokens/DictionaryToken.cs +++ b/src/UglyToad.PdfPig.Tokens/DictionaryToken.cs @@ -8,7 +8,7 @@ /// A dictionary object is an associative table containing pairs of objects, known as the dictionary's entries. /// The key must be a and the value may be an kind of . /// - public class DictionaryToken : IDataToken> + public class DictionaryToken : IDataToken>, IEquatable { /// /// The key value pairs in this dictionary. @@ -123,20 +123,25 @@ return new DictionaryToken(data ?? throw new ArgumentNullException(nameof(data))); } - /// public bool Equals(IToken obj) { - if (ReferenceEquals(this, obj)) + return Equals(obj as DictionaryToken); + } + + /// + public bool Equals(DictionaryToken other) + { + if (other == null) + { + return false; + } + + if (ReferenceEquals(this, other)) { return true; - } - - if (!(obj is DictionaryToken other)) - { - return false; - } - + } + if (Data.Count != other.Data.Count) { return false; @@ -150,13 +155,14 @@ } } - return true; + return true; } /// public override string ToString() { return string.Join(", ", Data.Select(x => $"<{x.Key}, {x.Value}>")); - } + } + } } diff --git a/src/UglyToad.PdfPig/Filters/DecodeParameterResolver.cs b/src/UglyToad.PdfPig/Filters/DecodeParameterResolver.cs index 296f053f..2446abdc 100644 --- a/src/UglyToad.PdfPig/Filters/DecodeParameterResolver.cs +++ b/src/UglyToad.PdfPig/Filters/DecodeParameterResolver.cs @@ -34,7 +34,7 @@ case ArrayToken array: if (parameters is ArrayToken arr) { - if (index < arr.Data.Count && array.Data[index] is DictionaryToken dictionary) + if (index < arr.Data.Count && arr.Data[index] is DictionaryToken dictionary) { return dictionary; }