flutter_application_1/test/unit_test.dart

73 lines
3.1 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_application_1/utils.dart';
import 'package:flutter_application_1/enums.dart';
import 'package:flutter_application_1/calculator.dart';
void main() {
group('Calculation Tests', () {
test('Calculates correct invested money', () {
expect(calculateInvestedMoney(1000, 100, 5, []), equals(7000.0));
expect(calculateInvestedMoney(2000, 50, 10, []), equals(8000.0));
});
test('Calculates correct compound interest', () {
// Testen mit jährlicher Auszahlung
List<double> investedMoneyListYearly = [];
List<double> compoundInterestListYearly = [];
calculateInvestedMoney(1000, 100, 10, investedMoneyListYearly);
expect(calculateCompoundInterest(1000, 100, 5, 10, PayoutInterval.yearly, investedMoneyListYearly, compoundInterestListYearly), equals(16722.0));
// Testen mit monatlicher Auszahlung
List<double> investedMoneyListMonthly = [];
List<double> compoundInterestListMonthly = [];
calculateInvestedMoney(2000, 50, 10, investedMoneyListMonthly);
expect(calculateCompoundInterest(2000, 50, 5, 10, PayoutInterval.monthly, investedMoneyListMonthly, compoundInterestListMonthly), equals(11058.0));
});
test('Calculates correct invested money with extremely high input values', () {
expect(calculateInvestedMoney(1e15, 1e12, 100, []), equals(2.2e15));
});
test('Calculates correct invested money with extremely low input values', () {
expect(calculateInvestedMoney(0, 0, 0, []), equals(0.0));
expect(calculateInvestedMoney(0.1, 0.01, 0.001, []), equals(0.0));
});
test('Calculates correct compound interest with extremely high interest rate', () {
List<double> investedMoneyListHighInterest = [];
List<double> compoundInterestListHighInterest = [];
calculateInvestedMoney(1000, 100, 10, investedMoneyListHighInterest);
expect(calculateCompoundInterest(1000, 100, 1000, 10, PayoutInterval.yearly, investedMoneyListHighInterest, compoundInterestListHighInterest), equals(29049915553000.0));
});
});
group('Utility Methods Tests', () {
test('roundToInteger should round the value to the nearest integer', () {
final controller = TextEditingController();
controller.text = '2.7';
roundToInteger(controller);
expect(controller.text, '3');
controller.text = '2.3';
roundToInteger(controller);
expect(controller.text, '2');
});
test('isNumeric should check if a string is a valid number', () {
expect(isNumeric('123'), true);
expect(isNumeric('12.3'), true);
expect(isNumeric('12,3'), true);
expect(isNumeric('abc'), false);
expect(isNumeric('12a'), false);
});
test('restoreDefaultValuesIfEmpty should set default value 0 if the field is empty', () {
final controller = TextEditingController();
controller.text = '';
restoreDefaultValuesIfEmpty(controller);
expect(controller.text, '0');
controller.text = '5';
restoreDefaultValuesIfEmpty(controller);
expect(controller.text, '5');
});
});
}