|
| 1 | +import 'package:flutter/services.dart'; |
| 2 | +import 'package:flutter_code_editor/src/code_field/editor_params.dart'; |
| 3 | +import 'package:flutter_code_editor/src/code_modifiers/insertion.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + test('inserts at the start of string correctly', () { |
| 8 | + const modifier = InsertionCodeModifier(openChar: '1', closeString: '23'); |
| 9 | + const text = 'Hello World'; |
| 10 | + final selection = TextSelection.fromPosition(const TextPosition(offset: 0)); |
| 11 | + const editorParams = EditorParams(); |
| 12 | + |
| 13 | + final result = modifier.updateString(text, selection, editorParams); |
| 14 | + |
| 15 | + expect(result!.text, '123Hello World'); |
| 16 | + expect(result.selection.baseOffset, 1); |
| 17 | + expect(result.selection.extentOffset, 1); |
| 18 | + }); |
| 19 | + |
| 20 | + test('inserts in the middle of string correctly', () { |
| 21 | + const modifier = InsertionCodeModifier(openChar: '1', closeString: '23'); |
| 22 | + const text = 'Hello World'; |
| 23 | + final selection = TextSelection.fromPosition(const TextPosition(offset: 5)); |
| 24 | + const editorParams = EditorParams(); |
| 25 | + |
| 26 | + final result = modifier.updateString(text, selection, editorParams); |
| 27 | + |
| 28 | + expect(result!.text, 'Hello123 World'); |
| 29 | + expect(result.selection.baseOffset, 6); |
| 30 | + expect(result.selection.extentOffset, 6); |
| 31 | + }); |
| 32 | + |
| 33 | + test('inserts at the end of string correctly', () { |
| 34 | + const modifier = InsertionCodeModifier(openChar: '1', closeString: '23'); |
| 35 | + const text = 'Hello World'; |
| 36 | + final selection = |
| 37 | + TextSelection.fromPosition(const TextPosition(offset: text.length)); |
| 38 | + const editorParams = EditorParams(); |
| 39 | + |
| 40 | + final result = modifier.updateString(text, selection, editorParams); |
| 41 | + |
| 42 | + expect(result!.text, 'Hello World123'); |
| 43 | + expect(result.selection.baseOffset, text.length + 1); |
| 44 | + expect(result.selection.extentOffset, text.length + 1); |
| 45 | + }); |
| 46 | + |
| 47 | + test('inserts in the middle of string with selection correctly', () { |
| 48 | + const modifier = InsertionCodeModifier(openChar: '1', closeString: '23'); |
| 49 | + const text = 'Hello World'; |
| 50 | + const selection = TextSelection( |
| 51 | + baseOffset: 5, |
| 52 | + extentOffset: 7, |
| 53 | + ); |
| 54 | + const editorParams = EditorParams(); |
| 55 | + |
| 56 | + final result = modifier.updateString(text, selection, editorParams); |
| 57 | + |
| 58 | + expect(result!.text, 'Hello123orld'); |
| 59 | + expect(result.selection.baseOffset, 6); |
| 60 | + expect(result.selection.extentOffset, 6); |
| 61 | + }); |
| 62 | + |
| 63 | + test('inserts at empty string correctly', () { |
| 64 | + const modifier = InsertionCodeModifier(openChar: '1', closeString: '23'); |
| 65 | + const text = ''; |
| 66 | + final selection = TextSelection.fromPosition(const TextPosition(offset: 0)); |
| 67 | + const editorParams = EditorParams(); |
| 68 | + |
| 69 | + final result = modifier.updateString(text, selection, editorParams); |
| 70 | + |
| 71 | + expect(result!.text, '123'); |
| 72 | + expect(result.selection.baseOffset, 1); |
| 73 | + expect(result.selection.extentOffset, 1); |
| 74 | + }); |
| 75 | +} |
0 commit comments