MarkdownToolbar: Replace current line

Add a few more tests
This commit is contained in:
Vishesh Handa
2020-08-13 00:44:12 +02:00
parent f3ad0144c5
commit d4b201f7ef
2 changed files with 53 additions and 0 deletions

View File

@ -98,6 +98,8 @@ TextEditingValue modifyCurrentLine(
var lineStartPos = text.lastIndexOf('\n', cursorPos == 0 ? 0 : cursorPos - 1);
if (lineStartPos == -1) {
lineStartPos = 0;
} else {
lineStartPos += 1;
}
var lineEndPos = text.indexOf('\n', cursorPos);

View File

@ -17,4 +17,55 @@ void main() {
expect(modifyCurrentLine(val, '# '), expectedVal);
});
test('Adds a header to the last line correctly', () {
var val = const TextEditingValue(
text: 'Hi\nHello',
selection: TextSelection.collapsed(offset: 8),
);
var expectedVal = const TextEditingValue(
text: 'Hi\n# Hello',
selection: TextSelection.collapsed(offset: 10),
);
expect(modifyCurrentLine(val, '# '), expectedVal);
});
test('Adds a header to a middle line correctly', () {
var val = const TextEditingValue(
text: 'Hi\nHello\nFire',
selection: TextSelection.collapsed(offset: 8),
);
var expectedVal = const TextEditingValue(
text: 'Hi\n# Hello\nFire',
selection: TextSelection.collapsed(offset: 10),
);
expect(modifyCurrentLine(val, '# '), expectedVal);
});
test('Adds a header to a middle line middle word correctly', () {
var val = const TextEditingValue(
text: 'Hi\nHello Darkness\nFire',
selection: TextSelection.collapsed(offset: 8),
);
var expectedVal = const TextEditingValue(
text: 'Hi\n# Hello Darkness\nFire',
selection: TextSelection.collapsed(offset: 10),
);
expect(modifyCurrentLine(val, '# '), expectedVal);
});
// Removes from first line
// Removes from last line
// Removes from middle line
// Removes when cursor is in the middle of a word
// Removes when cursor is at the start of the line
// Removes when cursor is in between '#' and ' '
// TODO: Simplify the tests, avoid all this code duplication
}