Navaron Bracke

let enums throw if raw value is out of bounds

... ... @@ -13,13 +13,14 @@ enum AddressType {
factory AddressType.fromRawValue(int value) {
switch (value) {
case 0:
return AddressType.unknown;
case 1:
return AddressType.work;
case 2:
return AddressType.home;
case 0:
default:
return AddressType.unknown;
throw ArgumentError.value(value, 'value', 'Invalid raw value.');
}
}
... ...
... ... @@ -49,6 +49,8 @@ enum BarcodeFormat {
factory BarcodeFormat.fromRawValue(int value) {
switch (value) {
case -1:
return BarcodeFormat.unknown;
case 0:
return BarcodeFormat.all;
case 1:
... ... @@ -77,9 +79,8 @@ enum BarcodeFormat {
return BarcodeFormat.pdf417;
case 4096:
return BarcodeFormat.aztec;
case -1:
default:
return BarcodeFormat.unknown;
throw ArgumentError.value(value, 'value', 'Invalid raw value.');
}
}
... ...
... ... @@ -43,6 +43,8 @@ enum BarcodeType {
factory BarcodeType.fromRawValue(int value) {
switch (value) {
case 0:
return BarcodeType.unknown;
case 1:
return BarcodeType.contactInfo;
case 2:
... ... @@ -67,9 +69,8 @@ enum BarcodeType {
return BarcodeType.calendarEvent;
case 12:
return BarcodeType.driverLicense;
case 0:
default:
return BarcodeType.unknown;
throw ArgumentError.value(value, 'value', 'Invalid raw value.');
}
}
... ...
... ... @@ -17,12 +17,12 @@ void main() {
}
});
test('invalid raw value creates unknown address type', () {
final AddressType negative = AddressType.fromRawValue(-1);
final AddressType outOfRange = AddressType.fromRawValue(3);
test('invalid raw value throws argument error', () {
const int negative = -1;
const int outOfRange = 3;
expect(negative, AddressType.unknown);
expect(outOfRange, AddressType.unknown);
expect(() => AddressType.fromRawValue(negative), throwsArgumentError);
expect(() => AddressType.fromRawValue(outOfRange), throwsArgumentError);
});
test('can be converted to raw value', () {
... ...
... ... @@ -29,12 +29,12 @@ void main() {
}
});
test('invalid raw value creates unknown barcode format', () {
final BarcodeFormat negative = BarcodeFormat.fromRawValue(-2);
final BarcodeFormat outOfRange = BarcodeFormat.fromRawValue(4097);
test('invalid raw value throws argument error', () {
const int negative = -2;
const int outOfRange = 4097;
expect(negative, BarcodeFormat.unknown);
expect(outOfRange, BarcodeFormat.unknown);
expect(() => BarcodeFormat.fromRawValue(negative), throwsArgumentError);
expect(() => BarcodeFormat.fromRawValue(outOfRange), throwsArgumentError);
});
test('can be converted to raw value', () {
... ...
... ... @@ -27,12 +27,12 @@ void main() {
}
});
test('invalid raw value creates unknown barcode type', () {
final BarcodeType negative = BarcodeType.fromRawValue(-1);
final BarcodeType outOfRange = BarcodeType.fromRawValue(13);
test('invalid raw value throws argument error', () {
const int negative = -1;
const int outOfRange = 13;
expect(negative, BarcodeType.unknown);
expect(outOfRange, BarcodeType.unknown);
expect(() => BarcodeType.fromRawValue(negative), throwsArgumentError);
expect(() => BarcodeType.fromRawValue(outOfRange), throwsArgumentError);
});
test('can be converted to raw value', () {
... ...
... ... @@ -16,7 +16,7 @@ void main() {
}
});
test('invalid raw value throws assertion error', () {
test('invalid raw value throws argument error', () {
const int negative = -1;
const int outOfRange = 2;
... ...