added tests

feature/initViews
Christopher Schmitt 2024-01-09 14:29:17 +01:00
parent 72eb1c28be
commit 623cb60d2c
6 changed files with 434 additions and 75 deletions

View File

@ -1,6 +1,30 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
_fe_analyzer_shared:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: eb376e9acf6938204f90eb3b1f00b578640d3188b4c8a8ec054f9f479af8d051
url: "https://pub.dev"
source: hosted
version: "64.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893"
url: "https://pub.dev"
source: hosted
version: "6.2.0"
args:
dependency: transitive
description:
name: args
sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596
url: "https://pub.dev"
source: hosted
version: "2.4.2"
async:
dependency: transitive
description:
@ -17,6 +41,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
build:
dependency: transitive
description:
name: build
sha256: "80184af8b6cb3e5c1c4ec6d8544d27711700bc3e6d2efad04238c7b5290889f0"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
built_collection:
dependency: transitive
description:
name: built_collection
sha256: "376e3dd27b51ea877c28d525560790aee2e6fbb5f20e2f85d5081027d94e2100"
url: "https://pub.dev"
source: hosted
version: "5.1.1"
built_value:
dependency: transitive
description:
name: built_value
sha256: c9aabae0718ec394e5bc3c7272e6bb0dc0b32201a08fe185ec1d8401d3e39309
url: "https://pub.dev"
source: hosted
version: "8.8.1"
characters:
dependency: transitive
description:
@ -33,6 +81,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.1"
code_builder:
dependency: transitive
description:
name: code_builder
sha256: f692079e25e7869c14132d39f223f8eec9830eb76131925143b2129c4bb01b37
url: "https://pub.dev"
source: hosted
version: "4.10.0"
collection:
dependency: transitive
description:
@ -41,6 +97,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.17.2"
convert:
dependency: transitive
description:
name: convert
sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592"
url: "https://pub.dev"
source: hosted
version: "3.1.1"
crypto:
dependency: transitive
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
url: "https://pub.dev"
source: hosted
version: "3.0.3"
cupertino_icons:
dependency: "direct main"
description:
@ -49,6 +121,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.6"
dart_style:
dependency: transitive
description:
name: dart_style
sha256: "40ae61a5d43feea6d24bd22c0537a6629db858963b99b4bc1c3db80676f32368"
url: "https://pub.dev"
source: hosted
version: "2.3.4"
fake_async:
dependency: transitive
description:
@ -73,6 +153,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "7.0.0"
fixnum:
dependency: transitive
description:
name: fixnum
sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
flutter:
dependency: "direct main"
description: flutter
@ -96,6 +184,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
glob:
dependency: transitive
description:
name: glob
sha256: "0e7014b3b7d4dac1ca4d6114f82bf1782ee86745b9b42a92c9289c23d8a0ab63"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
go_router:
dependency: "direct main"
description:
@ -152,6 +248,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.1"
mockito:
dependency: "direct dev"
description:
name: mockito
sha256: "6841eed20a7befac0ce07df8116c8b8233ed1f4486a7647c7fc5a02ae6163917"
url: "https://pub.dev"
source: hosted
version: "5.4.4"
package_config:
dependency: transitive
description:
name: package_config
sha256: "1c5b77ccc91e4823a5af61ee74e6b972db1ef98c2ff5a18d3161c982a55448bd"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
path:
dependency: transitive
description:
@ -172,10 +284,10 @@ packages:
dependency: transitive
description:
name: path_provider_platform_interface
sha256: "94b1e0dd80970c1ce43d5d4e050a9918fce4f4a775e6142424c30a29a363265c"
sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "2.1.2"
path_provider_windows:
dependency: transitive
description:
@ -188,18 +300,26 @@ packages:
dependency: transitive
description:
name: platform
sha256: "0a279f0707af40c890e80b1e9df8bb761694c074ba7e1d4ab1bc4b728e200b59"
sha256: "12220bb4b65720483f8fa9450b4332347737cf8213dd2840d8b2c823e47243ec"
url: "https://pub.dev"
source: hosted
version: "3.1.3"
version: "3.1.4"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: f4f88d4a900933e7267e2b353594774fc0d07fb072b47eedcd5b54e1ea3269f8
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.7"
version: "2.1.8"
pub_semver:
dependency: transitive
description:
name: pub_semver
sha256: "40d3ab1bbd474c4c2328c91e3a7df8c6dd629b79ece4c4bd04bee496a224fb0c"
url: "https://pub.dev"
source: hosted
version: "2.1.4"
shared_preferences:
dependency: "direct main"
description:
@ -236,10 +356,10 @@ packages:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: d4ec5fc9ebb2f2e056c617112aa75dcf92fc2e4faaf2ae999caa297473f75d8a
sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b"
url: "https://pub.dev"
source: hosted
version: "2.3.1"
version: "2.3.2"
shared_preferences_web:
dependency: transitive
description:
@ -261,6 +381,14 @@ packages:
description: flutter
source: sdk
version: "0.0.99"
source_gen:
dependency: transitive
description:
name: source_gen
sha256: "14658ba5f669685cd3d63701d01b31ea748310f7ab854e471962670abcf57832"
url: "https://pub.dev"
source: hosted
version: "1.5.0"
source_span:
dependency: transitive
description:
@ -309,30 +437,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.0"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
url: "https://pub.dev"
source: hosted
version: "1.3.2"
url_launcher:
dependency: "direct main"
description:
name: url_launcher
sha256: e9aa5ea75c84cf46b3db4eea212523591211c3cf2e13099ee4ec147f54201c86
sha256: d25bb0ca00432a5e1ee40e69c36c85863addf7cc45e433769d61bed3fe81fd96
url: "https://pub.dev"
source: hosted
version: "6.2.2"
version: "6.2.3"
url_launcher_android:
dependency: transitive
description:
name: url_launcher_android
sha256: "31222ffb0063171b526d3e569079cf1f8b294075ba323443fdc690842bfd4def"
sha256: "507dc655b1d9cb5ebc756032eb785f114e415f91557b73bf60b7e201dfedeb2f"
url: "https://pub.dev"
source: hosted
version: "6.2.0"
version: "6.2.2"
url_launcher_ios:
dependency: transitive
description:
name: url_launcher_ios
sha256: bba3373219b7abb6b5e0d071b0fe66dfbe005d07517a68e38d4fc3638f35c6d3
sha256: cdb7b6da34483f9b2c9f8b2b29bc468fa7271d92e2021607ca0c4d3bcb04cdd4
url: "https://pub.dev"
source: hosted
version: "6.2.1"
version: "6.2.3"
url_launcher_linux:
dependency: transitive
description:
@ -353,10 +489,10 @@ packages:
dependency: transitive
description:
name: url_launcher_platform_interface
sha256: "980e8d9af422f477be6948bdfb68df8433be71f5743a188968b0c1b887807e50"
sha256: a932c3a8082e118f80a475ce692fde89dc20fddb24c57360b96bc56f7035de1f
url: "https://pub.dev"
source: hosted
version: "2.2.0"
version: "2.3.1"
url_launcher_web:
dependency: transitive
description:
@ -381,6 +517,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
watcher:
dependency: transitive
description:
name: watcher
sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
web:
dependency: transitive
description:
@ -401,10 +545,18 @@ packages:
dependency: transitive
description:
name: xdg_directories
sha256: "589ada45ba9e39405c198fe34eb0f607cddb2108527e658136120892beac46d2"
sha256: faea9dee56b520b55a566385b84f2e8de55e7496104adada9962e0bd11bcff1d
url: "https://pub.dev"
source: hosted
version: "1.0.3"
version: "1.0.4"
yaml:
dependency: transitive
description:
name: yaml
sha256: "75769501ea3489fca56601ff33454fe45507ea3bfb014161abc3b43ae25989d5"
url: "https://pub.dev"
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.1.5 <4.0.0"
flutter: ">=3.13.0"

