Пример #1
0
        public static void ComparisonsTest()
        {
            DateOnly dateOnly1 = DateOnly.FromDateTime(DateTime.Today);
            DateOnly dateOnly2 = DateOnly.FromDateTime(DateTime.Today);
            DateOnly dateOnly3 = dateOnly1.AddYears(-10);

            Assert.Equal(0, dateOnly1.CompareTo(dateOnly2));
            Assert.True(dateOnly1.Equals(dateOnly2));
            Assert.True(dateOnly1.Equals((object)dateOnly2));
            Assert.Equal(0, dateOnly2.CompareTo(dateOnly1));
            Assert.True(dateOnly2.Equals(dateOnly1));
            Assert.True(dateOnly2.Equals((object)dateOnly1));
            Assert.Equal(1, dateOnly1.CompareTo(dateOnly3));
            Assert.False(dateOnly1.Equals(dateOnly3));
            Assert.False(dateOnly1.Equals((object)dateOnly3));
            Assert.Equal(-1, dateOnly3.CompareTo(dateOnly1));
            Assert.False(dateOnly3.Equals(dateOnly1));
            Assert.False(dateOnly3.Equals((object)dateOnly1));

            Assert.Equal(0, dateOnly1.CompareTo((object)dateOnly2));
            Assert.Equal(0, dateOnly2.CompareTo((object)dateOnly1));
            Assert.Equal(1, dateOnly1.CompareTo((object)dateOnly3));
            Assert.Equal(-1, dateOnly3.CompareTo((object)dateOnly1));

            Assert.Equal(1, dateOnly1.CompareTo(null));

            Assert.Throws <ArgumentException>(() => dateOnly1.CompareTo(new object()));
            Assert.False(dateOnly3.Equals(new object()));
        }