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

add scope.get #62

Merged
merged 1 commit into from
Jul 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/src/scopes/errors/scope_value_not_exposed_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

class ScopeValueNotExposedError<T> extends Error {

ScopeValueNotExposedError(Object? name): _name = name;

final Object? _name;

@override
String toString() {
final header = _name == null ? '$T' : '$T $_name';
return '`$header` is not exposed in current scope';
}
}
10 changes: 10 additions & 0 deletions lib/src/scopes/scope_methods/scope_get.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '../errors/scope_value_not_exposed_error.dart';

abstract class ScopeGet {
T? getOrNull<T>({
Expand All @@ -7,3 +8,12 @@ abstract class ScopeGet {
Object? name,
});
}

extension ScopeGetX on ScopeGet {

T get<T>({
Object? name,
}) => has<T>(name: name)
? getOrNull<T>(name: name) as T
: throw ScopeValueNotExposedError<T>(name);
}
1 change: 1 addition & 0 deletions lib/src/scopes/scopes.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

export 'configurables/configurable.dart';
export 'errors/scope_value_not_exposed_error.dart';
export 'scope_methods/scope_get.dart';
export 'scope_methods/scope_expose.dart';
export 'scope_methods/scope_push.dart';
Expand Down
78 changes: 78 additions & 0 deletions test/scopes/scope_methods/scope_get_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'package:test/test.dart';
import 'package:scopes/scopes.dart';

import '../../toolbox/mock_configurable.dart';

void main() {

test('`scope.get` return value if value exposed', () async {

final scope = await Scope.root([
MockConfigurable((scope) {
scope.expose<String>(expose: () => 'a');
}),
]);

final value = scope.get<String>();

expect(value, 'a');

});

test('`scope.get` throws if value not exposed', () async {

final scope = await Scope.root([]);

expect(
() {
scope.get<String>();
},
throwsA(
isA<ScopeValueNotExposedError<String>>()
.having(
(error) => '$error',
'description',
contains('`String` is not exposed in current scope'),
),
)
);

});

test('`scope.get` return value if value exposed with name', () async {

final scope = await Scope.root([
MockConfigurable((scope) {
scope.expose<String>(
name: 'state',
expose: () => 'a',
);
}),
]);

final value = scope.get<String>(name: 'state');

expect(value, 'a');

});

test('`scope.get` throws if value not exposed with name', () async {

final scope = await Scope.root([]);

expect(
() {
scope.get<String>(name: 'state');
},
throwsA(
isA<ScopeValueNotExposedError<String>>()
.having(
(error) => '$error',
'description',
contains('`String state` is not exposed in current scope'),
),
)
);

});
}
2 changes: 2 additions & 0 deletions test/scopes/scopes_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

import 'scope_methods/disposable_test.dart' as disposable_test;
import 'scope_methods/scope_get_test.dart' as scope_get_test;
import 'scope_methods/scope_push_test.dart' as scope_push_test;
import 'scopes/scope_test.dart' as scope_test;

void main() {
disposable_test.main();
scope_get_test.main();
scope_push_test.main();
scope_test.main();
}