|
| 1 | +import 'package:flutter_dotenv/flutter_dotenv.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('NotInitializedError', () { |
| 6 | + test('toString contains actionable guidance', () { |
| 7 | + final error = NotInitializedError(); |
| 8 | + expect(error.toString(), contains('NotInitializedError')); |
| 9 | + expect(error.toString(), contains('load()')); |
| 10 | + expect(error.toString(), contains('loadFromString()')); |
| 11 | + }); |
| 12 | + |
| 13 | + test('is thrown when accessing env before initialization', () { |
| 14 | + final env = DotEnv(); |
| 15 | + expect(() => env.env, throwsA(isA<NotInitializedError>())); |
| 16 | + }); |
| 17 | + |
| 18 | + test('thrown error message does not reference global singleton', () { |
| 19 | + final env = DotEnv(); |
| 20 | + try { |
| 21 | + env.env; |
| 22 | + fail('should have thrown'); |
| 23 | + } on NotInitializedError catch (e) { |
| 24 | + expect(e.toString(), contains('load()')); |
| 25 | + expect(e.toString(), isNot(contains('dotenv.load'))); |
| 26 | + } |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + group('FileNotFoundError', () { |
| 31 | + test('toString contains the filename when provided', () { |
| 32 | + final error = FileNotFoundError('.env'); |
| 33 | + expect(error.toString(), contains('FileNotFoundError')); |
| 34 | + expect(error.toString(), contains('.env')); |
| 35 | + expect(error.toString(), contains('pubspec.yaml')); |
| 36 | + }); |
| 37 | + |
| 38 | + test('toString provides generic message when no filename given', () { |
| 39 | + final error = FileNotFoundError(); |
| 40 | + expect(error.toString(), contains('FileNotFoundError')); |
| 41 | + expect(error.toString(), contains('not found')); |
| 42 | + }); |
| 43 | + |
| 44 | + test('filename field is accessible', () { |
| 45 | + expect(FileNotFoundError('.env.production').filename, '.env.production'); |
| 46 | + expect(FileNotFoundError().filename, isNull); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + group('EmptyEnvFileError from loadFromString', () { |
| 51 | + test('thrown error message describes empty string context', () { |
| 52 | + final env = DotEnv(); |
| 53 | + try { |
| 54 | + env.loadFromString(envString: ''); |
| 55 | + fail('should have thrown'); |
| 56 | + } on EmptyEnvFileError catch (e) { |
| 57 | + expect(e.toString(), contains('empty')); |
| 58 | + expect(e.filename, isNull); |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + group('EmptyEnvFileError', () { |
| 64 | + test('toString contains the filename when provided', () { |
| 65 | + final error = EmptyEnvFileError(filename: '.env'); |
| 66 | + expect(error.toString(), contains('EmptyEnvFileError')); |
| 67 | + expect(error.toString(), contains('.env')); |
| 68 | + expect(error.toString(), contains('empty')); |
| 69 | + }); |
| 70 | + |
| 71 | + test('toString provides generic message when no filename given', () { |
| 72 | + final error = EmptyEnvFileError(); |
| 73 | + expect(error.toString(), contains('EmptyEnvFileError')); |
| 74 | + expect(error.toString(), contains('empty')); |
| 75 | + }); |
| 76 | + |
| 77 | + test('filename field is accessible', () { |
| 78 | + expect(EmptyEnvFileError(filename: '.env').filename, '.env'); |
| 79 | + expect(EmptyEnvFileError().filename, isNull); |
| 80 | + }); |
| 81 | + }); |
| 82 | +} |
0 commit comments