View File

@ -24,6 +24,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^5.0.0
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is

View File

@ -1,27 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moody/main.dart';
import 'package:moody/utils/slide_direction.dart';
import 'package:moody/views/settings_page/settings_page.dart';
void main() {
// Test for basic rendering of MyApp
testWidgets('MyApp renders correctly', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
expect(find.byType(MaterialApp), findsOneWidget);
});
// Test for route configuration
testWidgets('Navigating to /settings shows SettingsPage', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
await tester.tap(find.text('Settings'));
await tester.pumpAndSettle();
expect(find.byType(SettingsPage), findsOneWidget);
});
// Test for custom logic - determineSlideDirection
test('determineSlideDirection returns correct SlideDirection', () {
expect(determineSlideDirection('/', '/settings'), equals(SlideDirection.left));
// Add more test cases for different route combinations
});
}

View File

@ -0,0 +1,206 @@
import 'dart:ui';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:moody/utils/definitions/color_pair.dart';
import 'package:moody/utils/logic/preferences_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MockSharedPreferences extends Mock implements SharedPreferences {}
void main() {
group('PreferencesService Tests', () {
MockSharedPreferences mockPrefs;
PreferencesService service = PreferencesService();
setUp(() {
mockPrefs = MockSharedPreferences();
SharedPreferences.setMockInitialValues({}); // Set initial values for mocks
});
test('saveColorPair and loadColorPair should work correctly', () async {
var testColorPair = ColorPair(name: "test", textColor: Color(0xff123456), backgroundColor: Color(0xff654321));
// Save color pair
await service.saveColorPair(testColorPair);
// Load color pair
var loadedColorPair = await service.loadColorPair();
// Compare values
expect(loadedColorPair.textColor.value, equals(testColorPair.textColor.value));
expect(loadedColorPair.backgroundColor.value, equals(testColorPair.backgroundColor.value));
});
test('loadColorPair should return default value if none set', () async {
// Load color pair without setting any value
var defaultColorPair = await service.loadColorPair();
// Verify default values
expect(defaultColorPair, equals(colorPairs[0]));
});
// Test for setting and checking PIN
test('setPin and checkPin should work correctly', () async {
const int testPin = 1234;
// Set PIN
await service.setPin(testPin);
// Check PIN
bool isCorrectPin = await service.checkPin(testPin);
expect(isCorrectPin, isTrue);
// Check with incorrect PIN
isCorrectPin = await service.checkPin(9999);
expect(isCorrectPin, isFalse);
});
test('enablePin should enable PIN if it\'s already set', () async {
// Set PIN
const int testPin = 1234;
await service.setPin(testPin);
// Attempt to enable PIN
await service.enablePin();
var isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isTrue);
// Disable PIN again
await service.disablePin();
isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isFalse);
});
// Test for enabling PIN only if it's set
test('enablePin should only enable if a PIN is set', () async {
// Assuming no PIN is set initially
await service.disablePin();
// Attempt to enable PIN without setting it
await service.enablePin();
var isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isFalse);
// Set PIN and then enable it
const int testPin = 1234;
await service.setPin(testPin);
await service.enablePin();
isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isTrue);
// Disable PIN again
await service.disablePin();
isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isFalse);
});
// Test for isPinEnabled
test('isPinEnabled should return correct status', () async {
// Assuming PIN is not set
bool isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isFalse);
// After setting and enabling PIN
await service.setPin(1234);
await service.enablePin();
isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isTrue);
});
// Test for calculateStreak
test('calculateStreak should return the correct streak count', () async {
// Scenario 1: Consecutive diary entries
await service.saveDiaryEntry(DiaryEntry(date: DateTime.now(), percentValue: 80, texts: ["Entry 1"]));
await service.saveDiaryEntry(DiaryEntry(date: DateTime.now().subtract(const Duration(days: 1)), percentValue: 90, texts: ["Entry 2"]));
await service.saveDiaryEntry(DiaryEntry(date: DateTime.now().subtract(const Duration(days: 2)), percentValue: 95, texts: ["Entry 3"]));
var streakCount = await service.calculateStreak();
expect(streakCount, equals(3));
// Scenario 2: Missing entries in between
await service.saveDiaryEntry(DiaryEntry(date: DateTime.now().subtract(const Duration(days: 2)), percentValue: 95, texts: ["Entry 2"]));
streakCount = await service.calculateStreak();
expect(streakCount, equals(3)); // Streak is broken due to missing entries
// Scenario 3: No diary entries
// No need to add entries here
streakCount = await service.calculateStreak();
expect(streakCount, equals(3)); // No entries, streak is 0
});
service = PreferencesService();
// Test for getDiaryEntryByCurrentDate
test('getDiaryEntryByCurrentDate should return the correct entry for the current date', () async {
final currentDate = DateTime.now();
final formattedDate = DateTime(currentDate.year, currentDate.month, currentDate.day);
final DiaryEntry testEntry = DiaryEntry(
date: formattedDate,
percentValue: 80,
texts: ["Entry for Today"],
);
// Save a test entry for the current date
await service.saveDiaryEntry(testEntry);
final DiaryEntry? retrievedEntry = await service.getDiaryEntryByCurrentDate();
expect(retrievedEntry?.texts.join(" "), equals((testEntry?.texts.join(" "))));
expect(retrievedEntry?.date, equals(formattedDate));
expect(retrievedEntry?.percentValue, testEntry.percentValue);
});
// Test for getDiaryEntryByDate
test('getDiaryEntryByCurrentDate should return the correct entry for the current date', () async {
final currentDate = DateTime.now();
final formattedDate = DateTime(currentDate.year, currentDate.month, currentDate.day);
final DiaryEntry testEntry = DiaryEntry(
date: formattedDate,
percentValue: 80,
texts: ["Entry for Today"],
);
// Save a test entry for the current date
await service.saveDiaryEntry(testEntry);
final DiaryEntry? retrievedEntry = await service.getDiaryEntryByCurrentDate();
expect(retrievedEntry?.texts.join(" "), equals((testEntry?.texts.join(" "))));
expect(retrievedEntry?.date, equals(formattedDate));
expect(retrievedEntry?.percentValue, testEntry.percentValue);
});
test('saveDiaryEntry and loadDiaryEntries should work correctly', () async {
final List<DiaryEntry> testEntries = [
DiaryEntry(
date: DateTime(2024, 1, 10), // Updated date format
percentValue: 70,
texts: ["Entry 1"],
),
DiaryEntry(
date: DateTime(2024, 1, 11), // Updated date format
percentValue: 80,
texts: ["Entry 2"],
),
DiaryEntry(
date: DateTime(2024, 1, 12), // Updated date format
percentValue: 90,
texts: ["Entry 3"],
),
];
// Save test entries
for (final entry in testEntries) {
await service.saveDiaryEntry(entry);
}
// Load saved entries
final List<DiaryEntry> loadedEntries = await service.loadDiaryEntries();
// Compare loaded entries with test entries
expect(loadedEntries.length, equals(testEntries.length));
for (int i = 0; i < loadedEntries.length; i++) {
expect(loadedEntries[i].date, equals(testEntries[i].date));
expect(loadedEntries[i].percentValue, equals(testEntries[i].percentValue));
expect(loadedEntries[i].texts, equals(testEntries[i].texts));
}
});
});
}

