148 lines
3.3 KiB
Dart
148 lines
3.3 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:kalkulator/calculator_logic.dart';
|
|
|
|
void main() {
|
|
late CalculatorLogic calc;
|
|
|
|
setUp(() {
|
|
calc = CalculatorLogic();
|
|
});
|
|
|
|
group('inputDigit', () {
|
|
test('starts with 0', () {
|
|
expect(calc.display, '0');
|
|
});
|
|
|
|
test('replaces initial 0 with digit', () {
|
|
calc.inputDigit('5');
|
|
expect(calc.display, '5');
|
|
});
|
|
|
|
test('appends digits', () {
|
|
calc.inputDigit('1');
|
|
calc.inputDigit('2');
|
|
calc.inputDigit('3');
|
|
expect(calc.display, '123');
|
|
});
|
|
});
|
|
|
|
group('inputDecimal', () {
|
|
test('adds decimal point', () {
|
|
calc.inputDigit('3');
|
|
calc.inputDecimal();
|
|
expect(calc.display, '3.');
|
|
});
|
|
|
|
test('ignores second decimal point', () {
|
|
calc.inputDigit('3');
|
|
calc.inputDecimal();
|
|
calc.inputDigit('1');
|
|
calc.inputDecimal();
|
|
expect(calc.display, '3.1');
|
|
});
|
|
});
|
|
|
|
group('addition', () {
|
|
test('2 + 3 = 5', () {
|
|
calc.inputDigit('2');
|
|
calc.inputOperator('+');
|
|
calc.inputDigit('3');
|
|
calc.calculate();
|
|
expect(calc.display, '5');
|
|
});
|
|
});
|
|
|
|
group('subtraction', () {
|
|
test('9 - 4 = 5', () {
|
|
calc.inputDigit('9');
|
|
calc.inputOperator('-');
|
|
calc.inputDigit('4');
|
|
calc.calculate();
|
|
expect(calc.display, '5');
|
|
});
|
|
});
|
|
|
|
group('multiplication', () {
|
|
test('6 * 7 = 42', () {
|
|
calc.inputDigit('6');
|
|
calc.inputOperator('*');
|
|
calc.inputDigit('7');
|
|
calc.calculate();
|
|
expect(calc.display, '42');
|
|
});
|
|
});
|
|
|
|
group('division', () {
|
|
test('8 / 2 = 4', () {
|
|
calc.inputDigit('8');
|
|
calc.inputOperator('/');
|
|
calc.inputDigit('2');
|
|
calc.calculate();
|
|
expect(calc.display, '4');
|
|
});
|
|
|
|
test('division by zero returns Infinity', () {
|
|
calc.inputDigit('5');
|
|
calc.inputOperator('/');
|
|
calc.inputDigit('0');
|
|
calc.calculate();
|
|
expect(calc.display, 'Infinity');
|
|
});
|
|
|
|
test('decimal result: 7 / 2 = 3.5', () {
|
|
calc.inputDigit('7');
|
|
calc.inputOperator('/');
|
|
calc.inputDigit('2');
|
|
calc.calculate();
|
|
expect(calc.display, '3.5');
|
|
});
|
|
});
|
|
|
|
group('chained operations', () {
|
|
test('2 + 3 + 4 = 9', () {
|
|
calc.inputDigit('2');
|
|
calc.inputOperator('+');
|
|
calc.inputDigit('3');
|
|
calc.inputOperator('+');
|
|
expect(calc.display, '5');
|
|
calc.inputDigit('4');
|
|
calc.calculate();
|
|
expect(calc.display, '9');
|
|
});
|
|
});
|
|
|
|
group('clear', () {
|
|
test('resets to 0', () {
|
|
calc.inputDigit('5');
|
|
calc.inputOperator('+');
|
|
calc.inputDigit('3');
|
|
calc.clear();
|
|
expect(calc.display, '0');
|
|
});
|
|
});
|
|
|
|
group('toggleSign', () {
|
|
test('positive to negative', () {
|
|
calc.inputDigit('7');
|
|
calc.toggleSign();
|
|
expect(calc.display, '-7');
|
|
});
|
|
|
|
test('negative to positive', () {
|
|
calc.inputDigit('7');
|
|
calc.toggleSign();
|
|
calc.toggleSign();
|
|
expect(calc.display, '7');
|
|
});
|
|
});
|
|
|
|
group('percent', () {
|
|
test('50 percent = 0.5', () {
|
|
calc.inputDigit('5');
|
|
calc.inputDigit('0');
|
|
calc.percent();
|
|
expect(calc.display, '0.5');
|
|
});
|
|
});
|
|
}
|