Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rrousselGit committed Nov 13, 2023
1 parent f76e77f commit aacc03c
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ import '../../utils.dart';
import 'factory.dart';

void main() {
test('Throws if using notifier properties in its constructor', () {
expect(
CtorNotifier.new,
throwsA(isA<StateError>()),
);
expect(
AutoDisposeCtorNotifier.new,
throwsA(isA<StateError>()),
);
expect(
AutoDisposeFamilyCtorNotifier.new,
throwsA(isA<StateError>()),
);
expect(
FamilyCtorNotifier.new,
throwsA(isA<StateError>()),
);
});

for (final factory in matrix()) {
group(factory.label, () {
test('Can read state inside onDispose', () {
Expand Down Expand Up @@ -490,11 +509,11 @@ void main() {

final sub = container.listen(provider.notifier, (previous, next) {});

// ignore: prefer_const_constructors, not using `const` as we voluntarility break identity to test `identical`
// ignore: prefer_const_constructors, not using `const` as we voluntarily break identity to test `identical`
final newState = AsyncData(84);
// ignore: prefer_const_constructors, not using `const` as we voluntarility break identity to test `identical`
// ignore: prefer_const_constructors, not using `const` as we voluntarily break identity to test `identical`
final newLoading = AsyncLoading<int>();
// ignore: prefer_const_constructors, not using `const` as we voluntarility break identity to test `identical`
// ignore: prefer_const_constructors, not using `const` as we voluntarily break identity to test `identical`
final newError = AsyncError<int>(84, StackTrace.empty);

sub.read().state = newState;
Expand Down Expand Up @@ -736,7 +755,7 @@ void main() {
await expectLater(sub.read().future, completion(1));
});

test('retuns a Future identical to that of .future', () {
test('returns a Future identical to that of .future', () {
final listener = OnBuildMock();
final dep = StateProvider((ref) => 0);
final provider = factory.simpleTestProvider<int>(
Expand Down Expand Up @@ -838,7 +857,7 @@ void main() {
);
});

group('AsyncNotifer.update', () {
group('AsyncNotifier.update', () {
test('passes in the latest state', () async {
final container = createContainer();
final provider = factory.simpleTestProvider<int>(
Expand Down Expand Up @@ -902,8 +921,7 @@ void main() {
expect(container.read(provider), const AsyncData(21));
});

test(
'executes immediately with current state if a state is avalailable',
test('executes immediately with current state if a state is available',
() async {
final container = createContainer();
final provider = factory.simpleTestProvider<int>((ref) => 1);
Expand All @@ -919,8 +937,7 @@ void main() {
expect(container.read(provider), const AsyncData(2));
});

test(
'executes immediately with current state if an error is avalailable',
test('executes immediately with current state if an error is available',
() async {
final container = createContainer();
final provider = factory.simpleTestProvider<int>(
Expand Down Expand Up @@ -1215,3 +1232,40 @@ class Equal<T> {
@override
int get hashCode => Object.hash(runtimeType, value);
}

class CtorNotifier extends AsyncNotifier<int> {
CtorNotifier() {
state;
}

@override
FutureOr<int> build() => 0;
}

class AutoDisposeCtorNotifier extends AutoDisposeAsyncNotifier<int> {
AutoDisposeCtorNotifier() {
state;
}

@override
FutureOr<int> build() => 0;
}

class AutoDisposeFamilyCtorNotifier
extends AutoDisposeFamilyAsyncNotifier<int, int> {
AutoDisposeFamilyCtorNotifier() {
state;
}

@override
FutureOr<int> build(int arg) => 0;
}

class FamilyCtorNotifier extends FamilyAsyncNotifier<int, int> {
FamilyCtorNotifier() {
state;
}

@override
FutureOr<int> build(int arg) => 0;
}
58 changes: 57 additions & 1 deletion packages/riverpod/test/providers/notifier/notifier_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ import '../../utils.dart';
import 'factory.dart';

void main() {
test('Throws if using notifier properties in its constructor', () {
expect(
CtorNotifier.new,
throwsA(isA<StateError>()),
);
expect(
AutoDisposeCtorNotifier.new,
throwsA(isA<StateError>()),
);
expect(
AutoDisposeFamilyCtorNotifier.new,
throwsA(isA<StateError>()),
);
expect(
FamilyCtorNotifier.new,
throwsA(isA<StateError>()),
);
});

for (final factory in matrix()) {
group(factory.label, () {
test('Can read state inside onDispose', () {
Expand Down Expand Up @@ -326,7 +345,7 @@ void main() {
});

test(
'Reading the state inside the notifier rethrows initilization error, if any',
'Reading the state inside the notifier rethrows initialization error, if any',
() {
final provider = factory
.simpleTestProvider<int>((ref) => throw UnimplementedError());
Expand Down Expand Up @@ -719,3 +738,40 @@ class Equal<T> {
@override
int get hashCode => Object.hash(runtimeType, value);
}

class CtorNotifier extends Notifier<int> {
CtorNotifier() {
state;
}

@override
int build() => 0;
}

class AutoDisposeCtorNotifier extends AutoDisposeNotifier<int> {
AutoDisposeCtorNotifier() {
state;
}

@override
int build() => 0;
}

class AutoDisposeFamilyCtorNotifier
extends AutoDisposeFamilyNotifier<int, int> {
AutoDisposeFamilyCtorNotifier() {
state;
}

@override
int build(int arg) => 0;
}

class FamilyCtorNotifier extends FamilyNotifier<int, int> {
FamilyCtorNotifier() {
state;
}

@override
int build(int arg) => 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ import '../../utils.dart';
import 'factory.dart';

void main() {
test('Throws if using notifier properties in its constructor', () {
expect(
CtorNotifier.new,
throwsA(isA<StateError>()),
);
expect(
AutoDisposeCtorNotifier.new,
throwsA(isA<StateError>()),
);
expect(
AutoDisposeFamilyCtorNotifier.new,
throwsA(isA<StateError>()),
);
expect(
FamilyCtorNotifier.new,
throwsA(isA<StateError>()),
);
});

for (final factory in matrix()) {
group(factory.label, () {
test('Can read state inside onDispose', () {
Expand Down Expand Up @@ -391,11 +410,11 @@ void main() {
final sub = container.listen(provider.notifier, (previous, next) {});
await container.read(provider.future);

// ignore: prefer_const_constructors, not using `const` as we voluntarility break identity to test `identical`
// ignore: prefer_const_constructors, not using `const` as we voluntarily break identity to test `identical`
final newState = AsyncData(84);
// ignore: prefer_const_constructors, not using `const` as we voluntarility break identity to test `identical`
// ignore: prefer_const_constructors, not using `const` as we voluntarily break identity to test `identical`
final newLoading = AsyncLoading<int>();
// ignore: prefer_const_constructors, not using `const` as we voluntarility break identity to test `identical`
// ignore: prefer_const_constructors, not using `const` as we voluntarily break identity to test `identical`
final newError = AsyncError<int>(84, StackTrace.empty);

sub.read().state = newState;
Expand Down Expand Up @@ -642,7 +661,7 @@ void main() {
await expectLater(sub.read().future, completion(1));
});

test('retuns a Future identical to that of .future', () {
test('returns a Future identical to that of .future', () {
final listener = OnBuildMock();
final dep = StateProvider((ref) => 0);
final provider = factory.simpleTestProvider<int>(
Expand Down Expand Up @@ -753,7 +772,7 @@ void main() {
);
});

group('AsyncNotifer.update', () {
group('AsyncNotifier.update', () {
test('passes in the latest state', () async {
final container = createContainer();
final provider = factory.simpleTestProvider<int>(
Expand Down Expand Up @@ -820,8 +839,7 @@ void main() {
expect(container.read(provider), const AsyncData(21));
});

test(
'executes immediately with current state if a state is avalailable',
test('executes immediately with current state if a state is available',
() async {
final container = createContainer();
final provider = factory.simpleTestProvider<int>(
Expand All @@ -842,8 +860,7 @@ void main() {
expect(container.read(provider), const AsyncData(2));
});

test(
'executes immediately with current state if an error is avalailable',
test('executes immediately with current state if an error is available',
() async {
final container = createContainer();
final provider = factory.simpleTestProvider<int>(
Expand Down Expand Up @@ -1155,3 +1172,40 @@ class Equal<T> {
@override
int get hashCode => Object.hash(runtimeType, value);
}

class CtorNotifier extends StreamNotifier<int> {
CtorNotifier() {
state;
}

@override
Stream<int> build() => Stream.value(0);
}

class AutoDisposeCtorNotifier extends AutoDisposeStreamNotifier<int> {
AutoDisposeCtorNotifier() {
state;
}

@override
Stream<int> build() => Stream.value(0);
}

class AutoDisposeFamilyCtorNotifier
extends AutoDisposeFamilyStreamNotifier<int, int> {
AutoDisposeFamilyCtorNotifier() {
state;
}

@override
Stream<int> build(int arg) => Stream.value(0);
}

class FamilyCtorNotifier extends FamilyStreamNotifier<int, int> {
FamilyCtorNotifier() {
state;
}

@override
Stream<int> build(int arg) => Stream.value(0);
}

0 comments on commit aacc03c

Please sign in to comment.