Showing
2 changed files
with
47 additions
and
2 deletions
| @@ -16,15 +16,16 @@ enum EncryptionType { | @@ -16,15 +16,16 @@ enum EncryptionType { | ||
| 16 | 16 | ||
| 17 | factory EncryptionType.fromRawValue(int value) { | 17 | factory EncryptionType.fromRawValue(int value) { |
| 18 | switch (value) { | 18 | switch (value) { |
| 19 | + case 0: | ||
| 20 | + return EncryptionType.none; | ||
| 19 | case 1: | 21 | case 1: |
| 20 | return EncryptionType.open; | 22 | return EncryptionType.open; |
| 21 | case 2: | 23 | case 2: |
| 22 | return EncryptionType.wpa; | 24 | return EncryptionType.wpa; |
| 23 | case 3: | 25 | case 3: |
| 24 | return EncryptionType.wep; | 26 | return EncryptionType.wep; |
| 25 | - case 0: | ||
| 26 | default: | 27 | default: |
| 27 | - return EncryptionType.none; | 28 | + throw ArgumentError.value(value, 'value', 'Invalid raw value.'); |
| 28 | } | 29 | } |
| 29 | } | 30 | } |
| 30 | 31 |
test/enums/encryption_type_test.dart
0 → 100644
| 1 | +import 'package:flutter_test/flutter_test.dart'; | ||
| 2 | +import 'package:mobile_scanner/src/enums/encryption_type.dart'; | ||
| 3 | + | ||
| 4 | +void main() { | ||
| 5 | + group('$EncryptionType tests', () { | ||
| 6 | + test('can be created from raw value', () { | ||
| 7 | + const values = <int, EncryptionType>{ | ||
| 8 | + 0: EncryptionType.none, | ||
| 9 | + 1: EncryptionType.open, | ||
| 10 | + 2: EncryptionType.wpa, | ||
| 11 | + 3: EncryptionType.wep, | ||
| 12 | + }; | ||
| 13 | + | ||
| 14 | + for (final MapEntry<int, EncryptionType> entry in values.entries) { | ||
| 15 | + final EncryptionType result = EncryptionType.fromRawValue(entry.key); | ||
| 16 | + | ||
| 17 | + expect(result, entry.value); | ||
| 18 | + } | ||
| 19 | + }); | ||
| 20 | + | ||
| 21 | + test('invalid raw value throws argument error', () { | ||
| 22 | + const int negative = -1; | ||
| 23 | + const int outOfRange = 4; | ||
| 24 | + | ||
| 25 | + expect(() => EncryptionType.fromRawValue(negative), throwsArgumentError); | ||
| 26 | + expect(() => EncryptionType.fromRawValue(outOfRange), throwsArgumentError); | ||
| 27 | + }); | ||
| 28 | + | ||
| 29 | + test('can be converted to raw value', () { | ||
| 30 | + const values = <EncryptionType, int>{ | ||
| 31 | + EncryptionType.none: 0, | ||
| 32 | + EncryptionType.open: 1, | ||
| 33 | + EncryptionType.wpa: 2, | ||
| 34 | + EncryptionType.wep: 3, | ||
| 35 | + }; | ||
| 36 | + | ||
| 37 | + for (final MapEntry<EncryptionType, int> entry in values.entries) { | ||
| 38 | + final int result = entry.key.rawValue; | ||
| 39 | + | ||
| 40 | + expect(result, entry.value); | ||
| 41 | + } | ||
| 42 | + }); | ||
| 43 | + }); | ||
| 44 | +} |
-
Please register or login to post a comment