Exemplo n.º 1
0
        public void TestCloningAndSorting()
        {
            IList <SortClass> data = CreateSimpleSortData();

            Sorter <SortClass> sorter = new Sorter <SortClass>();

            sorter.AddProperty(x => x.A, true);
            sorter.AddProperty(x => x.B, false);
            Sorter <SortClass> clonedSorter = sorter.Clone().SortBy(x => x.B, true);

            List <SortClass> sortedData = data.AsQueryable().OrderBy(clonedSorter).ToList();

            Assert.AreSame(data[3], sortedData[0]);
            Assert.AreSame(data[0], sortedData[1]);
            Assert.AreSame(data[2], sortedData[2]);
            Assert.AreSame(data[1], sortedData[3]);
            Assert.AreSame(data[4], sortedData[4]);

            sortedData = data.AsQueryable().OrderBy(sorter).ToList();

            Assert.AreSame(data[3], sortedData[0]);
            Assert.AreSame(data[1], sortedData[1]);
            Assert.AreSame(data[2], sortedData[2]);
            Assert.AreSame(data[0], sortedData[3]);
            Assert.AreSame(data[4], sortedData[4]);
        }
Exemplo n.º 2
0
        public void TestEncodingWithPrefix()
        {
            Sorter <SortClass> sorter = new Sorter <SortClass>("e");

            sorter.AddProperty(x => x.A, true);
            sorter.AddProperty(x => x.B, true);

            Assert.AreEqual("e.A'e.B", sorter.Encode());

            sorter.SortBy(x => x.A, false).SortBy(x => x.B, false);
            Assert.AreEqual("e.B!'e.A!", sorter.Encode());

            sorter.SortBy(x => x.A, true);
            Assert.AreEqual("e.A'e.B!", sorter.Encode());
        }
Exemplo n.º 3
0
        public void TestEncoding()
        {
            Sorter <SortClass> sorter = new Sorter <SortClass>();

            sorter.AddProperty(x => x.A, true);
            sorter.AddProperty(x => x.B, true);

            Assert.AreEqual("A'B", sorter.Encode());

            sorter.SortBy(x => x.A, false).SortBy(x => x.B, false);
            Assert.AreEqual("B!'A!", sorter.Encode());

            sorter.SortBy(x => x.A, true);
            Assert.AreEqual("A'B!", sorter.Encode());
        }
Exemplo n.º 4
0
        public void TestInvalidConfigurations()
        {
            TestHelper.ExpectException <ArgumentException>(() => new Sorter <SortClass>("a.b"));

            Sorter <SortClass> sorter = new Sorter <SortClass>();

            TestHelper.ExpectException <ArgumentException>(() => sorter.AddProperty(s => s.ToString(), true));
            TestHelper.ExpectException <ArgumentException>(() => sorter.AddProperty(s => sorter.OrderedPropertySelectors, true));

            IDictionary <Expression <Func <SortClass, object> >, string> dict = new Dictionary <Expression <Func <SortClass, object> >, string>
            {
                { s => s.ToString(), "0" },
            };

            TestHelper.ExpectException <ArgumentException>(() => new Sorter <SortClass>(dict));
        }
Exemplo n.º 5
0
        public void TestSimpleSortingAfterDeserialization()
        {
            IList <SortClass> data = CreateSimpleSortData();

            Sorter <SortClass> sorter = new Sorter <SortClass>();

            sorter.AddProperty(x => x.A, true);
            sorter.AddProperty(x => x.B, false);

            sorter = SerializeAndDeserializeSorter(sorter);

            List <SortClass> sortedData = data.AsQueryable().OrderBy(sorter).ToList();

            Assert.AreSame(data[3], sortedData[0]);
            Assert.AreSame(data[1], sortedData[1]);
            Assert.AreSame(data[2], sortedData[2]);
            Assert.AreSame(data[0], sortedData[3]);
            Assert.AreSame(data[4], sortedData[4]);
        }
Exemplo n.º 6
0
        public void TestEncodingWithPrefixAndTranslationDictionary()
        {
            IDictionary <Expression <Func <SortClass, object> >, string> translationDictionary = new Dictionary <Expression <Func <SortClass, object> >, string>
            {
                { s => s.A, "0" },
                { s => s.B, "1" },
            };

            Sorter <SortClass> sorter = new Sorter <SortClass>("e", translationDictionary);

            sorter.AddProperty(x => x.A, true);
            sorter.AddProperty(x => x.B, true);

            Assert.AreEqual("e.0'e.1", sorter.Encode());

            sorter.SortBy(x => x.A, false).SortBy(x => x.B, false);
            Assert.AreEqual("e.1!'e.0!", sorter.Encode());

            sorter.SortBy(x => x.A, true);
            Assert.AreEqual("e.0'e.1!", sorter.Encode());
        }