1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_gallery/demo/calculator/logic.dart';
// ignore: deprecated_member_use
import 'package:test_api/test_api.dart' hide TypeMatcher, isInstanceOf;
void main() {
test('Test order of operations: 12 + 3 * 4 = 24', () {
CalcExpression expression = CalcExpression.empty();
expression = expression.appendDigit(1)!;
expression = expression.appendDigit(2)!;
expression = expression.appendOperation(Operation.Addition)!;
expression = expression.appendDigit(3)!;
expression = expression.appendOperation(Operation.Multiplication)!;
expression = expression.appendDigit(4)!;
expression = expression.computeResult()!;
expect(expression.state, equals(ExpressionState.Result));
expect(expression.toString(), equals('24'));
});
test('Test floating point 0.1 + 0.2 = 0.3', () {
CalcExpression expression = CalcExpression.empty();
expression = expression.appendDigit(0)!;
expression = expression.appendPoint()!;
expression = expression.appendDigit(1)!;
expression = expression.appendOperation(Operation.Addition)!;
expression = expression.appendDigit(0)!;
expression = expression.appendPoint()!;
expression = expression.appendDigit(2)!;
expression = expression.computeResult()!;
expect(expression.state, equals(ExpressionState.Result));
expect(expression.toString(), equals('0.3'));
});
test('Test floating point 1.0/10.0 = 0.1', () {
CalcExpression expression = CalcExpression.empty();
expression = expression.appendDigit(1)!;
expression = expression.appendPoint()!;
expression = expression.appendDigit(0)!;
expression = expression.appendOperation(Operation.Division)!;
expression = expression.appendDigit(1)!;
expression = expression.appendDigit(0)!;
expression = expression.appendPoint()!;
expression = expression.appendDigit(0)!;
expression = expression.computeResult()!;
expect(expression.state, equals(ExpressionState.Result));
expect(expression.toString(), equals('0.1'));
});
test('Test 1/0 = Infinity', () {
CalcExpression expression = CalcExpression.empty();
expression = expression.appendDigit(1)!;
expression = expression.appendOperation(Operation.Division)!;
expression = expression.appendDigit(0)!;
expression = expression.computeResult()!;
expect(expression.state, equals(ExpressionState.Result));
expect(expression.toString(), equals('Infinity'));
});
test('Test use result in next calculation: 1 + 1 = 2 + 1 = 3 + 1 = 4', () {
CalcExpression expression = CalcExpression.empty();
expression = expression.appendDigit(1)!;
expression = expression.appendOperation(Operation.Addition)!;
expression = expression.appendDigit(1)!;
expression = expression.computeResult()!;
expression = expression.appendOperation(Operation.Addition)!;
expression = expression.appendDigit(1)!;
expression = expression.computeResult()!;
expression = expression.appendOperation(Operation.Addition)!;
expression = expression.appendDigit(1)!;
expression = expression.computeResult()!;
expect(expression.state, equals(ExpressionState.Result));
expect(expression.toString(), equals('4'));
});
test('Test minus -3 - -2 = -1', () {
CalcExpression expression = CalcExpression.empty();
expression = expression.appendMinus()!;
expression = expression.appendDigit(3)!;
expression = expression.appendMinus()!;
expression = expression.appendMinus()!;
expression = expression.appendDigit(2)!;
expression = expression.computeResult()!;
expect(expression.state, equals(ExpressionState.Result));
expect(expression.toString(), equals('-1'));
});
}