Files
GitJournal/test/checklist_test.dart
Vishesh Handa 657721adc6 Update dart-git and stop using the Result class
Instead we're going to move back to standard exceptions.

Using a custom Result class has created far far more problems
- The Stacktraces aren't always right
- Sometimes one forgets to check the Result error
- All other exception throwing code needing to be converted to Results
- Non idiomatic Dart code

I think it's better to just go back to exceptions. They have their
problems, but overall, I think it's a better approach.
2023-11-24 14:03:30 +01:00

287 lines
8.2 KiB
Dart

/*
* SPDX-FileCopyrightText: 2019-2021 Vishesh Handa <me@vhanda.in>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import 'package:gitjournal/core/checklist.dart';
import 'package:gitjournal/core/file/file.dart';
import 'package:gitjournal/core/file/file_storage.dart';
import 'package:gitjournal/core/folder/notes_folder_config.dart';
import 'package:gitjournal/core/folder/notes_folder_fs.dart';
import 'package:gitjournal/core/note_storage.dart';
import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart';
import 'package:universal_io/io.dart' as io;
import 'lib.dart';
void main() {
setUpAll(gjSetupAllTests);
group('Note', () {
late String repoPath;
late io.Directory tempDir;
late NotesFolderConfig config;
late FileStorage fileStorage;
final gitDt = DateTime.now();
setUpAll(() async {
tempDir = await io.Directory.systemTemp.createTemp('__notes_test__');
repoPath = tempDir.path + p.separator;
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
fileStorage = await FileStorage.fake(repoPath);
});
tearDownAll(() async {
tempDir.deleteSync(recursive: true);
});
test('Should parse simple checklists', () async {
var content = """---
bar: Foo
---
Title 1
How are you doing?
- [ ] item 1
- [x] item 2
- [X] item 3
- [ ] item 4
- [ ] item 5
Booga Wooga
""";
var noteFullPath = p.join(repoPath, "note.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
var items = checklist.items;
expect(items.length, equals(5));
expect(items[0].checked, false);
expect(items[1].checked, true);
expect(items[2].checked, true);
expect(items[3].checked, false);
expect(items[4].checked, false);
expect(items[0].text, "item 1");
expect(items[1].text, "item 2");
expect(items[2].text, "item 3");
expect(items[3].text, "item 4");
expect(items[4].text, "item 5");
//
// Serialization
//
checklist.items[0].checked = true;
checklist.items[1].checked = false;
checklist.items[1].text = "Foo";
var item = checklist.buildItem(false, "Howdy");
checklist.addItem(item);
checklist.removeItem(checklist.items[4]);
var cn = checklist.note
.copyWith(file: checklist.note.file.copyFile(oid: GitHash.zero()));
await NoteStorage.save(cn);
var expectedContent = """---
bar: Foo
---
Title 1
How are you doing?
- [x] item 1
- [ ] Foo
- [X] item 3
- [ ] item 4
- [ ] Howdy
Booga Wooga
""";
var actualContent = io.File(noteFullPath).readAsStringSync();
expect(actualContent, equals(expectedContent));
});
test('Should not add line breaks', () async {
var content = """
- [ ] item 1
- [x] item 2
- [x] item 3""";
var noteFullPath = p.join(repoPath, "note2.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note2.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
var items = checklist.items;
expect(items.length, equals(3));
});
test('Should add \\n before item when adding', () async {
var content = "Hi.";
var noteFullPath = p.join(repoPath, "note3.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note3.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
var items = checklist.items;
expect(items.length, equals(0));
checklist.addItem(checklist.buildItem(false, "item"));
expect(items.length, 1);
note = checklist.note;
expect(note.body, "Hi.\n- [ ] item");
});
test('Should not add \\n when adding after item', () async {
var content = "- [ ] one";
var noteFullPath = p.join(repoPath, "note13.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note13.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
var items = checklist.items;
expect(items.length, equals(1));
checklist.addItem(checklist.buildItem(false, "item"));
note = checklist.note;
expect(note.body, "- [ ] one\n- [ ] item");
});
test('insertItem works', () async {
var content = "Hi.\n- [ ] One\n- Two\n- [ ] Three";
var noteFullPath = p.join(repoPath, "note4.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note4.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
var items = checklist.items;
expect(items.length, 2);
checklist.insertItem(1, checklist.buildItem(false, "item"));
note = checklist.note;
expect(note.body, "Hi.\n- [ ] One\n- Two\n- [ ] item\n- [ ] Three");
});
test('Does not Remove empty trailing items', () async {
var content = "Hi.\n- [ ] One\n- Two\n- [ ] \n- [ ] ";
var noteFullPath = p.join(repoPath, "note4.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note4.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
note = checklist.note;
expect(note.body, "Hi.\n- [ ] One\n- Two\n- [ ] \n- [ ] ");
});
test('Does not add extra new line', () async {
var content = "- [ ] One\n- [ ]Two\n- [ ] Three\n- [ ]Four\n";
var noteFullPath = p.join(repoPath, "note449.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note449.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
checklist.addItem(checklist.buildItem(false, "Five"));
note = checklist.note;
expect(note.body,
"- [ ] One\n- [ ] Two\n- [ ] Three\n- [ ] Four\n- [ ] Five\n");
});
test('Maintain x case', () async {
var content = "- [X] One\n- [ ] Two";
var noteFullPath = p.join(repoPath, "note448.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note448.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
note = checklist.note;
expect(note.body, content);
});
test('Empty Checklist', () async {
var content = "- [X] One\n";
var noteFullPath = p.join(repoPath, "note449.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note449.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
checklist.removeAt(0);
note = checklist.note;
expect(note.body, "\n");
});
test('Checklist Header only', () async {
var content = "#Title\n- [X] One\n";
var noteFullPath = p.join(repoPath, "note429.md");
await io.File(noteFullPath).writeAsString(content);
var parentFolder = NotesFolderFS.root(config, fileStorage);
var file = File.short("note429.md", repoPath, gitDt);
var note = await NoteStorage.load(file, parentFolder);
var checklist = Checklist(note);
checklist.removeAt(0);
note = checklist.note;
expect(note.body, "");
expect(note.title, "Title");
});
});
}