Test to verify changing dylib opening behavior

This commit is contained in:
Simon Binder 2019-12-25 13:34:08 +01:00
parent c15c4738a6
commit 4910736730
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 28 additions and 0 deletions

View File

@ -1,6 +1,8 @@
import 'dart:ffi';
import 'dart:io';
import 'package:meta/meta.dart';
/// Signature responsible for loading the dynamic sqlite3 library that moor will
/// use.
typedef OpenLibrary = DynamicLibrary Function();
@ -90,6 +92,7 @@ class OpenDynamicLibrary {
_overriddenPlatforms[os] = open;
}
// ignore: use_setters_to_change_properties
/// Makes `moor_ffi` use the [OpenLibrary] function for all Dart platforms.
/// If this method has been called, it takes precedence over [overrideFor].
/// This method must be called before opening any database.
@ -99,4 +102,11 @@ class OpenDynamicLibrary {
void overrideForAll(OpenLibrary open) {
_overriddenForAll = open;
}
/// Clears all associated open helpers for all platforms.
@visibleForTesting
void reset() {
_overriddenForAll = null;
_overriddenPlatforms.clear();
}
}

View File

@ -11,6 +11,7 @@ dependencies:
moor: ">=1.7.0 <3.0.0"
ffi: ^0.1.3
collection: ^1.0.0
meta: ^1.0.2
dev_dependencies:
test: ^1.6.0

View File

@ -0,0 +1,17 @@
import 'package:test/test.dart';
import 'package:moor_ffi/open_helper.dart';
void main() {
tearDown(open.reset);
test('opening behavior can be overridden', () {
var called = false;
open.overrideFor(open.os, () {
called = true;
return null;
});
expect(open.openSqlite(), isNull);
expect(called, isTrue);
});
}