share/libgc: add a feature to script to use a local version of the source pkgs
[nit.git] / examples / calculator / src / calculator_test.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013-2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Test the business logic
18 module calculator_test
19
20 import calculator_logic
21
22 # context tests
23 var context = new CalculatorContext
24 context.push_digit( 1 )
25 context.push_digit( 2 )
26 context.push_op( '+' )
27 context.push_digit( 3 )
28 context.push_op( '*' )
29 context.push_digit( 2 )
30 context.push_op( '=' )
31 var r = context.result
32 assert r == "30.00" else print r or else "-"
33
34 context = new CalculatorContext
35 context.push_digit( 1 )
36 context.push_digit( 4 )
37 context.switch_to_decimals
38 context.push_digit( 1 )
39 context.push_op( '*' )
40 context.push_digit( 3 )
41 context.push_op( '=' )
42 r = context.result
43 assert r == "42.30" else print r or else "-"
44
45 context.push_op( '+' )
46 context.push_digit( 1 )
47 context.push_digit( 1 )
48 context.push_op( '=' )
49 r = context.result
50 assert r == "53.30" else print r or else "-"
51
52 context = new CalculatorContext
53 context.push_digit( 4 )
54 context.push_digit( 2 )
55 context.switch_to_decimals
56 context.push_digit( 3 )
57 context.push_op( '/' )
58 context.push_digit( 3 )
59 context.push_op( '=' )
60 r = context.result
61 assert r == "14.10" else print r or else "-"
62
63 #test multiple decimals
64 context = new CalculatorContext
65 context.push_digit( 5 )
66 context.push_digit( 0 )
67 context.switch_to_decimals
68 context.push_digit( 1 )
69 context.push_digit( 2 )
70 context.push_digit( 3 )
71 context.push_op( '+' )
72 context.push_digit( 1 )
73 context.push_op( '=' )
74 r = context.result
75 assert r == "51.123" else print r or else "-"
76
77 #test 'C' button
78 context = new CalculatorContext
79 context.push_digit( 1 )
80 context.push_digit( 0 )
81 context.push_op( '+' )
82 context.push_digit( 1 )
83 context.push_digit( 0 )
84 context.push_op( '=' )
85 context.push_op( 'C' )
86 r = context.result
87 assert r == "0.0" else print r or else "-"