mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 14:58:34 +00:00
[linting_tool] Implement saving rules to DB (#860)
This commit is contained in:
committed by
GitHub
parent
1818925286
commit
bbb8e342f1
@@ -1,20 +1,88 @@
|
||||
// This is a basic Flutter widget test.
|
||||
//
|
||||
// To perform an interaction with a widget in your test, use the WidgetTester
|
||||
// utility that Flutter provides. For example, you can send tap and scroll
|
||||
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||||
// tree, read text, and verify that the values of widget properties are correct.
|
||||
// Copyright 2021 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:linting_tool/app.dart';
|
||||
import 'package:linting_tool/model/profile.dart';
|
||||
import 'package:linting_tool/model/profiles_store.dart';
|
||||
import 'package:linting_tool/model/rule.dart';
|
||||
import 'package:linting_tool/model/rules_store.dart';
|
||||
import 'package:linting_tool/pages/default_lints_page.dart';
|
||||
import 'package:linting_tool/pages/home_page.dart';
|
||||
import 'package:linting_tool/pages/saved_lints_page.dart';
|
||||
import 'package:linting_tool/theme/app_theme.dart';
|
||||
import 'package:linting_tool/widgets/adaptive_nav.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'widget_test.mocks.dart';
|
||||
|
||||
late MockClient _mockClient;
|
||||
|
||||
class _TestApp extends StatelessWidget {
|
||||
const _TestApp({
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider<RuleStore>(
|
||||
create: (context) => RuleStore(_mockClient),
|
||||
),
|
||||
ChangeNotifierProvider<ProfilesStore>(
|
||||
create: (context) => ProfilesStore(),
|
||||
),
|
||||
],
|
||||
child: MaterialApp(
|
||||
title: 'Flutter Linting Tool',
|
||||
initialRoute: LintingTool.homeRoute,
|
||||
theme: AppTheme.buildReplyLightTheme(context),
|
||||
onGenerateRoute: (settings) {
|
||||
switch (settings.name) {
|
||||
case LintingTool.homeRoute:
|
||||
return MaterialPageRoute<void>(
|
||||
builder: (context) => const AdaptiveNav(),
|
||||
settings: settings,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@GenerateMocks([http.Client])
|
||||
void main() {
|
||||
setUp(() async {
|
||||
final tempDir = await Directory.systemTemp.createTemp();
|
||||
Hive.init(tempDir.path);
|
||||
Hive.registerAdapter(RuleAdapter());
|
||||
Hive.registerAdapter(RulesProfileAdapter());
|
||||
await Hive.openLazyBox<RulesProfile>('rules_profile');
|
||||
_mockClient = MockClient();
|
||||
});
|
||||
testWidgets('NavigationRail smoke test', (tester) async {
|
||||
await tester.pumpWidget(const LintingTool());
|
||||
var responseBody =
|
||||
'''[{"name": "always_use_package_imports","description": "Avoid relative imports for files in `lib/`.","group": "errors","maturity": "stable","incompatible": [],"sets": [],"details": "*DO* avoid relative imports for files in `lib/`.\n\nWhen mixing relative and absolute imports it's possible to create confusion\nwhere the same member gets imported in two different ways. One way to avoid\nthat is to ensure you consistently use absolute imports for files withing the\n`lib/` directory.\n\nThis is the opposite of 'prefer_relative_imports'.\nMight be used with 'avoid_relative_lib_imports' to avoid relative imports of\nfiles within `lib/` directory outside of it. (for example `test/`)\n\n**GOOD:**\n\n```dart\nimport 'package:foo/bar.dart';\n\nimport 'package:foo/baz.dart';\n\nimport 'package:foo/src/baz.dart';\n...\n```\n\n**BAD:**\n\n```dart\nimport 'baz.dart';\n\nimport 'src/bag.dart'\n\nimport '../lib/baz.dart';\n\n...\n```\n\n"}]''';
|
||||
|
||||
when(_mockClient.get(Uri.parse(
|
||||
'https://dart-lang.github.io/linter//lints/machine/rules.json')))
|
||||
.thenAnswer(
|
||||
(_) async => http.Response(responseBody, 400),
|
||||
);
|
||||
|
||||
await tester.pumpWidget(const _TestApp());
|
||||
|
||||
expect(find.byType(HomePage), findsOneWidget);
|
||||
expect(find.byType(SavedLintsPage), findsNothing);
|
||||
await tester.tap(find.text('Saved Profiles'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
107
experimental/linting_tool/test/widget_test.mocks.dart
Normal file
107
experimental/linting_tool/test/widget_test.mocks.dart
Normal file
@@ -0,0 +1,107 @@
|
||||
// Mocks generated by Mockito 5.0.13 from annotations
|
||||
// in linting_tool/test/widget_test.dart.
|
||||
// Do not manually edit this file.
|
||||
|
||||
import 'dart:async' as _i5;
|
||||
import 'dart:convert' as _i6;
|
||||
import 'dart:typed_data' as _i7;
|
||||
|
||||
import 'package:http/src/base_request.dart' as _i8;
|
||||
import 'package:http/src/client.dart' as _i4;
|
||||
import 'package:http/src/response.dart' as _i2;
|
||||
import 'package:http/src/streamed_response.dart' as _i3;
|
||||
import 'package:mockito/mockito.dart' as _i1;
|
||||
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
// ignore_for_file: avoid_setters_without_getters
|
||||
// ignore_for_file: comment_references
|
||||
// ignore_for_file: implementation_imports
|
||||
// ignore_for_file: invalid_use_of_visible_for_testing_member
|
||||
// ignore_for_file: prefer_const_constructors
|
||||
// ignore_for_file: unnecessary_parenthesis
|
||||
|
||||
class _FakeResponse extends _i1.Fake implements _i2.Response {}
|
||||
|
||||
class _FakeStreamedResponse extends _i1.Fake implements _i3.StreamedResponse {}
|
||||
|
||||
/// A class which mocks [Client].
|
||||
///
|
||||
/// See the documentation for Mockito's code generation for more information.
|
||||
class MockClient extends _i1.Mock implements _i4.Client {
|
||||
MockClient() {
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i5.Future<_i2.Response> head(Uri? url, {Map<String, String>? headers}) =>
|
||||
(super.noSuchMethod(Invocation.method(#head, [url], {#headers: headers}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> get(Uri? url, {Map<String, String>? headers}) =>
|
||||
(super.noSuchMethod(Invocation.method(#get, [url], {#headers: headers}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> post(Uri? url,
|
||||
{Map<String, String>? headers,
|
||||
Object? body,
|
||||
_i6.Encoding? encoding}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#post, [url],
|
||||
{#headers: headers, #body: body, #encoding: encoding}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> put(Uri? url,
|
||||
{Map<String, String>? headers,
|
||||
Object? body,
|
||||
_i6.Encoding? encoding}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#put, [url],
|
||||
{#headers: headers, #body: body, #encoding: encoding}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> patch(Uri? url,
|
||||
{Map<String, String>? headers,
|
||||
Object? body,
|
||||
_i6.Encoding? encoding}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#patch, [url],
|
||||
{#headers: headers, #body: body, #encoding: encoding}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<_i2.Response> delete(Uri? url,
|
||||
{Map<String, String>? headers,
|
||||
Object? body,
|
||||
_i6.Encoding? encoding}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#delete, [url],
|
||||
{#headers: headers, #body: body, #encoding: encoding}),
|
||||
returnValue: Future<_i2.Response>.value(_FakeResponse()))
|
||||
as _i5.Future<_i2.Response>);
|
||||
@override
|
||||
_i5.Future<String> read(Uri? url, {Map<String, String>? headers}) =>
|
||||
(super.noSuchMethod(Invocation.method(#read, [url], {#headers: headers}),
|
||||
returnValue: Future<String>.value('')) as _i5.Future<String>);
|
||||
@override
|
||||
_i5.Future<_i7.Uint8List> readBytes(Uri? url,
|
||||
{Map<String, String>? headers}) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#readBytes, [url], {#headers: headers}),
|
||||
returnValue: Future<_i7.Uint8List>.value(_i7.Uint8List(0)))
|
||||
as _i5.Future<_i7.Uint8List>);
|
||||
@override
|
||||
_i5.Future<_i3.StreamedResponse> send(_i8.BaseRequest? request) =>
|
||||
(super.noSuchMethod(Invocation.method(#send, [request]),
|
||||
returnValue:
|
||||
Future<_i3.StreamedResponse>.value(_FakeStreamedResponse()))
|
||||
as _i5.Future<_i3.StreamedResponse>);
|
||||
@override
|
||||
void close() => super.noSuchMethod(Invocation.method(#close, []),
|
||||
returnValueForMissingStub: null);
|
||||
@override
|
||||
String toString() => super.toString();
|
||||
}
|
||||
Reference in New Issue
Block a user