|
@@ -0,0 +1,86 @@
|
|
|
+using Invercargill;
|
|
|
+using Invercargill.Convert;
|
|
|
+
|
|
|
+void order_by_tests() {
|
|
|
+
|
|
|
+ Test.add_func("/invercargill/operator/order_by/ascending_int", () => {
|
|
|
+ var items = ClassToOrder.data();
|
|
|
+ var expected_ids = ate(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 });
|
|
|
+
|
|
|
+ var result = items.order_by<int>(i => i.id);
|
|
|
+
|
|
|
+ assert_true(expected_ids.matches(result.select<int>(i => i.id), (a, b) => a == b));
|
|
|
+ });
|
|
|
+
|
|
|
+ Test.add_func("/invercargill/operator/order_by/descending_int", () => {
|
|
|
+ var items = ClassToOrder.data();
|
|
|
+ var expected_ids = ate(new int[] { 8, 7, 6, 5, 4, 3, 2, 1 });
|
|
|
+
|
|
|
+ var result = items.order_by_descending<int>(i => i.id);
|
|
|
+
|
|
|
+ assert_true(expected_ids.matches(result.select<int>(i => i.id), (a, b) => a == b));
|
|
|
+ });
|
|
|
+
|
|
|
+ Test.add_func("/invercargill/operator/order_by/ascending_string", () => {
|
|
|
+ var items = ClassToOrder.data();
|
|
|
+ var expected_ids = ate(new int[] { 2, 4, 8, 3, 7, 6, 1, 5 });
|
|
|
+
|
|
|
+ var result = items.order_by<string>(i => i.name);
|
|
|
+
|
|
|
+ assert_true(expected_ids.matches(result.select<int>(i => i.id), (a, b) => a == b));
|
|
|
+ });
|
|
|
+
|
|
|
+ Test.add_func("/invercargill/operator/order_by/descending_string", () => {
|
|
|
+ var items = ClassToOrder.data();
|
|
|
+ var expected_ids = ate(new int[] { 5, 1, 6, 7, 3, 8, 4, 2 });
|
|
|
+
|
|
|
+ var result = items.order_by_descending<string>(i => i.name);
|
|
|
+
|
|
|
+ assert_true(expected_ids.matches(result.select<int>(i => i.id), (a, b) => a == b));
|
|
|
+ });
|
|
|
+
|
|
|
+ Test.add_func("/invercargill/operator/order_by/complex_priority_birthyear_id", () => {
|
|
|
+ var items = ClassToOrder.data();
|
|
|
+ var expected_ids = ate(new int[] { 5, 7, 3, 2, 1, 4, 6, 8 });
|
|
|
+
|
|
|
+ var result = items.order_by_complex(c => c
|
|
|
+ .order_by<int>(i => i.priority)
|
|
|
+ .then_by_descending<int>(i => i.birth_year)
|
|
|
+ .then_by<int>(i => i.id)
|
|
|
+ );
|
|
|
+ result.debug_dump("ordered", i => @"$(i.id): $(i.name)");
|
|
|
+
|
|
|
+ assert_true(expected_ids.matches(result.select<int>(i => i.id), (a, b) => a == b));
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+class ClassToOrder {
|
|
|
+ public string name { get; set; }
|
|
|
+ public int birth_year { get; set; }
|
|
|
+ public int priority { get; set; }
|
|
|
+ public int id { get; set; }
|
|
|
+
|
|
|
+ public ClassToOrder(string name, int birth_year, int priority, int id) {
|
|
|
+ this.name = name;
|
|
|
+ this.birth_year = birth_year;
|
|
|
+ this.priority = priority;
|
|
|
+ this.id = id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Enumerable<ClassToOrder> data() {
|
|
|
+ var series = new DataStructures.Series<ClassToOrder>();
|
|
|
+ series.add(new ClassToOrder("Bob", 1997, 4, 4));
|
|
|
+ series.add(new ClassToOrder("Alice", 1997, 2, 2));
|
|
|
+ series.add(new ClassToOrder("James", 1999, 4, 1));
|
|
|
+ series.add(new ClassToOrder("Ella", 1999, 2, 3));
|
|
|
+ series.add(new ClassToOrder("Carter", 2000, 7, 8));
|
|
|
+ series.add(new ClassToOrder("Grace", 2000, 7, 6));
|
|
|
+ series.add(new ClassToOrder("Luca", 2000, 1, 5));
|
|
|
+ series.add(new ClassToOrder("Eve", 2000, 2, 7));
|
|
|
+ return series;
|
|
|
+ }
|
|
|
+
|
|
|
+ ~ClassToOrder() {
|
|
|
+ print(@"Bye $(name)\n");
|
|
|
+ }
|
|
|
+}
|