View File

@ -0,0 +1,57 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:moody/utils/logic/question_generator.dart';
void main() {
group('QuestionGenerator Tests', () {
test('getQuestionByDate should return a valid question', () {
final QuestionGenerator generator = QuestionGenerator();
// Test with specific dates
final DateTime date1 = DateTime(2024, 1, 10); // Replace with your desired date
final DateTime date2 = DateTime(2024, 1, 11); // Replace with another date
final DateTime date3 = DateTime(2024, 1, 12); // Replace with yet another date
final String question1 = generator.getQuestionByDate(date1);
final String question2 = generator.getQuestionByDate(date2);
final String question3 = generator.getQuestionByDate(date3);
// Assert that the generated questions are not null
expect(question1, isNotNull);
expect(question2, isNotNull);
expect(question3, isNotNull);
// Add more test cases...
});
test('getQuestionByDate should return different questions for different dates', () {
final QuestionGenerator generator = QuestionGenerator();
// Test with specific dates
final DateTime date1 = DateTime(2024, 1, 10); // Replace with your desired date
final DateTime date2 = DateTime(2024, 1, 11); // Replace with another date
final DateTime date3 = DateTime(2024, 1, 12); // Replace with yet another date
final String question1 = generator.getQuestionByDate(date1);
final String question2 = generator.getQuestionByDate(date2);
final String question3 = generator.getQuestionByDate(date3);
// Assert that the generated questions for different dates are not the same
expect(question1, equals(question2));
expect(question2, equals(question3));
expect(question3, equals(question1));
});
test('getQuestionByDate should return the same question for the same date', () {
final QuestionGenerator generator = QuestionGenerator();
// Test with the same date
final DateTime date = DateTime(2024, 1, 10); // Replace with your desired date
final String question1 = generator.getQuestionByDate(date);
final String question2 = generator.getQuestionByDate(date);
// Assert that the generated questions for the same date are the same
expect(question1, equals(question2));
});
});
}

View File

@ -1,30 +0,0 @@
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. 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.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moody/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const 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);
});
}