forked from aws-amplify/amplify-flutter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(smithy): Proper enum support (aws-amplify#4)
* fix: Proper enum fixes * chore: Update goldens * Add test * Remove orElse from enum helpers * Update goldens
- Loading branch information
Showing
2 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import 'package:smithy/smithy.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class _MyEnum extends SmithyEnum<_MyEnum> { | ||
const _MyEnum._(int index, String name, String value) | ||
: super(index, name, value); | ||
|
||
const _MyEnum._sdkUnknown(String value) : super.sdkUnknown(value); | ||
|
||
static const bar = _MyEnum._(0, 'BAR', 'Bar'); | ||
|
||
static const baz = _MyEnum._(1, 'BAZ', 'Baz'); | ||
|
||
static const foo = _MyEnum._(2, 'FOO', 'Foo'); | ||
|
||
/// All values of [_MyEnum]. | ||
static const values = <_MyEnum>[ | ||
_MyEnum.bar, | ||
_MyEnum.baz, | ||
_MyEnum.foo, | ||
]; | ||
|
||
// ignore: unused_field | ||
static const List<SmithySerializer<_MyEnum>> serializers = [ | ||
SmithyEnumSerializer( | ||
'_MyEnum', | ||
values: values, | ||
sdkUnknown: _MyEnum._sdkUnknown, | ||
supportedProtocols: [], | ||
) | ||
]; | ||
} | ||
|
||
extension _MyEnumHelpers on List<_MyEnum> { | ||
/// Returns the value of [_MyEnum] whose name matches [name]. | ||
/// | ||
/// Throws a `StateError` if no matching value is found. | ||
_MyEnum byName(String name) => | ||
firstWhere((el) => el.name.toLowerCase() == name.toLowerCase()); | ||
|
||
/// Returns the value of [_MyEnum] whose value matches [value]. | ||
_MyEnum byValue(String value) => firstWhere((el) => el.value == value); | ||
} | ||
|
||
void main() { | ||
group('SmithyEnum', () { | ||
test('can switch', () { | ||
_MyEnum getEnum() => _MyEnum.bar; | ||
switch (getEnum()) { | ||
case _MyEnum.bar: | ||
break; | ||
default: | ||
fail('Did not switch correctly'); | ||
} | ||
}); | ||
|
||
test('byName', () { | ||
expect( | ||
_MyEnum.values.byName('foo'), | ||
equals(_MyEnum.foo), | ||
); | ||
}); | ||
|
||
test('byValue', () { | ||
expect( | ||
_MyEnum.values.byValue('Foo'), | ||
equals(_MyEnum.foo), | ||
); | ||
}); | ||
|
||
test('byValue (unknown)', () { | ||
expect( | ||
() => _MyEnum.values.byValue('UNKNOWN'), | ||
throwsStateError, | ||
); | ||
}); | ||
}); | ||
} |