Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tests (unit/widget) setup #10

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/pr-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: PR review/test

on:
pull_request:

jobs:
analyze:
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- uses: actions/checkout@v3
- name: Setup Flutter
uses: subosito/flutter-action@v2
- name: Install dependencies
run: |
flutter pub get
- uses: invertase/github-action-dart-analyzer@v1
- name: Run tests
run: |
flutter test
6 changes: 4 additions & 2 deletions lib/view/board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class Board extends ConsumerWidget {

return Padding(
padding: const EdgeInsets.all(16),
child: Column(
child: SingleChildScrollView(child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(16),
Expand Down Expand Up @@ -64,7 +66,7 @@ class Board extends ConsumerWidget {
),
),
],
),
)),
);
}

Expand Down
139 changes: 139 additions & 0 deletions test/place_mark_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:tic_tac_toe_handson/provider/tic_tac_toe_provider.dart';

void main() {
late TicTacToeProvider target;

setUp(() {
final container = ProviderContainer();
target = container.read(ticTacToeProvider.notifier);
});

test("placeMark test - row: 0, col: 0", () {
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
target.placeMark(0, 0);
expect(target.state.board, [
['X', '', ''],
['', '', ''],
['', '', ''],
]);
});

test("placeMark test - row: 0, col: 1", () {
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
target.placeMark(0, 1);
expect(target.state.board, [
['', 'X', ''],
['', '', ''],
['', '', ''],
]);
});

test("placeMark test - row: 0, col: 2", () {
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
target.placeMark(0, 2);
expect(target.state.board, [
['', '', 'X'],
['', '', ''],
['', '', ''],
]);
});

test("placeMark test - row: 1, col: 0", () {
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
target.placeMark(1, 0);
expect(target.state.board, [
['', '', ''],
['X', '', ''],
['', '', ''],
]);
});

test("placeMark test - row: 1, col: 1", () {
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
target.placeMark(1, 1);
expect(target.state.board, [
['', '', ''],
['', 'X', ''],
['', '', ''],
]);
});

test("placeMark test - row: 1, col: 2", () {
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
target.placeMark(1, 2);
expect(target.state.board, [
['', '', ''],
['', '', 'X'],
['', '', ''],
]);
});

test("placeMark test - row: 2, col: 0", () {
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
target.placeMark(2, 0);
expect(target.state.board, [
['', '', ''],
['', '', ''],
['X', '', ''],
]);
});

test("placeMark test - row: 2, col: 1", () {
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
target.placeMark(2, 1);
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', 'X', ''],
]);
});

test("placeMark test - row: 2, col: 2", () {
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
target.placeMark(2, 2);
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', 'X'],
]);
});
}
148 changes: 148 additions & 0 deletions test/reset_board_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:tic_tac_toe_handson/provider/tic_tac_toe_provider.dart';

void main() {
late TicTacToeProvider target;

setUp(() {
final container = ProviderContainer();
target = container.read(ticTacToeProvider.notifier);
});

test("resetBoard test - row: 0, col: 0", () {
target.placeMark(0, 0);
expect(target.state.board, [
['X', '', ''],
['', '', ''],
['', '', ''],
]);
target.resetBoard();
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
});

test("resetBoard test - row: 0, col: 1", () {
target.placeMark(0, 1);
expect(target.state.board, [
['', 'X', ''],
['', '', ''],
['', '', ''],
]);
target.resetBoard();
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
});

test("resetBoard test - row: 0, col: 2", () {
target.placeMark(0, 2);
expect(target.state.board, [
['', '', 'X'],
['', '', ''],
['', '', ''],
]);
target.resetBoard();
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
});

test("resetBoard test - row: 1, col: 0", () {
target.placeMark(1, 0);
expect(target.state.board, [
['', '', ''],
['X', '', ''],
['', '', ''],
]);
target.resetBoard();
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
});

test("resetBoard test - row: 1, col: 1", () {
target.placeMark(1, 1);
expect(target.state.board, [
['', '', ''],
['', 'X', ''],
['', '', ''],
]);
target.resetBoard();
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
});

test("resetBoard test - row: 1, col: 2", () {
target.placeMark(1, 2);
expect(target.state.board, [
['', '', ''],
['', '', 'X'],
['', '', ''],
]);
target.resetBoard();
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
});

test("resetBoard test - row: 2, col: 0", () {
target.placeMark(2, 0);
expect(target.state.board, [
['', '', ''],
['', '', ''],
['X', '', ''],
]);
target.resetBoard();
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
});

test("resetBoard test - row: 2, col: 1", () {
target.placeMark(2, 1);
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', 'X', ''],
]);
target.resetBoard();
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
});

test("resetBoard test - row: 2, col: 2", () {
target.placeMark(2, 2);
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', 'X'],
]);
target.resetBoard();
expect(target.state.board, [
['', '', ''],
['', '', ''],
['', '', ''],
]);
});
}
21 changes: 6 additions & 15 deletions test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,17 @@
// 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.

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:tic_tac_toe_handson/main.dart';

void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
testWidgets('Title rendering test', (WidgetTester tester) async {
await tester.pumpWidget(const ProviderScope(
child: MyApp(),
));

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);

// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();

// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
expect(find.text('FlutterKaigi 2023 - TicTacToe'), findsOneWidget);
});
}
Loading