|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | + |
| 3 | +import '../../tool/create_release.dart'; |
| 4 | + |
| 5 | +typedef Lines = List<String>; |
| 6 | + |
| 7 | +void main() { |
| 8 | + group('extractChangelogForVersion:', () { |
| 9 | + test('extracts content for a specific version', () { |
| 10 | + final content = _joinLines([ |
| 11 | + '# Changelog', |
| 12 | + '', |
| 13 | + '## [Unreleased]', |
| 14 | + '', |
| 15 | + '## [0.10.0] - 2026-02-09', |
| 16 | + '', |
| 17 | + '### Added', |
| 18 | + '- New feature A', |
| 19 | + '- New feature B', |
| 20 | + '', |
| 21 | + '## [0.9.0] - 2026-01-28', |
| 22 | + '', |
| 23 | + '### Fixed', |
| 24 | + '- Bug fix C', |
| 25 | + ]); |
| 26 | + |
| 27 | + final extracted = extractChangelogForVersion(content, '0.10.0'); |
| 28 | + |
| 29 | + expect(extracted, contains('### Added')); |
| 30 | + expect(extracted, contains('- New feature A')); |
| 31 | + expect(extracted, contains('- New feature B')); |
| 32 | + expect(extracted, isNot(contains('### Fixed'))); |
| 33 | + expect(extracted, isNot(contains('- Bug fix C'))); |
| 34 | + }); |
| 35 | + |
| 36 | + test('stops at next header even if version is prefix', () { |
| 37 | + final content = _joinLines([ |
| 38 | + '# Changelog', |
| 39 | + '', |
| 40 | + '## [Unreleased]', |
| 41 | + '', |
| 42 | + '## [0.1.1] - 2026-02-01', |
| 43 | + '- Fix alpha issue', |
| 44 | + '', |
| 45 | + '## [0.1.10] - 2026-02-03', |
| 46 | + '- Add beta feature', |
| 47 | + '', |
| 48 | + '## [0.1.2] - 2026-02-04', |
| 49 | + '- Another fix', |
| 50 | + ]); |
| 51 | + |
| 52 | + final extracted = extractChangelogForVersion(content, '0.1.1'); |
| 53 | + |
| 54 | + expect(extracted, contains('- Fix alpha issue')); |
| 55 | + expect(extracted, isNot(contains('- Add beta feature'))); |
| 56 | + expect(extracted, isNot(contains('- Another fix'))); |
| 57 | + }); |
| 58 | + |
| 59 | + test('throws when version is not found', () { |
| 60 | + final content = _joinLines([ |
| 61 | + '# Changelog', |
| 62 | + '', |
| 63 | + '## [Unreleased]', |
| 64 | + '', |
| 65 | + '## [0.1.0] - 2026-01-01', |
| 66 | + '- Initial release', |
| 67 | + ]); |
| 68 | + |
| 69 | + expect( |
| 70 | + () => extractChangelogForVersion(content, '0.2.0'), |
| 71 | + throwsA(isA<Exception>()), |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + test('removes leading and trailing empty lines', () { |
| 76 | + final content = _joinLines([ |
| 77 | + '## [0.5.0] - 2026-01-01', |
| 78 | + '', |
| 79 | + '', |
| 80 | + '### Added', |
| 81 | + '- Feature X', |
| 82 | + '', |
| 83 | + '', |
| 84 | + '## [0.4.0] - 2025-12-01', |
| 85 | + '- Old stuff', |
| 86 | + ]); |
| 87 | + |
| 88 | + final extracted = extractChangelogForVersion(content, '0.5.0'); |
| 89 | + |
| 90 | + expect(extracted, startsWith('### Added')); |
| 91 | + expect(extracted, endsWith('- Feature X')); |
| 92 | + }); |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +String _joinLines(Lines lines) => lines.join('\n'); |
0 commit comments