anonymousMethod method

Method anonymousMethod({
  1. required String widgetName,
  2. required String? builder,
  3. required String goldenName,
  4. required String? theme,
  5. required bool darkTheme,
  6. required String dimension,
})

Generates an anonymous method containing the Golden test body.

Implementation

Method anonymousMethod({
  required String widgetName,
  required String? builder,
  required String goldenName,
  required String? theme,
  required bool darkTheme,
  required String dimension,
}) {
  final width = dimension.split('x')[0];
  final height = dimension.split('x')[1];
  return Method(
    (m) => m
      ..modifier = MethodModifier.async
      ..requiredParameters.add(
        Parameter(
          (p) => p
            ..name = 'tester'
            ..type = refer('WidgetTester'),
        ),
      )
      ..body = Block.of([
        refer('tester')
            .property('view')
            .property('physicalSize')
            .assign(
              refer('Size').constInstance([
                literalNum(double.parse(width)),
                literalNum(double.parse(height)),
              ]),
            )
            .statement,

        /// Pump widget to test
        refer('tester')
            .property('pumpWidget')
            .call([
              refer('MaterialApp').newInstance([], {
                'theme': theme != null
                    ? refer(theme).call([])
                    : refer('ThemeData').newInstance([]),
                'home': builder == null
                    ? refer(widgetName).constInstance([])
                    : refer(builder).call([]),
              }),
            ])
            .awaited
            .statement,

        /// Exoectation
        refer('expectLater')
            .call([
              // find.byType(MyWidget)
              refer('find').property('byType').call([
                refer(widgetName),
              ]),
              // matchesGoldenFile('golden.png')
              refer('matchesGoldenFile').call([
                literalString(
                  finalGoldenName(
                    goldenName,
                    darkTheme,
                    dimension,
                  ),
                ),
              ]),
            ])
            .awaited
            .statement,
      ]),
  );
}