Skip to main content

Integration Testing

Sidecar ships with a series of test utilities that make it easy to write integration tests for Sidecar rules.

When writing rule tests, we must create a string containing example code that we expect to trigger or not trigger a lint.

The below snippet shows code that we would expect to show a lint for the avoid_edge_insets_literal rule from design_system_lints. The rule is expected to find values used in an EdgeInsets constructor, and show a lint if that value is not annotated with @designSystem (see design_system_lints for a full explanation of these rules).


const contentEdgeInsetsAll = '''
import 'package:flutter/material.dart';
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(2.0),
);
}
}
''';

In this example, we would expect a lint to be reported underneath the 2.0 value. We can easily test for this, by writing the following rule in the test directory of our rule package:

test/src/avoid_edge_insets_literal_test.dart

void main() {
group('avoid_edge_insets_literal:', () {
setUpRules([AvoidEdgeInsetsLiteral()]);

ruleTest('EdgeInsets.all', contentEdgeInsetsAll, [ExpectedText('2.0')]);

});
}

Before we can run any rule test, we must first use the setUpRules function with the particular rule we're looking to test (here, AvoidEdgeInsetsLiteral). We can then use the ExpectedText test utility to check if a lint is found over the particular code 2.0. We also make the test description EdgeInsets.all, as that's exactly the use case we're testing for.

If we want to test that a lint is found when using a different instantiation of EdgeInsets (e.g. EdgeInsets.only), we can create another test that checks each of the parameters given to EdgeInsets.only (i.e. left, right, top, bottom) by writing 4 different ExpectedText values and providing them to our ruleTest:


import 'package:design_system_lints/design_system_lints.dart';
import 'package:sidecar/test.dart';
import 'package:test/test.dart';


const contentEdgeInsetsOnly = '''
import 'package:flutter/material.dart';
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.only(left: 1.1, right: 1.2, top: 1.3, bottom: 1.4),
);
}
}
''';

void main() {
group('avoid_edge_insets_literal:', () {
setUpRules([AvoidEdgeInsetsLiteral()]);

ruleTest('EdgeInsets.only', contentEdgeInsetsOnly, [
ExpectedText('1.1'),
ExpectedText('1.2'),
ExpectedText('1.3'),
ExpectedText('1.4'),
]);

});
}

Finally, if we write a test where we expect no lints (such as when our EdgeInset values are annotated with @designSystem), we simply simply leave the ExpectedText array empty for our ruleTest:


import 'package:design_system_lints/design_system_lints.dart';
import 'package:sidecar/test.dart';
import 'package:test/test.dart';


const contentDesignSystem = '''
import 'package:design_system_annotations/design_system_annotations.dart';
import 'package:flutter/material.dart';
@designSystem
class DesignSystemX {
static const value = 3.1;
}
final edgeInsets = EdgeInsets.all(DesignSystemX.value);
''';

void main() {
group('avoid_edge_insets_literal:', () {
setUpRules([AvoidEdgeInsetsLiteral()]);

ruleTest('EdgeInsets using Design System', contentDesignSystem, []);

});
}