Navaron Bracke

let enums throw if raw value is out of bounds

@@ -13,13 +13,14 @@ enum AddressType { @@ -13,13 +13,14 @@ enum AddressType {
13 13
14 factory AddressType.fromRawValue(int value) { 14 factory AddressType.fromRawValue(int value) {
15 switch (value) { 15 switch (value) {
  16 + case 0:
  17 + return AddressType.unknown;
16 case 1: 18 case 1:
17 return AddressType.work; 19 return AddressType.work;
18 case 2: 20 case 2:
19 return AddressType.home; 21 return AddressType.home;
20 - case 0:  
21 default: 22 default:
22 - return AddressType.unknown; 23 + throw ArgumentError.value(value, 'value', 'Invalid raw value.');
23 } 24 }
24 } 25 }
25 26
@@ -49,6 +49,8 @@ enum BarcodeFormat { @@ -49,6 +49,8 @@ enum BarcodeFormat {
49 49
50 factory BarcodeFormat.fromRawValue(int value) { 50 factory BarcodeFormat.fromRawValue(int value) {
51 switch (value) { 51 switch (value) {
  52 + case -1:
  53 + return BarcodeFormat.unknown;
52 case 0: 54 case 0:
53 return BarcodeFormat.all; 55 return BarcodeFormat.all;
54 case 1: 56 case 1:
@@ -77,9 +79,8 @@ enum BarcodeFormat { @@ -77,9 +79,8 @@ enum BarcodeFormat {
77 return BarcodeFormat.pdf417; 79 return BarcodeFormat.pdf417;
78 case 4096: 80 case 4096:
79 return BarcodeFormat.aztec; 81 return BarcodeFormat.aztec;
80 - case -1:  
81 default: 82 default:
82 - return BarcodeFormat.unknown; 83 + throw ArgumentError.value(value, 'value', 'Invalid raw value.');
83 } 84 }
84 } 85 }
85 86
@@ -43,6 +43,8 @@ enum BarcodeType { @@ -43,6 +43,8 @@ enum BarcodeType {
43 43
44 factory BarcodeType.fromRawValue(int value) { 44 factory BarcodeType.fromRawValue(int value) {
45 switch (value) { 45 switch (value) {
  46 + case 0:
  47 + return BarcodeType.unknown;
46 case 1: 48 case 1:
47 return BarcodeType.contactInfo; 49 return BarcodeType.contactInfo;
48 case 2: 50 case 2:
@@ -67,9 +69,8 @@ enum BarcodeType { @@ -67,9 +69,8 @@ enum BarcodeType {
67 return BarcodeType.calendarEvent; 69 return BarcodeType.calendarEvent;
68 case 12: 70 case 12:
69 return BarcodeType.driverLicense; 71 return BarcodeType.driverLicense;
70 - case 0:  
71 default: 72 default:
72 - return BarcodeType.unknown; 73 + throw ArgumentError.value(value, 'value', 'Invalid raw value.');
73 } 74 }
74 } 75 }
75 76
@@ -17,12 +17,12 @@ void main() { @@ -17,12 +17,12 @@ void main() {
17 } 17 }
18 }); 18 });
19 19
20 - test('invalid raw value creates unknown address type', () {  
21 - final AddressType negative = AddressType.fromRawValue(-1);  
22 - final AddressType outOfRange = AddressType.fromRawValue(3); 20 + test('invalid raw value throws argument error', () {
  21 + const int negative = -1;
  22 + const int outOfRange = 3;
23 23
24 - expect(negative, AddressType.unknown);  
25 - expect(outOfRange, AddressType.unknown); 24 + expect(() => AddressType.fromRawValue(negative), throwsArgumentError);
  25 + expect(() => AddressType.fromRawValue(outOfRange), throwsArgumentError);
26 }); 26 });
27 27
28 test('can be converted to raw value', () { 28 test('can be converted to raw value', () {
@@ -29,12 +29,12 @@ void main() { @@ -29,12 +29,12 @@ void main() {
29 } 29 }
30 }); 30 });
31 31
32 - test('invalid raw value creates unknown barcode format', () {  
33 - final BarcodeFormat negative = BarcodeFormat.fromRawValue(-2);  
34 - final BarcodeFormat outOfRange = BarcodeFormat.fromRawValue(4097); 32 + test('invalid raw value throws argument error', () {
  33 + const int negative = -2;
  34 + const int outOfRange = 4097;
35 35
36 - expect(negative, BarcodeFormat.unknown);  
37 - expect(outOfRange, BarcodeFormat.unknown); 36 + expect(() => BarcodeFormat.fromRawValue(negative), throwsArgumentError);
  37 + expect(() => BarcodeFormat.fromRawValue(outOfRange), throwsArgumentError);
38 }); 38 });
39 39
40 test('can be converted to raw value', () { 40 test('can be converted to raw value', () {
@@ -27,12 +27,12 @@ void main() { @@ -27,12 +27,12 @@ void main() {
27 } 27 }
28 }); 28 });
29 29
30 - test('invalid raw value creates unknown barcode type', () {  
31 - final BarcodeType negative = BarcodeType.fromRawValue(-1);  
32 - final BarcodeType outOfRange = BarcodeType.fromRawValue(13); 30 + test('invalid raw value throws argument error', () {
  31 + const int negative = -1;
  32 + const int outOfRange = 13;
33 33
34 - expect(negative, BarcodeType.unknown);  
35 - expect(outOfRange, BarcodeType.unknown); 34 + expect(() => BarcodeType.fromRawValue(negative), throwsArgumentError);
  35 + expect(() => BarcodeType.fromRawValue(outOfRange), throwsArgumentError);
36 }); 36 });
37 37
38 test('can be converted to raw value', () { 38 test('can be converted to raw value', () {
@@ -16,7 +16,7 @@ void main() { @@ -16,7 +16,7 @@ void main() {
16 } 16 }
17 }); 17 });
18 18
19 - test('invalid raw value throws assertion error', () { 19 + test('invalid raw value throws argument error', () {
20 const int negative = -1; 20 const int negative = -1;
21 const int outOfRange = 2; 21 const int outOfRange = 2;
22 22