Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localised | StringLocalize #88

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/src/translate/localized.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'localizations.dart';

///
/// Translates [text] to the current application language
class Localized {
Expand All @@ -15,3 +14,14 @@ class Localized {
/// - returns translation for current lang
String get v => toString();
}
///
/// Simple access to the localised
/// ```
/// ```
extension StringLocalize on String {
///
/// Returns translated version of given `string`
String get loc {
return Localizations.getTranslation(this);
}
}
32 changes: 17 additions & 15 deletions test/unit/core/result/result_constructor_test.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
// ignore_for_file: deprecated_member_use_from_same_package
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result_old.dart';
import 'package:hmi_core/hmi_core_result.dart';

import 'test_data.dart';

void main() {
group('Result constructor', () {
test('asserts if no args', () {
expect(() => Result(), throwsA(isA<AssertionError>()));
});
test('completes if data is provided', () {
expect(() => const Result(data: 'test'), returnsNormally);
group('Result constructor of', () {
test('Ok completes if data is provided', () {
for(final value in testData) {
expect(() => Ok(value), returnsNormally);
}
});
test('completes if error is provided', () {
expect(
() => Result(
error: Failure(message: '', stackTrace: StackTrace.current),
),
returnsNormally,
);
test('Err completes if error is provided', () {
for(final value in testData) {
expect(
() => Err(
Failure(message: value, stackTrace: StackTrace.current),
),
returnsNormally,
);
}
});
});
}
70 changes: 49 additions & 21 deletions test/unit/core/result/result_fold_test.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result_old.dart';
import 'package:hmi_core/hmi_core_result.dart';
import 'test_data.dart';

void main() {
group('Result fold', () {
const resultWithData = Result(data: 'test');
final resultWithError = Result(
error: Failure(message: '', stackTrace: StackTrace.current),
);
final resultWithDataAndError = Result(
data: 'test',
error: Failure(message: '', stackTrace: StackTrace.current),
);
const valueIfData = 1;
const valueIfError = 0;
int onData<T>(T data) => valueIfData;
int onError(Failure error) => valueIfError;
test('calls onData if data is provided', () {
expect(resultWithData.fold(onData: onData, onError: onError), valueIfData);
test('matches Ok if data is provided', () {
for (final value in testData) {
final Result<dynamic, Failure> result = Ok(value);
final foldResult = switch(result) {
Ok(:final value) => value,
Err(:final error) => error,
};
expect(foldResult, value);
}
});
test('matches Err if no data', () {
for (final error in testData) {
final Result result = Err(error);
final foldResult = switch(result) {
Ok(:final value) => value,
Err(:final error) => error,
};
expect(foldResult, error);
}
});
test('calls onError if no data', () {
expect(resultWithError.fold(onData: onData, onError: onError), valueIfError);
test('works perfectly even if data is void', () {
for (final value in testData) {
// ignore: void_checks
final Result<void, Failure> result = Ok(value);
expect(
() {
return switch(result) {
Ok(:final value) => value,
Err(:final error) => error,
};
},
returnsNormally,
);
}
});
test('calls onData if both data and error are provided', () {
expect(resultWithDataAndError.fold(onData: onData, onError: onError), valueIfData);
test('works perfectly even if error is void', () {
for (final error in testData) {
// ignore: void_checks
final Result<dynamic, void> result = Err(error);
expect(
() {
return switch(result) {
Ok(:final value) => value,
Err(:final error) => error,
};
},
returnsNormally,
);
}
});
});
}
25 changes: 0 additions & 25 deletions test/unit/core/result_new/new_result_constructor_test.dart

This file was deleted.

59 changes: 0 additions & 59 deletions test/unit/core/result_new/new_result_fold_test.dart

This file was deleted.

23 changes: 23 additions & 0 deletions test/unit/core/result_old/result_constructor_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ignore_for_file: deprecated_member_use_from_same_package
import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result_old.dart';

void main() {
group('Result constructor', () {
test('asserts if no args', () {
expect(() => Result(), throwsA(isA<AssertionError>()));
});
test('completes if data is provided', () {
expect(() => const Result(data: 'test'), returnsNormally);
});
test('completes if error is provided', () {
expect(
() => Result(
error: Failure(message: '', stackTrace: StackTrace.current),
),
returnsNormally,
);
});
});
}
31 changes: 31 additions & 0 deletions test/unit/core/result_old/result_fold_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter_test/flutter_test.dart';
import 'package:hmi_core/hmi_core_failure.dart';
import 'package:hmi_core/hmi_core_result_old.dart';

void main() {
group('Result fold', () {
const resultWithData = Result(data: 'test');
final resultWithError = Result(
error: Failure(message: '', stackTrace: StackTrace.current),
);
final resultWithDataAndError = Result(
data: 'test',
error: Failure(message: '', stackTrace: StackTrace.current),
);
const valueIfData = 1;
const valueIfError = 0;
int onData<T>(T data) => valueIfData;
int onError(Failure error) => valueIfError;
test('calls onData if data is provided', () {
expect(resultWithData.fold(onData: onData, onError: onError), valueIfData);
});
test('calls onError if no data', () {
expect(resultWithError.fold(onData: onData, onError: onError), valueIfError);
});
test('calls onData if both data and error are provided', () {
expect(resultWithDataAndError.fold(onData: onData, onError: onError), valueIfData);
});
});
}
Loading