-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from a-givertzman/TextFile-write
Text file write
- Loading branch information
Showing
7 changed files
with
127 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,43 @@ | ||
import 'dart:io'; | ||
import 'package:flutter/services.dart'; | ||
/// | ||
abstract class TextFile { | ||
/// | ||
/// The interface for reading and writing a text file. | ||
abstract interface class TextFile { | ||
/// Text file from the file system. | ||
/// | ||
/// `filePath` - file system valid relative or full path to the file. | ||
const factory TextFile.path(String filePath) = _PathTextFile; | ||
/// | ||
/// Text file from application asset bundle. | ||
/// | ||
/// `assetPath` - path to the file according to your `assets` section in `pubspec.yaml`. | ||
const factory TextFile.asset(String assetPath) = _AssetTextFile; | ||
/// | ||
/// Internal file contents as text. | ||
Future<String> get content; | ||
/// | ||
/// Rewrites the entire file with provided `text`. | ||
Future<void> write(String text); | ||
} | ||
/// | ||
class _PathTextFile implements TextFile { | ||
final String _filePath; | ||
const _PathTextFile(this._filePath); | ||
// | ||
@override | ||
Future<String> get content => File(_filePath).readAsString(); | ||
// | ||
@override | ||
Future<void> write(String text) => File(_filePath).writeAsString(text, flush: true); | ||
} | ||
/// | ||
class _AssetTextFile implements TextFile { | ||
final String _assetPath; | ||
const _AssetTextFile(this._assetPath); | ||
// | ||
@override | ||
Future<String> get content => rootBundle.loadString(_assetPath); | ||
// | ||
@override | ||
Future<void> write(String text) { | ||
throw UnimplementedError('Unable to write to asset file.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:hmi_core/src/core/text_file.dart'; | ||
|
||
void main() { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
const testAssetPath = 'assets/test/test.txt'; | ||
const originalContent = 'test\nasset\ntext'; | ||
group('TextFile.asset(...).read()', () { | ||
test('reads existing bundle file correctly', () async { | ||
const textFile = TextFile.asset(testAssetPath); | ||
final receivedContent = await textFile.content; | ||
expect(originalContent, equals(receivedContent)); | ||
}); | ||
test('throws error on unexisting bundle file', () async { | ||
const textFile = TextFile.asset('assets/unexisting_asset.txt'); | ||
expect(() async => await textFile.content, throwsA(isA<Error>())); | ||
}); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'dart:io'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:hmi_core/src/core/text_file.dart'; | ||
|
||
void main() { | ||
const testFileNamePrefix = 'text_file_path_write_test'; | ||
const contentToWrite = [ | ||
'first line some test text 12345\nsecond line some test text 12345\n', | ||
'', | ||
'0123456789', | ||
'abcdefghijklmnopqrstuvwxyz', | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
'абвгдеёжзийклмнопрстуфхцчшщъыьэюя', | ||
'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ', | ||
'!@#\$%^&*()_+=-"№;:?./\\|[]{}', | ||
]; | ||
group('TextFile.asset(...).read()', () { | ||
setUpAll(() async { | ||
await Future.wait([ | ||
for(int i=0; i < contentToWrite.length; i++) | ||
File('$testFileNamePrefix-$i.txt').writeAsString(contentToWrite[i]), | ||
]); | ||
}); | ||
tearDownAll(() async { | ||
await Future.wait([ | ||
for(int i=0; i < contentToWrite.length; i++) | ||
File('$testFileNamePrefix-$i.txt').delete(), | ||
]); | ||
}); | ||
test('reads local file correctly', () async { | ||
for(int i=0; i < contentToWrite.length; i++) { | ||
final fileName = '$testFileNamePrefix-$i.txt'; | ||
final textFile = TextFile.path(fileName); | ||
final receivedContent = await textFile.content; | ||
expect(receivedContent, equals(contentToWrite[i])); | ||
} | ||
}); | ||
test('throws error on unexisting file', () async { | ||
const textFile = TextFile.path('unexisting_asset_file.txt'); | ||
expect(() async => await textFile.content, throwsA(isA<PathNotFoundException>())); | ||
}); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'dart:io'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:hmi_core/src/core/text_file.dart'; | ||
|
||
void main() { | ||
const testFileNamePrefix = 'text_file_path_write_test'; | ||
const contentToWrite = [ | ||
'first line some test text 12345\nsecond line some test text 12345\n', | ||
'', | ||
'0123456789', | ||
'abcdefghijklmnopqrstuvwxyz', | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
'абвгдеёжзийклмнопрстуфхцчшщъыьэюя', | ||
'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ', | ||
'!@#\$%^&*()_+=-"№;:?./\\|[]{}', | ||
]; | ||
setUpAll(() async { | ||
await Future.wait([ | ||
for(int i=0; i < contentToWrite.length; i++) | ||
File('$testFileNamePrefix-$i.txt').create(), | ||
]); | ||
}); | ||
tearDownAll(() async { | ||
await Future.wait([ | ||
for(int i=0; i < contentToWrite.length; i++) | ||
File('$testFileNamePrefix-$i.txt').delete(), | ||
]); | ||
}); | ||
test('TextFile.path(...).write() writes local file correctly', () async { | ||
for(int i=0; i < contentToWrite.length; i++) { | ||
final testFilePath = '$testFileNamePrefix-$i.txt'; | ||
final textFile = TextFile.path(testFilePath); | ||
await textFile.write(contentToWrite[i]); | ||
expect(await File(testFilePath).readAsString(), equals(contentToWrite[i])); | ||
} | ||
}); | ||
} |