Skip to content

Commit

Permalink
feat(gql_tristate_link): add abstentWhenNull factory
Browse files Browse the repository at this point in the history
  • Loading branch information
LiLatee authored Feb 23, 2024
1 parent dee7eed commit 44c3686
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 20 deletions.
12 changes: 12 additions & 0 deletions codegen/gql_tristate_value/lib/src/value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ sealed class Value<T extends Object> {
/// If the value is non-null, it will be serialized as the value.
const factory Value.present(T? value) = PresentValue<T>;

/// If the value is non-null, it will be serialized as the value.
/// If the value is null. It will not be serialized.
factory Value.absentWhenNull(T? value) {
if (value == null) {
return const AbsentValue();
}
return Value.present(value);
}

/// Returns the value if present (no matter if null or non-null), otherwise throws a [StateError].
T? get requireValue => switch (this) {
PresentValue(:final value) => value,
Expand Down Expand Up @@ -51,4 +60,7 @@ class PresentValue<T extends Object> extends Value<T> {

@override
int get hashCode => value.hashCode;

@override
String toString() => '$runtimeType(value: $value)';
}
114 changes: 94 additions & 20 deletions codegen/gql_tristate_value/test/value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,107 @@ import 'package:gql_tristate_value/gql_tristate_value.dart';
import 'package:test/test.dart';

void main() {
test("requireValue on absent throws", () {
final absent = const AbsentValue();
group(
'requireValue',
() {
test('on AbsentValue throws', () {
final absent = const AbsentValue();

expect(() => absent.requireValue, throwsA(isA<StateError>()));
});
expect(() => absent.requireValue, throwsA(isA<StateError>()));
});

test("requireValue on present returns value", () {
final present = Value.present(42);
test('on PresentValue with valuereturns value', () {
final present = Value.present(42);

expect(present.requireValue, equals(42));
});
expect(present.requireValue, equals(42));
});

test("requireValue on null returns null", () {
final nullValue = Value<String>.present(null);
test('on PresentValue with null returns null', () {
final nullValue = Value<String>.present(null);

expect(nullValue.requireValue, isNull);
});
expect(nullValue.requireValue, isNull);
});

test("valueOrNull on absent returns null", () {
final absent = const AbsentValue();
test('on Value.ofNullable with value returns value', () {
final present = Value.absentWhenNull(42);

expect(absent.valueOrNull, isNull);
});
expect(present.requireValue, equals(42));
});

test("valueOrNull on present returns value", () {
final present = Value.present(42);
test('on Value.ofNullable with null throws', () {
final nullValue = Value<String>.absentWhenNull(null);

expect(present.valueOrNull, equals(42));
});
expect(() => nullValue.requireValue, throwsA(isA<StateError>()));
});
},
);

group(
'valueOrNull',
() {
test('on AbsentValue returns null', () {
final absent = const AbsentValue();

expect(absent.valueOrNull, isNull);
});

test('on PresentValue with value returns value', () {
final present = Value.present(42);

expect(present.valueOrNull, equals(42));
});

test('on Value.ofNullable with value returns value', () {
final present = Value.absentWhenNull(42);

expect(present.valueOrNull, equals(42));
});

test('on Value.ofNullable with null returns null', () {
final present = Value<String>.absentWhenNull(null);

expect(present.valueOrNull, isNull);
});
},
);

group(
'isPresent',
() {
test('on AbsentValue returns false', () {
final absent = const AbsentValue();

expect(absent.isPresent, false);
});

test('on PresentValue with value returns true', () {
final present = Value.present(42);

expect(present.isPresent, true);
});

test('on Value.ofNullable with value returns true', () {
final present = Value.absentWhenNull(42);

expect(present.isPresent, true);
});

test('on Value.ofNullable with null returns false', () {
final present = Value<String>.absentWhenNull(null);

expect(present.isPresent, false);
});
},
);

group(
'toString',
() {
test('on PresentValue', () {
final present = Value.present(42);

expect(present.toString(), 'PresentValue<int>(value: 42)');
});
},
);
}

0 comments on commit 44c3686

Please sign in to comment.