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

Improve errors and tests for getLocation #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions lib/src/location_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class LocationDatabase {

/// Finds [Location] by its name.
Location get(String name) {
if (!isInitialized) {
// Before you can get a location, you need to manually initialize the
// timezone location database by calling initializeDatabase or similar.
throw LocationNotFoundException(
'Tried to get location before initializing timezone database');
}

final loc = _locations[name];
if (loc == null) {
throw LocationNotFoundException(
Expand Down
9 changes: 9 additions & 0 deletions test/datetime_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,14 @@ Future<void> main() async {
});
});
});
});

group('Timezones', () {
test(
'getLocation throws $LocationNotFoundException for unrecognized timezone',
() {
expect(() => getLocation('non-existent-location'),
throwsA(TypeMatcher<LocationNotFoundException>()));
});
});
}
10 changes: 10 additions & 0 deletions test/datetime_test_no_database.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
@TestOn('vm')
import 'package:test/test.dart';
import 'package:timezone/standalone.dart';
import 'package:timezone/timezone.dart';

void main() {
group('Without initializing timezone database', () {
test('Can still construct TZDateTime.utc', () {
TZDateTime.utc(2019);
});

test(
'getLocation throws $LocationNotFoundException with message about the database not being initialized',
() {
expect(
() => getLocation('America/New_York'),
throwsA(TypeMatcher<LocationNotFoundException>()
.having((e) => e.msg, 'msg', contains('database'))));
});
});
}