Navaron Bracke

add PhoneType test

@@ -19,6 +19,8 @@ enum PhoneType { @@ -19,6 +19,8 @@ enum PhoneType {
19 19
20 factory PhoneType.fromRawValue(int value) { 20 factory PhoneType.fromRawValue(int value) {
21 switch (value) { 21 switch (value) {
  22 + case 0:
  23 + return PhoneType.unknown;
22 case 1: 24 case 1:
23 return PhoneType.work; 25 return PhoneType.work;
24 case 2: 26 case 2:
@@ -27,9 +29,8 @@ enum PhoneType { @@ -27,9 +29,8 @@ enum PhoneType {
27 return PhoneType.fax; 29 return PhoneType.fax;
28 case 4: 30 case 4:
29 return PhoneType.mobile; 31 return PhoneType.mobile;
30 - case 0:  
31 default: 32 default:
32 - return PhoneType.unknown; 33 + throw ArgumentError.value(value, 'value', 'Invalid raw value.');
33 } 34 }
34 } 35 }
35 36
  1 +import 'package:flutter_test/flutter_test.dart';
  2 +import 'package:mobile_scanner/src/enums/phone_type.dart';
  3 +
  4 +void main() {
  5 + group('$PhoneType tests', () {
  6 + test('can be created from raw value', () {
  7 + const values = <int, PhoneType>{
  8 + 0: PhoneType.unknown,
  9 + 1: PhoneType.work,
  10 + 2: PhoneType.home,
  11 + 3: PhoneType.fax,
  12 + 4: PhoneType.mobile,
  13 + };
  14 +
  15 + for (final MapEntry<int, PhoneType> entry in values.entries) {
  16 + final PhoneType result = PhoneType.fromRawValue(entry.key);
  17 +
  18 + expect(result, entry.value);
  19 + }
  20 + });
  21 +
  22 + test('invalid raw value throws argument error', () {
  23 + const int negative = -1;
  24 + const int outOfRange = 5;
  25 +
  26 + expect(() => PhoneType.fromRawValue(negative), throwsArgumentError);
  27 + expect(() => PhoneType.fromRawValue(outOfRange), throwsArgumentError);
  28 + });
  29 +
  30 + test('can be converted to raw value', () {
  31 + const values = <PhoneType, int>{
  32 + PhoneType.unknown: 0,
  33 + PhoneType.work: 1,
  34 + PhoneType.home: 2,
  35 + PhoneType.fax: 3,
  36 + PhoneType.mobile: 4,
  37 + };
  38 +
  39 + for (final MapEntry<PhoneType, int> entry in values.entries) {
  40 + final int result = entry.key.rawValue;
  41 +
  42 + expect(result, entry.value);
  43 + }
  44 + });
  45 + });
  46 +}