Exemplo n.º 1
0
        /// <summary>
        /// Sorts the collection in memory.
        /// </summary>
        public GenericListItemCollection Sort(string orderBy)
        {
            GenericListItemCollection         list     = this;
            DynamicComparer <GenericListItem> comparer = new DynamicComparer <GenericListItem>(orderBy);

            list.Sort(comparer.Compare);

            return(list);
        }
Exemplo n.º 2
0
        public void IEqualityComparerEqualsShouldReturnTrueWhenEqual()
        {
            // arrange
            IEqualityComparer target = new DynamicComparer<string>( ( s1, s2 ) => s1.CompareTo( s2 ) );

            // act
            var actual = target.Equals( "test", "test" );

            // assert
            Assert.True( actual );
        }
Exemplo n.º 3
0
        public void EqualsShouldMatchComparison()
        {
            // arrange
            var target = new DynamicComparer<string>( ( s1, s2 ) => s1.CompareTo( s2 ) );

            // act
            var actual = target.Equals( "test", "test" );

            // assert
            Assert.True( actual );
        }
Exemplo n.º 4
0
        public void EqualsShouldMatchHashCode()
        {
            // arrange
            var target = new DynamicComparer<string>( s => s.GetHashCode() );

            // act
            var actual = target.Equals( "test", "test" );

            // assert
            Assert.True( actual );
        }
Exemplo n.º 5
0
        public void TestMultipleDifferentPropertiesPropertiesWithTheSameValues()
        {
            // Prepare
            var objA = new { IdA = 1, NameA = "A" };
            var objB = new { IdB = 1, NameB = "A" };

            // Act
            var actual = DynamicComparer.ArePropertiesEqual(objA, objB);

            // Assert
            Assert.IsFalse(actual);
        }
Exemplo n.º 6
0
        public void TestSinglePropertyWithTheSameValues()
        {
            // Prepare
            var objA = new { Id = 1 };
            var objB = new { Id = 1 };

            // Act
            var actual = DynamicComparer.ArePropertiesEqual(objA, objB);

            // Assert
            Assert.IsTrue(actual);
        }
Exemplo n.º 7
0
        public void TestTwoDifferentSinglePropertiesWithDifferentValues()
        {
            // Prepare
            var objA = new { IdA = 1 };
            var objB = new { IdB = 2 };

            // Act
            var actual = DynamicComparer.ArePropertiesEqual(objA, objB);

            // Assert
            Assert.IsFalse(actual);
        }
Exemplo n.º 8
0
        public void TestMultiplePropertiesWithDifferentValues()
        {
            // Prepare
            var objA = new { Id = 1, Name = "A" };
            var objB = new { Id = 2, Name = "B" };

            // Act
            var actual = DynamicComparer.ArePropertiesEqual(objA, objB);

            // Assert
            Assert.IsTrue(actual);
        }
Exemplo n.º 9
0
        public void GetHashCodeShouldUseCustomMethodWithComparison()
        {
            // arrange
            var value = "test";
            var expected = 1;
            var target = new DynamicComparer<string>( ( s1, s2 ) => s1.CompareTo( s2 ), s => s == "test" ? 1 : s.GetHashCode() );

            // act
            var actual = target.GetHashCode( value );

            // assert
            Assert.Equal( expected, actual );
        }
        //public void SetType(Type type)
        //{
        //    baseComparer = DynamicComparer<object>.CustomComparer(type, Name);
        //}

        public IComparer <object> GetComparer()
        {
            if (Name == null)
            {
                throw new Exception("Name is Null.");
            }
            if (Type == null)
            {
                throw new Exception("Type is Null.");
            }

            baseComparer = DynamicComparer <object> .CustomComparer(Type, Name);

            return(new DynamicComparer <object>((a, b) => {
                var result = baseComparer.Compare(a, b);
                return Order == SortingOrder.Descendant ? -result : result;
            }));
        }
    private void BindList(List <PrivateMessage> privateMessages, string sortExpression)
    {
        if (!string.IsNullOrEmpty(sortExpression))
        {
            SortDirection direction;
            if (grdMessages.Attributes[HtmlAttributeSortDirection] == SortDirection.Ascending.ToString())
            {
                direction = SortDirection.Descending;
            }

            else
            {
                direction = SortDirection.Ascending;
            }

            grdMessages.Attributes[HtmlAttributeSortDirection] = direction.ToString();
            var comparer = new DynamicComparer <PrivateMessage>(sortExpression, direction);
            privateMessages.Sort(comparer.Compare);
        }

        grdMessages.DataSource = privateMessages;
        grdMessages.DataBind();
    }
