logic.dart 3.73 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter_gallery/demo/calculator/logic.dart';
6

7
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
8 9 10

void main() {
  test('Test order of operations: 12 + 3 * 4 = 24', () {
11
    CalcExpression expression = CalcExpression.empty();
12 13 14 15 16 17 18
    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()!;
19
    expect(expression.state, equals(ExpressionState.Result));
20
    expect(expression.toString(), equals('24'));
21 22 23
  });

  test('Test floating point 0.1 + 0.2 = 0.3', () {
24
    CalcExpression expression = CalcExpression.empty();
25 26 27 28 29 30 31 32
    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()!;
33
    expect(expression.state, equals(ExpressionState.Result));
34
    expect(expression.toString(), equals('0.3'));
35 36 37
  });

  test('Test floating point 1.0/10.0 = 0.1', () {
38
    CalcExpression expression = CalcExpression.empty();
39 40 41 42 43 44 45 46 47
    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()!;
48
    expect(expression.state, equals(ExpressionState.Result));
49
    expect(expression.toString(), equals('0.1'));
50 51 52
  });

  test('Test 1/0 = Infinity', () {
53
    CalcExpression expression = CalcExpression.empty();
54 55 56 57
    expression = expression.appendDigit(1)!;
    expression = expression.appendOperation(Operation.Division)!;
    expression = expression.appendDigit(0)!;
    expression = expression.computeResult()!;
58
    expect(expression.state, equals(ExpressionState.Result));
59
    expect(expression.toString(), equals('Infinity'));
60 61 62
  });

  test('Test use result in next calculation: 1 + 1 = 2 + 1 = 3 + 1 = 4', () {
63
    CalcExpression expression = CalcExpression.empty();
64 65 66 67 68 69 70 71 72 73
    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()!;
74
    expect(expression.state, equals(ExpressionState.Result));
75
    expect(expression.toString(), equals('4'));
76 77 78
  });

  test('Test minus -3 - -2 = -1', () {
79
    CalcExpression expression = CalcExpression.empty();
80 81 82 83 84 85
    expression = expression.appendMinus()!;
    expression = expression.appendDigit(3)!;
    expression = expression.appendMinus()!;
    expression = expression.appendMinus()!;
    expression = expression.appendDigit(2)!;
    expression = expression.computeResult()!;
86
    expect(expression.state, equals(ExpressionState.Result));
87
    expect(expression.toString(), equals('-1'));
88 89
  });
}