Skip to content

Commit

Permalink
Merge pull request #79 from a-givertzman/TextFile-write
Browse files Browse the repository at this point in the history
Text file write
  • Loading branch information
Minyewoo authored Feb 29, 2024
2 parents c298fee + f68917d commit 966dc96
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 39 deletions.
26 changes: 22 additions & 4 deletions lib/src/core/text_file.dart
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.');
}
}
6 changes: 5 additions & 1 deletion test/unit/core/entities/app_settings/fake_text_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import 'package:hmi_core/src/core/text_file.dart';
///
class FakeTextFile implements TextFile {
final String text;
const FakeTextFile(this.text);
final Future<void>? writeFuture;
const FakeTextFile(this.text, {this.writeFuture});
//
@override
Future<String> get content => Future.value(text);
//
@override
Future<void> write(String text) => writeFuture ?? Future(() {return;});
}
20 changes: 20 additions & 0 deletions test/unit/core/text_file/text_file_asset_read_test.dart
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>()));
});
});
}
14 changes: 0 additions & 14 deletions test/unit/core/text_file/text_file_asset_test.dart

This file was deleted.

43 changes: 43 additions & 0 deletions test/unit/core/text_file/text_file_path_read_test.dart
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>()));
});
});
}
20 changes: 0 additions & 20 deletions test/unit/core/text_file/text_file_path_test.dart

This file was deleted.

37 changes: 37 additions & 0 deletions test/unit/core/text_file/text_file_path_write_test.dart
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]));
}
});
}

0 comments on commit 966dc96

Please sign in to comment.