Skip to content

Commit

Permalink
fix: loading fonts from other packages (#111)
Browse files Browse the repository at this point in the history
* fix: loading fonts from other packages

* fix formatting

* Empty-Commit

* fix: golden_test_adapter test

* fix: golden_test_adapter formatting
  • Loading branch information
krispypen authored Sep 9, 2024
1 parent 14790ef commit f21d072
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
3 changes: 2 additions & 1 deletion lib/src/golden_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Future<void> loadFonts() async {
.map((dynamic x) => x as Map<String, dynamic>);

for (final entry in fontManifest) {
final family = (entry['family'] as String).stripFontFamilyPackageName();
final family =
(entry['family'] as String).stripFontFamilyAlchemistPackageName();

final fontAssets = [
for (final fontAssetEntry in entry['fonts'] as List<dynamic>)
Expand Down
44 changes: 22 additions & 22 deletions lib/src/utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,34 @@ extension GoldenTestThemeDataExtensions on ThemeData {
static const obscuredTextFontFamily = 'Ahem';

/// Strips all text packages from this theme's [ThemeData.textTheme] for use
/// in golden tests using [GoldenTestTextStyleExtensions.stripPackage].
/// in golden tests using [GoldenTestTextStyleExtensions.stripAlchemistPackage].
///
/// Only used internally and should not be used by consumers.
@protected
ThemeData stripTextPackages() {
return copyWith(
textTheme: textTheme.copyWith(
displayLarge: textTheme.displayLarge?.stripPackage(),
displayMedium: textTheme.displayMedium?.stripPackage(),
displaySmall: textTheme.displaySmall?.stripPackage(),
headlineMedium: textTheme.headlineMedium?.stripPackage(),
headlineSmall: textTheme.headlineSmall?.stripPackage(),
titleLarge: textTheme.titleLarge?.stripPackage(),
titleMedium: textTheme.titleMedium?.stripPackage(),
titleSmall: textTheme.titleSmall?.stripPackage(),
bodyLarge: textTheme.bodyLarge?.stripPackage(),
bodyMedium: textTheme.bodyMedium?.stripPackage(),
bodySmall: textTheme.bodySmall?.stripPackage(),
labelLarge: textTheme.labelLarge?.stripPackage(),
labelSmall: textTheme.labelSmall?.stripPackage(),
displayLarge: textTheme.displayLarge?.stripAlchemistPackage(),
displayMedium: textTheme.displayMedium?.stripAlchemistPackage(),
displaySmall: textTheme.displaySmall?.stripAlchemistPackage(),
headlineMedium: textTheme.headlineMedium?.stripAlchemistPackage(),
headlineSmall: textTheme.headlineSmall?.stripAlchemistPackage(),
titleLarge: textTheme.titleLarge?.stripAlchemistPackage(),
titleMedium: textTheme.titleMedium?.stripAlchemistPackage(),
titleSmall: textTheme.titleSmall?.stripAlchemistPackage(),
bodyLarge: textTheme.bodyLarge?.stripAlchemistPackage(),
bodyMedium: textTheme.bodyMedium?.stripAlchemistPackage(),
bodySmall: textTheme.bodySmall?.stripAlchemistPackage(),
labelLarge: textTheme.labelLarge?.stripAlchemistPackage(),
labelSmall: textTheme.labelSmall?.stripAlchemistPackage(),
),
floatingActionButtonTheme: floatingActionButtonTheme.copyWith(
extendedTextStyle:
floatingActionButtonTheme.extendedTextStyle?.stripPackage(),
extendedTextStyle: floatingActionButtonTheme.extendedTextStyle
?.stripAlchemistPackage(),
),
dialogTheme: dialogTheme.copyWith(
titleTextStyle: dialogTheme.titleTextStyle?.stripPackage(),
contentTextStyle: dialogTheme.contentTextStyle?.stripPackage(),
titleTextStyle: dialogTheme.titleTextStyle?.stripAlchemistPackage(),
contentTextStyle: dialogTheme.contentTextStyle?.stripAlchemistPackage(),
),
);
}
Expand Down Expand Up @@ -135,7 +135,7 @@ extension GoldenTestTextStyleExtensions on TextStyle {
/// Strips the package name from this text style's [TextStyle.fontFamily] for
/// use in golden tests.
@protected
TextStyle stripPackage() {
TextStyle stripAlchemistPackage() {
return TextStyle(
inherit: inherit,
color: color,
Expand All @@ -158,7 +158,7 @@ extension GoldenTestTextStyleExtensions on TextStyle {
decorationStyle: decorationStyle,
decorationThickness: decorationThickness,
debugLabel: debugLabel,
fontFamily: fontFamily?.stripFontFamilyPackageName(),
fontFamily: fontFamily?.stripFontFamilyAlchemistPackageName(),
fontFamilyFallback: fontFamilyFallback,
overflow: overflow,
);
Expand All @@ -168,8 +168,8 @@ extension GoldenTestTextStyleExtensions on TextStyle {
/// Strips the package name from the given font family for use in golden tests.
extension FontFamilyStringExtensions on String {
/// Strips the package name from this font family for use in golden tests.
String stripFontFamilyPackageName() {
return replaceAll(RegExp(r'packages\/[^\/]*\/'), '');
String stripFontFamilyAlchemistPackageName() {
return replaceAll(RegExp(r'packages\/alchemist\/'), '');
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/src/golden_test_adapter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,10 @@ void main() {
);

testWidgets(
'has its text packages stripped',
'has alchemist text packages stripped',
(tester) async {
const fontFamilyName = 'fontFamilyName';
const providedFontFamily = 'packages/test_package/$fontFamilyName';
const providedFontFamily = 'packages/alchemist/$fontFamilyName';

await tester.pumpWidget(
Theme(
Expand Down
8 changes: 4 additions & 4 deletions test/src/utilities_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ to avoid unnecessary overhead.''',
group('GoldenTestThemeDataExtensions', () {
test('stripTextPackages remove package prefix from all textTheme styles',
() {
const fontFamilyBefore = 'packages/package_name/dir1/dir2';
const fontFamilyBefore = 'packages/alchemist/dir1/dir2';
const fontFamilyAfter = 'dir1/dir2';

final base = ThemeData();
Expand Down Expand Up @@ -142,12 +142,12 @@ to avoid unnecessary overhead.''',
});

group('GoldenTestTextStyleExtensions', () {
test('stripPackage removes package prefix from style', () {
const fontFamilyBefore = 'packages/package_name/dir1/dir2';
test('stripAlchemistPackage removes package prefix from style', () {
const fontFamilyBefore = 'packages/alchemist/dir1/dir2';
const fontFamilyAfter = 'dir1/dir2';

const styleBefore = TextStyle(fontFamily: fontFamilyBefore);
final styleAfter = styleBefore.stripPackage();
final styleAfter = styleBefore.stripAlchemistPackage();

expect(
styleAfter,
Expand Down

0 comments on commit f21d072

Please sign in to comment.