Skip to content

Commit

Permalink
fix: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
marwfair committed Sep 16, 2024
1 parent b2e070a commit 4e6aadd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 79 deletions.
1 change: 0 additions & 1 deletion src/content/docs/engineering/philosophy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ mappedStream.listen((value) {
print(value);
});
}
```

:::caution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Error handling
description: Error handling best practices.
---

import { FileTree, TabItem, Tabs } from "@astrojs/starlight/components";

## Document when a call may throw

Document exceptions associated with calling a function in its documentation comments to help understand when an exception might be thrown.
Expand Down Expand Up @@ -47,55 +49,52 @@ By creating custom exceptions, developers can provide more meaningful error mess

<Tabs>
<TabItem label="Good ✅">
```dart
class UnauthorizedException implements Exception {
UnauthorizedException(this.message);
```dart
class UnauthorizedException implements Exception {
UnauthorizedException(this.message);
final String message;
@override
String toString() => 'UnauthorizedException: $message';
}
final String message;
void deleteAccount(String name) {
if (activeRole != Role.admin) {
throw UnauthorizedException('Only admin can delete account');
}
// ...
}
@override
String toString() => 'UnauthorizedException: $message';
}
void main() {
try {
deleteAccount('user');
} on UnauthorizedException catch (e) {
// Handle the exception.
}
}
void deleteAccount(String name) {
if (activeRole != Role.admin) {
throw UnauthorizedException('Only admin can delete account');
}
// ...
}
```
void main() {
try {
deleteAccount('user');
} on UnauthorizedException catch (e) {
// Handle the exception.
}
}
```

</TabItem>
<TabItem label="Bad ❗️">
```dart
void deleteAccount(String name) {
if (activeRole != Role.admin) {
throw Exception('Only admin can delete account');
}
// ...
}
```dart
void deleteAccount(String name) {
if (activeRole != Role.admin) {
throw Exception('Only admin can delete account');
}
// ...
}
void main() {
try {
deleteAccount('user');
} on Exception catch (e) {
// Exception is a marker interface implemented by all core library exceptions.
// It is very generic, potentially catching many different types of exceptions,
// lacking intent, and making the code harder to understand.
}
}
```
void main() {
try {
deleteAccount('user');
} on Exception catch (e) {
// Exception is a marker interface implemented by all core library exceptions.
// It is very generic, potentially catching many different types of exceptions,
// lacking intent, and making the code harder to understand.
}
}
```

</TabItem>
</Tabs>
12 changes: 0 additions & 12 deletions src/content/docs/navigation/navigation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Structure your routes in a way that makes logical sense. Avoid placing all of yo

<Tabs>
<TabItem label="Good ✅">

```txt
/
/flutter
Expand All @@ -32,7 +31,6 @@ Structure your routes in a way that makes logical sense. Avoid placing all of yo

</TabItem>
<TabItem label="Bad ❗️">

```txt
/
/flutter
Expand All @@ -56,7 +54,6 @@ GoRouter allows you to define [type-safe routes](https://pub.dev/documentation/g

<Tabs>
<TabItem label="Good ✅">

```dart
@TypedGoRoute<CategoriesPageRoute>(
name: 'categories',
Expand Down Expand Up @@ -84,7 +81,6 @@ GoRouter allows you to define [type-safe routes](https://pub.dev/documentation/g

</TabItem>
<TabItem label="Bad ❗️">

```dart
GoRoute(
name: 'categories',
Expand Down Expand Up @@ -113,14 +109,12 @@ For reasons listed in the [Prefer navigating by name over path](#prefer-navigati

<Tabs>
<TabItem label="Navigating by name">

```dart
context.goNamed('categories', queryParameters: {'size': 'small', 'color': 'blue'})
```

</TabItem>
<TabItem label="Navigating by path">

```dart
context.go('/categories?size=small&color=blue');
```
Expand Down Expand Up @@ -162,14 +156,12 @@ Mobile app users will likely never see your route's path, but web app users can

<Tabs>
<TabItem label="Good ✅">

```txt
/user/update-address
```

</TabItem>
<TabItem label="Bad ❗️">

```txt
/user/update_address
/user/updateAddress
Expand Down Expand Up @@ -239,14 +231,12 @@ GoRouter provides extension methods on `BuildContext` to simplify navigation. Fo

<Tabs>
<TabItem label="Good ✅">

```dart
context.goNamed('flutterNews');
```

</TabItem>
<TabItem label="Bad ❗️">

```dart
GoRouter.of(context).goNamed('flutterNews');
```
Expand Down Expand Up @@ -353,7 +343,6 @@ The `extra` option used during navigation does not work on the web and cannot be

<Tabs>
<TabItem label="Bad ❗️">

```dart
@TypedGoRoute<FlutterArticlePageRoute>(
name: 'flutterArticle',
Expand Down Expand Up @@ -385,7 +374,6 @@ In this example, we are passing the `article` object to the article details page

<Tabs>
<TabItem label="Good ✅">

```dart
FlutterArticlePageRoute(id: state.article.id).go(context);
```
Expand Down
25 changes: 0 additions & 25 deletions src/content/docs/testing/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ All tests should have one or more statements at the end of the test asserting th

<Tabs>
<TabItem label="Good ✅">

```dart
testWidgets('calls [onTap] on tapping widget', (tester) async {
var isTapped = false;
Expand All @@ -97,7 +96,6 @@ All tests should have one or more statements at the end of the test asserting th

</TabItem>
<TabItem label="Bad ❗️">

```dart
testWidgets('can tap widget', (tester) async {
await tester.pumpWidget(SomeTappableWidget());
Expand All @@ -121,7 +119,6 @@ Now, we are explicitly testing that we have accessed the `onTap` property of `So

<Tabs>
<TabItem label="Good ✅">

```dart
expect(name, equals('Hank'));
expect(people, hasLength(3));
Expand All @@ -130,7 +127,6 @@ Now, we are explicitly testing that we have accessed the `onTap` property of `So

</TabItem>
<TabItem label="Bad ❗️">

```dart
expect(name, 'Hank');
expect(people.length, 3);
Expand All @@ -146,14 +142,12 @@ If you're referencing a type within a test description, use a [string expression

<Tabs>
<TabItem label="Good ✅">

```dart
testWidgets('renders $YourView', (tester) async {});
```

</TabItem>
<TabItem label="Bad ❗️">

```dart
testWidgets('renders YourView', (tester) async {});
```
Expand All @@ -165,14 +159,12 @@ If your [test](https://pub.dev/documentation/test/latest/test/test.html) or [gro

<Tabs>
<TabItem label="Good ✅">

```dart
group(YourView, () {});
```

</TabItem>
<TabItem label="Bad ❗️">

```dart
group('$YourView', () {});
```
Expand All @@ -186,7 +178,6 @@ Don't be afraid of being verbose in your tests. Make sure everything is readable

<Tabs>
<TabItem label="Good ✅">

```dart
testWidgets('renders $YourView', (tester) async {});
testWidgets('renders $YourView for $YourState', (tester) async {});
Expand All @@ -196,7 +187,6 @@ Don't be afraid of being verbose in your tests. Make sure everything is readable

</TabItem>
<TabItem label="Bad ❗️">

```dart
testWidgets('renders', (tester) async {});
test('works', () async {});
Expand All @@ -212,15 +202,13 @@ Aim to test one scenario per test. You might end up with more tests in the codeb

<Tabs>
<TabItem label="Good ✅">

```dart
testWidgets('renders $WidgetA', (tester) async {});
testWidgets('renders $WidgetB', (tester) async {});
```

</TabItem>
<TabItem label="Bad ❗️">

```dart
testWidgets('renders $WidgetA and $WidgetB', (tester) async {});
```
Expand All @@ -234,14 +222,12 @@ Although keys can be an easy way to look for a widget while testing, they tend t

<Tabs>
<TabItem label="Good ✅">

```dart
expect(find.byType(HomePage), findsOneWidget);
```

</TabItem>
<TabItem label="Bad ❗️">

```dart
expect(find.byKey(Key('homePageKey')), findsOneWidget);
```
Expand All @@ -255,14 +241,12 @@ Developers may reuse mocks across different test files. This could lead to undes

<Tabs>
<TabItem label="Good ✅">

```dart
class _MockYourClass extends Mock implements YourClass {}
```

</TabItem>
<TabItem label="Bad ❗️">

```dart
class MockYourClass extends Mock implements YourClass {}
```
Expand Down Expand Up @@ -299,7 +283,6 @@ In order to avoid such issues, refrain from adding `setUp` and `setUpAll` (as we

<Tabs>
<TabItem label="Good ✅">

```dart
void main() {
group(UserRepository, () {
Expand All @@ -317,7 +300,6 @@ In order to avoid such issues, refrain from adding `setUp` and `setUpAll` (as we

</TabItem>
<TabItem label="Bad ❗️">

```dart
void main() {
late ApiClient apiClient;
Expand All @@ -342,7 +324,6 @@ We should ensure that shared mutable objects are initialized per test. This avoi

<Tabs>
<TabItem label="Good ✅">

```dart
void main() {
group(_MySubject, () {
Expand Down Expand Up @@ -371,7 +352,6 @@ We should ensure that shared mutable objects are initialized per test. This avoi

</TabItem>
<TabItem label="Bad ❗️">

```dart
class _MySubjectDependency {
var value = 0;
Expand Down Expand Up @@ -418,7 +398,6 @@ When [tagging tests](https://github.com/dart-lang/test/blob/master/pkgs/test/doc

<Tabs>
<TabItem label="Good ✅">

```dart
testWidgets(
'render matches golden file',
Expand All @@ -431,7 +410,6 @@ When [tagging tests](https://github.com/dart-lang/test/blob/master/pkgs/test/doc

</TabItem>
<TabItem label="Bad ❗️">

```dart
testWidgets(
'render matches golden file',
Expand Down Expand Up @@ -478,7 +456,6 @@ When tests share state (such as relying on static members), the order that tests

<Tabs>
<TabItem label="Good ✅">

```dart
class _Counter {
int value = 0;
Expand Down Expand Up @@ -507,7 +484,6 @@ When tests share state (such as relying on static members), the order that tests

</TabItem>
<TabItem label="Bad ❗️">

```dart
class _Counter {
int value = 0;
Expand Down Expand Up @@ -548,7 +524,6 @@ This practice ensures that tests do not share state or rely on the side effects

<Tabs>
<TabItem label="Good ✅">

```sh
# Randomize test ordering using the --test-randomize-ordering-seed option
flutter test --test-randomize-ordering-seed random
Expand Down

0 comments on commit 4e6aadd

Please sign in to comment.