Exemplo n.º 12
0
        public void WhenValueAndDepthOfTwoItemsAreUnequalTheyShouldYieldDifferentHashCodes()
        {
            var comparer = new DynamicComparer<Tuple<int, string>>( tuple => tuple.Item1.GetHashCode() );
            var command = new Command<string>( Console.WriteLine );
            var item1 = new HierarchicalItem<Tuple<int, string>>( new Tuple<int, string>( 2, "0" ), command, comparer );
            item1.Add( new HierarchicalItem<Tuple<int, string>>( new Tuple<int, string>( 1, "1" ), command, comparer ) );
            var item2 = new HierarchicalItem<Tuple<int, string>>( new Tuple<int, string>( 0, "2" ), command, comparer );
            item1[0].Add( item2 );

            Assert.NotEqual( item1.Depth, item2.Depth );
            Assert.NotEqual( item1.GetHashCode(), item2.GetHashCode() );
        }
Exemplo n.º 13
0
        public void TwoItemsWithTheSameValueAtDifferentDepthsShouldYieldDifferentHashCodes()
        {
            var comparer = new DynamicComparer<Tuple<int, string>>( tuple => tuple.Item1.GetHashCode() );
            var command = new Command<string>( Console.WriteLine );
            var item1 = new HierarchicalItem<Tuple<int, string>>( new Tuple<int, string>( 1, "0" ), command, comparer );
            item1.Add( new HierarchicalItem<Tuple<int, string>>( new Tuple<int, string>( 1, "1" ), command, comparer ) );
            var item2 = new HierarchicalItem<Tuple<int, string>>( new Tuple<int, string>( 1, "2" ), command, comparer );
            item1[0].Add( item2 );

            Assert.Equal( item1.Value.Item1, item2.Value.Item1 );
            Assert.NotEqual( item1.Depth, item2.Depth );
            Assert.NotEqual( item1.GetHashCode(), item2.GetHashCode() );
        }
Exemplo n.º 14
0
        public void IEqualityComparerGetHashCodeShouldReturnExpectedResult()
        {
            // arrage
            var value = "test";
            var expected = 1;
            IEqualityComparer target = new DynamicComparer<string>( s => s == "test" ? 1 : s.GetHashCode() );

            // act
            var actual = target.GetHashCode( value );

            // assert
            Assert.Equal( expected, actual );
        }
Exemplo n.º 15
0
 public void CompareReturnNegativeOneWhenUsingComparisonMethodAndRightSideIsGreater()
 {
     var target = new DynamicComparer<string>( ( s1, s2 ) => s1.Length.CompareTo( s2.Length ) );
     Assert.Equal( -1, target.Compare( "short", "longer" ) );
 }
Exemplo n.º 16
0
        public void IComparerCompareReturnZeroWhenUsingComparisonMethod()
        {
            IComparer t1 = new DynamicComparer<string>( ( s1, s2 ) => s1.CompareTo( s2 ) ); // ref type
            Assert.Equal( 0, t1.Compare( "test", "test" ) );

            IComparer t2 = new DynamicComparer<DateTime>( ( d1, d2 ) => d1.CompareTo( d2 ) ); // value type
            var date = new DateTime( 2013, 6, 27 );
            Assert.Equal( 0, t2.Compare( date, date ) );
        }