mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 02:07:39 +08:00
SortedNotesFolder: Add tests
This way it properly works. I shouldn't be coding so late in the night.
This commit is contained in:
@ -54,11 +54,6 @@ class SortedNotesFolder with NotesFolderNotifier implements NotesFolder {
|
||||
|
||||
void _noteAddedListener(int _, Note note) {
|
||||
assert(folder.notes.length == _notes.length + 1);
|
||||
if (note.loadState != NoteLoadState.Loaded) {
|
||||
_notes.add(note);
|
||||
notifyNoteAdded(_notes.length - 1, note);
|
||||
return;
|
||||
}
|
||||
|
||||
var i = _insertInCorrectPos(note);
|
||||
notifyNoteAdded(i, note);
|
||||
@ -85,9 +80,8 @@ class SortedNotesFolder with NotesFolderNotifier implements NotesFolder {
|
||||
}
|
||||
|
||||
int _insertInCorrectPos(Note note) {
|
||||
var i = _getInsertPos(note, 0, _notes.length);
|
||||
var i = _getInsertPos(note, 0, _notes.length - 1);
|
||||
_notes.insert(i, note);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
@ -100,9 +94,9 @@ class SortedNotesFolder with NotesFolderNotifier implements NotesFolder {
|
||||
if (r == 0) {
|
||||
return mid;
|
||||
} else if (r < 0) {
|
||||
return low = mid + 1;
|
||||
low = mid + 1;
|
||||
} else {
|
||||
return high = mid - 1;
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
95
test/sorted_notes_folder_test.dart
Normal file
95
test/sorted_notes_folder_test.dart
Normal file
@ -0,0 +1,95 @@
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:gitjournal/core/notes_folder_fs.dart';
|
||||
import 'package:gitjournal/core/sorted_notes_folder.dart';
|
||||
import 'package:gitjournal/core/sorting_mode.dart';
|
||||
import 'package:gitjournal/core/note.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('Sorted Notes Folder Test', () {
|
||||
Directory tempDir;
|
||||
NotesFolderFS folder;
|
||||
|
||||
setUp(() async {
|
||||
tempDir = await Directory.systemTemp.createTemp('__sorted_folder_test__');
|
||||
|
||||
folder = NotesFolderFS(null, tempDir.path);
|
||||
|
||||
var random = Random();
|
||||
for (var i = 0; i < 5; i++) {
|
||||
var note = Note(
|
||||
folder,
|
||||
p.join(folder.folderPath, "${random.nextInt(1000)}.md"),
|
||||
);
|
||||
note.modified = DateTime(2020, 1, 10 + (i * 2));
|
||||
note.body = "$i";
|
||||
await note.save();
|
||||
}
|
||||
await folder.loadRecursively();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
tempDir.deleteSync(recursive: true);
|
||||
});
|
||||
|
||||
test('Should load the notes sorted', () async {
|
||||
var sf = SortedNotesFolder(
|
||||
folder: folder,
|
||||
sortingMode: SortingMode.Modified,
|
||||
);
|
||||
expect(sf.hasNotes, true);
|
||||
expect(sf.isEmpty, false);
|
||||
expect(sf.name.startsWith("__sorted_folder_test__"), true);
|
||||
expect(sf.subFolders.length, 0);
|
||||
expect(sf.notes.length, 5);
|
||||
|
||||
expect(sf.notes[0].body, "4");
|
||||
expect(sf.notes[1].body, "3");
|
||||
expect(sf.notes[2].body, "2");
|
||||
expect(sf.notes[3].body, "1");
|
||||
expect(sf.notes[4].body, "0");
|
||||
});
|
||||
|
||||
test('Should on modification remains sorted', () async {
|
||||
var sf = SortedNotesFolder(
|
||||
folder: folder,
|
||||
sortingMode: SortingMode.Modified,
|
||||
);
|
||||
|
||||
var i = sf.notes.indexWhere((n) => n.body == "1");
|
||||
sf.notes[i].modified = DateTime(2020, 2, 1);
|
||||
|
||||
expect(sf.notes[0].body, "1");
|
||||
expect(sf.notes[1].body, "4");
|
||||
expect(sf.notes[2].body, "3");
|
||||
expect(sf.notes[3].body, "2");
|
||||
expect(sf.notes[4].body, "0");
|
||||
});
|
||||
|
||||
test('Should add new note correctly', () async {
|
||||
var sf = SortedNotesFolder(
|
||||
folder: folder,
|
||||
sortingMode: SortingMode.Modified,
|
||||
);
|
||||
|
||||
var note = Note(folder, p.join(folder.folderPath, "new.md"));
|
||||
note.modified = DateTime(2020, 2, 1);
|
||||
note.body = "new";
|
||||
await note.save();
|
||||
|
||||
folder.add(note);
|
||||
|
||||
expect(sf.notes.length, 6);
|
||||
|
||||
expect(sf.notes[0].body, "new");
|
||||
expect(sf.notes[1].body, "4");
|
||||
expect(sf.notes[2].body, "3");
|
||||
expect(sf.notes[3].body, "2");
|
||||
expect(sf.notes[4].body, "1");
|
||||
expect(sf.notes[5].body, "0");
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user