|
| 1 | +import 'package:fast_immutable_collections/fast_immutable_collections.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:common_domain_models/common_domain_models.dart'; |
| 4 | +import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik.dart'; |
| 5 | +import 'package:hausaufgabenheft_logik/src/shared/models/homework.dart'; |
| 6 | +import 'package:hausaufgabenheft_logik/src/shared/color.dart'; |
| 7 | +import 'package:hausaufgabenheft_logik/src/shared/sort_and_subcategorizer.dart'; |
| 8 | +import 'package:hausaufgabenheft_logik/src/shared/homework_section_view.dart'; |
| 9 | + |
| 10 | +class _TestHomework extends BaseHomeworkReadModel { |
| 11 | + const _TestHomework({ |
| 12 | + required super.id, |
| 13 | + required super.title, |
| 14 | + required super.subject, |
| 15 | + required super.courseId, |
| 16 | + required super.withSubmissions, |
| 17 | + required super.todoDate, |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +void main() { |
| 22 | + group('HomeworkSortAndSubcategorizer', () { |
| 23 | + late HomeworkSortAndSubcategorizer subcategorizer; |
| 24 | + final today = Date(year: 2023, month: 10, day: 15); // A Sunday |
| 25 | + |
| 26 | + setUp(() { |
| 27 | + subcategorizer = HomeworkSortAndSubcategorizer( |
| 28 | + getCurrentDate: () => today, |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + final mathSubject = Subject('Math', abbreviation: 'M', color: const Color(0xFF000000)); |
| 33 | + final englishSubject = Subject('English', abbreviation: 'E', color: const Color(0xFF000000)); |
| 34 | + |
| 35 | + final overdueHomework = _TestHomework( |
| 36 | + id: HomeworkId('hw-overdue'), |
| 37 | + title: Title('Overdue HW'), |
| 38 | + subject: mathSubject, |
| 39 | + courseId: CourseId('c-1'), |
| 40 | + withSubmissions: false, |
| 41 | + todoDate: DateTime(2023, 10, 14), // Yesterday |
| 42 | + ); |
| 43 | + |
| 44 | + final todayHomework = _TestHomework( |
| 45 | + id: HomeworkId('hw-today'), |
| 46 | + title: Title('Today HW'), |
| 47 | + subject: englishSubject, |
| 48 | + courseId: CourseId('c-1'), |
| 49 | + withSubmissions: false, |
| 50 | + todoDate: DateTime(2023, 10, 15), // Today |
| 51 | + ); |
| 52 | + |
| 53 | + final tomorrowHomework = _TestHomework( |
| 54 | + id: HomeworkId('hw-tomorrow'), |
| 55 | + title: Title('Tomorrow HW'), |
| 56 | + subject: mathSubject, |
| 57 | + courseId: CourseId('c-1'), |
| 58 | + withSubmissions: false, |
| 59 | + todoDate: DateTime(2023, 10, 16), // Tomorrow |
| 60 | + ); |
| 61 | + |
| 62 | + final in2DaysHomework = _TestHomework( |
| 63 | + id: HomeworkId('hw-in2days'), |
| 64 | + title: Title('In 2 Days HW'), |
| 65 | + subject: englishSubject, |
| 66 | + courseId: CourseId('c-1'), |
| 67 | + withSubmissions: false, |
| 68 | + todoDate: DateTime(2023, 10, 17), // In 2 days |
| 69 | + ); |
| 70 | + |
| 71 | + final futureHomework = _TestHomework( |
| 72 | + id: HomeworkId('hw-future'), |
| 73 | + title: Title('Future HW'), |
| 74 | + subject: mathSubject, |
| 75 | + courseId: CourseId('c-1'), |
| 76 | + withSubmissions: false, |
| 77 | + todoDate: DateTime(2023, 10, 18), // Future |
| 78 | + ); |
| 79 | + |
| 80 | + final homeworks = <BaseHomeworkReadModel>[ |
| 81 | + futureHomework, |
| 82 | + tomorrowHomework, |
| 83 | + todayHomework, |
| 84 | + overdueHomework, |
| 85 | + in2DaysHomework, |
| 86 | + ].toIList(); |
| 87 | + |
| 88 | + test('sortAndSubcategorize SmallestDateSubjectAndTitleSort', () { |
| 89 | + final result = subcategorizer.sortAndSubcategorize( |
| 90 | + homeworks, |
| 91 | + SmallestDateSubjectAndTitleSort(), |
| 92 | + ); |
| 93 | + |
| 94 | + expect(result.length, 5); |
| 95 | + |
| 96 | + expect(result[0].dateSection, equals(HomeworkDateSection.overdue)); |
| 97 | + expect(result[0].homeworks.single, overdueHomework); |
| 98 | + |
| 99 | + expect(result[1].dateSection, equals(HomeworkDateSection.today)); |
| 100 | + expect(result[1].homeworks.single, todayHomework); |
| 101 | + |
| 102 | + expect(result[2].dateSection, equals(HomeworkDateSection.tomorrow)); |
| 103 | + expect(result[2].homeworks.single, tomorrowHomework); |
| 104 | + |
| 105 | + expect(result[3].dateSection, equals(HomeworkDateSection.dayAfterTomorrow)); |
| 106 | + expect(result[3].homeworks.single, in2DaysHomework); |
| 107 | + |
| 108 | + expect(result[4].dateSection, equals(HomeworkDateSection.later)); |
| 109 | + expect(result[4].homeworks.single, futureHomework); |
| 110 | + }); |
| 111 | + |
| 112 | + test('sortAndSubcategorize SubjectSmallestDateAndTitleSort', () { |
| 113 | + final result = subcategorizer.sortAndSubcategorize( |
| 114 | + homeworks, |
| 115 | + SubjectSmallestDateAndTitleSort(), |
| 116 | + ); |
| 117 | + |
| 118 | + expect(result.length, 2); // Math and English |
| 119 | + |
| 120 | + final englishSection = result.firstWhere((s) => s.title == 'English'); |
| 121 | + expect(englishSection.homeworks.length, 2); |
| 122 | + expect(englishSection.homeworks.contains(todayHomework), isTrue); |
| 123 | + expect(englishSection.homeworks.contains(in2DaysHomework), isTrue); |
| 124 | + |
| 125 | + final mathSection = result.firstWhere((s) => s.title == 'Math'); |
| 126 | + expect(mathSection.homeworks.length, 3); |
| 127 | + expect(mathSection.homeworks.contains(overdueHomework), isTrue); |
| 128 | + expect(mathSection.homeworks.contains(tomorrowHomework), isTrue); |
| 129 | + expect(mathSection.homeworks.contains(futureHomework), isTrue); |
| 130 | + }); |
| 131 | + |
| 132 | + test('sortAndSubcategorize WeekdayDateSubjectAndTitleSort', () { |
| 133 | + final result = subcategorizer.sortAndSubcategorize( |
| 134 | + homeworks, |
| 135 | + WeekdayDateSubjectAndTitleSort(), |
| 136 | + ); |
| 137 | + |
| 138 | + // 14th = Sat (6), 15th = Sun (7), 16th = Mon (1), 17th = Tue (2), 18th = Wed (3) |
| 139 | + expect(result.length, 5); |
| 140 | + |
| 141 | + final mondaySection = result.firstWhere((s) => s.weekday == 1); |
| 142 | + expect(mondaySection.homeworks.single, tomorrowHomework); // 16th |
| 143 | + |
| 144 | + final tuesdaySection = result.firstWhere((s) => s.weekday == 2); |
| 145 | + expect(tuesdaySection.homeworks.single, in2DaysHomework); // 17th |
| 146 | + |
| 147 | + final wednesdaySection = result.firstWhere((s) => s.weekday == 3); |
| 148 | + expect(wednesdaySection.homeworks.single, futureHomework); // 18th |
| 149 | + |
| 150 | + final saturdaySection = result.firstWhere((s) => s.weekday == 6); |
| 151 | + expect(saturdaySection.homeworks.single, overdueHomework); // 14th |
| 152 | + |
| 153 | + final sundaySection = result.firstWhere((s) => s.weekday == 7); |
| 154 | + expect(sundaySection.homeworks.single, todayHomework); // 15th |
| 155 | + }); |
| 156 | + }); |
| 157 | +} |
0 commit comments